package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.StaffManage;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.propertyRepair.PropertyRepair;
import com.ruoyi.system.mapper.PropertyRepairMapper;
import com.ruoyi.system.mapper.StaffManageMapper;
import com.ruoyi.system.service.IPropertyRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.constant.Constants.*;

/**
 * 物业报修Service业务层处理
 * 
 * @author boman
 * @date 2025-02-14
 */
@Service
public class PropertyRepairServiceImpl implements IPropertyRepairService 
{
    @Autowired
    private PropertyRepairMapper propertyRepairMapper;
    @Autowired
    private StaffManageMapper staffManageMapper;

    /**
     * 查询物业报修
     * 
     * @param repairId 物业报修主键
     * @return 物业报修
     */
    @Override
    public PropertyRepair selectPropertyRepairByRepairId(Long repairId)
    {
        return propertyRepairMapper.selectPropertyRepairByRepairId(repairId);
    }

    /**
     * 查询物业报修列表
     * 
     * @param propertyRepair 物业报修
     * @return 物业报修
     */
    @Override
    public List<PropertyRepair> selectPropertyRepairList(PropertyRepair propertyRepair)
    {
        return propertyRepairMapper.selectPropertyRepairList(propertyRepair);
    }

    /**
     * 新增物业报修
     * 
     * @param propertyRepair 物业报修
     * @return 结果
     */
    @Override
    public int insertPropertyRepair(PropertyRepair propertyRepair)
    {
        propertyRepair.setCreateTime(DateUtils.getNowDate());
        return propertyRepairMapper.insertPropertyRepair(propertyRepair);
    }

    /**
     * 修改物业报修
     * 
     * @param propertyRepair 物业报修
     * @return 结果
     */
    @Override
    public int updatePropertyRepair(PropertyRepair propertyRepair)
    {
        propertyRepair.setUpdateTime(DateUtils.getNowDate());
        int i = propertyRepairMapper.updatePropertyRepair(propertyRepair);
        String repairStatus = propertyRepair.getRepairStatus();
        if (FOR.equals(repairStatus)){
            //判断如果维修完成，把维修人员的状态改成空闲
            StaffManage staffManage = new StaffManage();
            staffManage.setStaffId(propertyRepair.getStaffId());
            staffManage.setStatus(ONE);
            staffManageMapper.updateStaffManage(staffManage);
        }
        return i;
    }
    /**
     * 指派物业报修人员
     */
    @Override
    public AjaxResult assign(PropertyRepair propertyRepair) {

        Long staffId = propertyRepair.getStaffId();
        StaffManage staffManageOld = staffManageMapper.selectStaffManageByStaffId(staffId);
        if (TWO.equals(staffManageOld.getStatus())){
            return AjaxResult.error("当前人员处于不在空闲状态无法指派");
        }
        //报修ID/维修人员姓名/维修人员ID/维修人员手机号
        int i = propertyRepairMapper.updatePropertyRepair(propertyRepair);
        //修改维修人员的状态
        if (i > 0){
            StaffManage staffManage = new StaffManage();
            staffManage.setStaffId(propertyRepair.getStaffId());
            staffManage.setStatus(TWO);
            staffManageMapper.updateStaffManage(staffManage);
        }
        //指派后修改该物业报修表的状态的
        propertyRepair.setRepairStatus(TWO);
        propertyRepairMapper.updatePropertyRepair(propertyRepair);
        return AjaxResult.success();
    }

    /**
     * 批量删除物业报修
     * 
     * @param repairIds 需要删除的物业报修主键
     * @return 结果
     */
    @Override
    public int deletePropertyRepairByRepairIds(Long[] repairIds)
    {
        return propertyRepairMapper.deletePropertyRepairByRepairIds(repairIds);
    }

    /**
     * 删除物业报修信息
     * 
     * @param repairId 物业报修主键
     * @return 结果
     */
    @Override
    public int deletePropertyRepairByRepairId(Long repairId)
    {
        return propertyRepairMapper.deletePropertyRepairByRepairId(repairId);
    }

    @Override
    public AjaxResult statisticsQx() {
        Map<String,Object> mapList = new HashMap<>();
        List<String> weekDays = DateUtils.getPreviousSevenDays(DateUtils.YYYY_MM_DD);
        String starTime = weekDays.get(0);
        String endTime = weekDays.get(weekDays.size()-1);
        mapList.put("x",weekDays);
        //查询两个日期内的保修数据
        List<PropertyRepair> propertyRepairs = propertyRepairMapper.selectPropertyRepairListByTime(starTime, endTime);
        List<Integer> numList = new ArrayList<>();
        for (int i = 0; i < weekDays.size(); i++) {
            int num = 0;
            for (PropertyRepair propertyRepair : propertyRepairs) {
                String day = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, propertyRepair.getRepairTime());
                if(day.equals(weekDays.get(i))){
                    num++;
                }
            }
            numList.add(num);
        }
        mapList.put("y",numList);
        return AjaxResult.success(mapList);
    }
}
