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.common.utils.SecurityUtils;
import com.ruoyi.system.domain.checkPoint.CheckPointRecord;
import com.ruoyi.system.mapper.CheckPointRecordMapper;
import com.ruoyi.system.service.ICheckPointRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

/**
 * 巡更点记录Service业务层处理
 * 
 * @author boman
 * @date 2025-02-14
 */
@Service
public class CheckPointRecordServiceImpl implements ICheckPointRecordService 
{
    @Autowired
    private CheckPointRecordMapper checkPointRecordMapper;

    /**
     * 查询巡更点记录
     * 
     * @param checkPointRecordId 巡更点记录主键
     * @return 巡更点记录
     */
    @Override
    public CheckPointRecord selectCheckPointRecordByCheckPointRecordId(Long checkPointRecordId)
    {
        return checkPointRecordMapper.selectCheckPointRecordByCheckPointRecordId(checkPointRecordId);
    }

    /**
     * 查询巡更点记录列表
     * 
     * @param checkPointRecord 巡更点记录
     * @return 巡更点记录
     */
    @Override
    public List<CheckPointRecord> selectCheckPointRecordList(CheckPointRecord checkPointRecord)
    {
        return checkPointRecordMapper.selectCheckPointRecordList(checkPointRecord);
    }

    /**
     * 新增巡更点记录
     * 
     * @param checkPointRecord 巡更点记录
     * @return 结果
     */
    @Override
    public int insertCheckPointRecord(CheckPointRecord checkPointRecord)
    {
        checkPointRecord.setCreateTime(DateUtils.getNowDate());
        return checkPointRecordMapper.insertCheckPointRecord(checkPointRecord);
    }

    /**
     * 修改巡更点记录
     * 
     * @param checkPointRecord 巡更点记录
     * @return 结果
     */
    @Override
    public int updateCheckPointRecord(CheckPointRecord checkPointRecord)
    {
        StaffManage staffManage = SecurityUtils.getLoginUser().getStaffManage();
        checkPointRecord.setUpdateTime(DateUtils.getNowDate());
        if (staffManage != null){
            checkPointRecord.setCheckUserName(staffManage.getStaffName());
            checkPointRecord.setCheckUsePhone(staffManage.getPhoneNumber());
            checkPointRecord.setCheckStaffId(staffManage.getStaffId());
        }
        checkPointRecord.setCheckUseId(SecurityUtils.getUserId());
        return checkPointRecordMapper.updateCheckPointRecord(checkPointRecord);
    }

    /**
     * 批量删除巡更点记录
     * 
     * @param checkPointRecordIds 需要删除的巡更点记录主键
     * @return 结果
     */
    @Override
    public int deleteCheckPointRecordByCheckPointRecordIds(Long[] checkPointRecordIds)
    {
        return checkPointRecordMapper.deleteCheckPointRecordByCheckPointRecordIds(checkPointRecordIds);
    }

    /**
     * 删除巡更点记录信息
     * 
     * @param checkPointRecordId 巡更点记录主键
     * @return 结果
     */
    @Override
    public int deleteCheckPointRecordByCheckPointRecordId(Long checkPointRecordId)
    {
        return checkPointRecordMapper.deleteCheckPointRecordByCheckPointRecordId(checkPointRecordId);
    }

    /**
     * 巡更点记录首页统计
     * @param checkPointRecord
     * @return
     */
    @Override
    public AjaxResult getCheckPointRecordCount(CheckPointRecord checkPointRecord) {
        Map<String,Object> resultMap = new HashMap<>(3);
        resultMap.put("checkPoint",0);
        resultMap.put("yCheckPoint",0);
        resultMap.put("wCheckPoint",0);
        checkPointRecord.setCreateTime(DateUtils.getNowDate());
        //查询出今日所有巡更点记录
        List<CheckPointRecord> checkPointRecords = checkPointRecordMapper.selectCheckPointRecordList(checkPointRecord);
        if (checkPointRecords != null && checkPointRecords.size() > 0){
            //判断巡更时间不为null的代表已经巡更
            resultMap.put("checkPoint",checkPointRecords.size());
            long checkCount = checkPointRecords.stream().filter(e -> TWO.equals(e.getCheckStatus())).count();
            resultMap.put("yCheckPoint",checkCount);
            resultMap.put("wCheckPoint",checkPointRecords.size()-checkCount);
        }
        return AjaxResult.success(resultMap);
    }
}
