package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BusinessTrip;
import com.ruoyi.system.domain.KaoqinRecord;
import com.ruoyi.system.domain.ProcessConfiguration;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.CardReplacementRecord;
import com.ruoyi.system.service.ICardReplacementRecordService;

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

/**
 * 补卡记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-08
 */
@Service
public class CardReplacementRecordServiceImpl implements ICardReplacementRecordService 
{
    @Autowired
    private CardReplacementRecordMapper cardReplacementRecordMapper;

    @Autowired
    private ProcessConfigurationMapper processConfigurationMapper;

    @Autowired
    private KaoqinRecordMapper kaoqinRecordMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper deptMapper;
    /**
     * 查询补卡记录
     * 
     * @param id 补卡记录主键
     * @return 补卡记录
     */
    @Override
    public CardReplacementRecord selectCardReplacementRecordById(Long id)
    {
        return cardReplacementRecordMapper.selectCardReplacementRecordById(id);
    }

    /**
     * 查询补卡记录列表
     * 
     * @param cardReplacementRecord 补卡记录
     * @return 补卡记录
     */
    @Override
    public List<CardReplacementRecord> selectCardReplacementRecordList(CardReplacementRecord cardReplacementRecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        cardReplacementRecord.setUserId(user.getUserId());
        return cardReplacementRecordMapper.selectCardReplacementRecordList(cardReplacementRecord);
    }

    /**
     * 新增补卡记录
     * 
     * @param cardReplacementRecord 补卡记录
     * @return 结果
     */
    @Override
    public int insertCardReplacementRecord(CardReplacementRecord cardReplacementRecord)
    {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        cardReplacementRecord.setUserId(user.getUserId());
        cardReplacementRecord.setUserName(user.getNickName());
        cardReplacementRecord.setDeptId(user.getDeptId());
        cardReplacementRecord.setDeptName(user.getDept().getDeptName());
        String date = DateUtils.dateTime(cardReplacementRecord.getApplicationTime());
        cardReplacementRecord.setApplicationDate(date);
        cardReplacementRecord.setCreateTime(DateUtils.getNowDate());
        return cardReplacementRecordMapper.insertCardReplacementRecord(cardReplacementRecord);
    }

    /**
     * 修改补卡记录
     * 
     * @param cardReplacementRecord 补卡记录
     * @return 结果
     */
    @Override
    public int updateCardReplacementRecord(CardReplacementRecord cardReplacementRecord)
    {
        CardReplacementRecord cardReplacementRecord1 = cardReplacementRecordMapper.selectCardReplacementRecordById(cardReplacementRecord.getId());
        if("0".equals(cardReplacementRecord1.getIsPass())){
            throw new ServiceException("当前补卡已处理，无法修改");
        }
        return cardReplacementRecordMapper.updateCardReplacementRecord(cardReplacementRecord);
    }

    /**
     * 批量删除补卡记录
     * 
     * @param ids 需要删除的补卡记录主键
     * @return 结果
     */
    @Override
    public int deleteCardReplacementRecordByIds(Long[] ids)
    {
        return cardReplacementRecordMapper.deleteCardReplacementRecordByIds(ids);
    }

    /**
     * 删除补卡记录信息
     * 
     * @param id 补卡记录主键
     * @return 结果
     */
    @Override
    public int deleteCardReplacementRecordById(Long id)
    {
        return cardReplacementRecordMapper.deleteCardReplacementRecordById(id);
    }

    @Override
    public int putPass(CardReplacementRecord cardReplacementRecord) {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //查询打卡信息
        if(TWO.equals(cardReplacementRecord.getIsPass())){
            //查询信息
            CardReplacementRecord cardReplacementRecord1 = cardReplacementRecordMapper.selectCardReplacementRecordById(cardReplacementRecord.getId());
            //查询补卡人员信息
            SysUser sysUser = sysUserMapper.selectUserById(cardReplacementRecord1.getUserId());
            String date = cardReplacementRecord1.getApplicationDate();
            KaoqinRecord kaoqinRecord = kaoqinRecordMapper.selectKaoqinRecordByRecordByUser(sysUser.getUserId(),date);
            if(kaoqinRecord != null){
                if(ONE.equals(cardReplacementRecord.getKaSort())){
                    kaoqinRecord.setKaTypeAmIn(SIX);
                } else if (TWO.equals(cardReplacementRecord.getKaSort())) {
                    kaoqinRecord.setKaTypeAmOut(SIX);
                } else if (THREE.equals(cardReplacementRecord.getKaSort())) {
                    kaoqinRecord.setKaTypePmIn(SIX);
                } else if (FOUR.equals(cardReplacementRecord.getKaSort())) {
                    kaoqinRecord.setKaTypePmOut(SIX);
                }
                kaoqinRecordMapper.updateKaoqinRecord(kaoqinRecord);
            }else{
                kaoqinRecord = new KaoqinRecord();
                //将日期拆分成年月日的数组
                String[] datas = date.split("-");
                KaoqinRecord kaoqinRecords = new KaoqinRecord();
                kaoqinRecords.setUserId(sysUser.getUserId());
                kaoqinRecords.setUserName(sysUser.getNickName());
                kaoqinRecords.setPhone(sysUser.getPhonenumber());
                kaoqinRecords.setDeptId(sysUser.getDeptId());
                kaoqinRecords.setDeptName(sysUser.getDept().getDeptName());
                kaoqinRecords.setAncestors(sysUser.getDept().getAncestors());
                kaoqinRecords.setKaYear(datas[0]);
                kaoqinRecords.setKaMonth(datas[1]);
                kaoqinRecords.setKaDay(datas[2]);
                kaoqinRecords.setKaWeek(DateUtils.dateToWeek(date));
                kaoqinRecords.setKaTime(date);
                kaoqinRecords.setKaStatus(TWO);
                if(ONE.equals(cardReplacementRecord.getKaSort())){
                    kaoqinRecord.setKaTypeAmIn(SIX);
                } else if (TWO.equals(cardReplacementRecord.getKaSort())) {
                    kaoqinRecord.setKaTypeAmOut(SIX);
                } else if (THREE.equals(cardReplacementRecord.getKaSort())) {
                    kaoqinRecord.setKaTypePmIn(SIX);
                } else if (FOUR.equals(cardReplacementRecord.getKaSort())) {
                    kaoqinRecord.setKaTypePmOut(SIX);
                }
                kaoqinRecords.setCreateTime(DateUtils.getNowDate());
                kaoqinRecordMapper.insertKaoqinRecord(kaoqinRecord);
            }

        }

        cardReplacementRecord.setExaminersId(user.getUserId());
        cardReplacementRecord.setExaminersName(user.getNickName());
        return cardReplacementRecordMapper.updateCardReplacementRecord(cardReplacementRecord);
    }

    @Override
    public List<CardReplacementRecord> presentList(CardReplacementRecord cardReplacementRecord) {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!"admin".equals(user.getUserName())){
            //获取当前人员补卡配置所在的部门id
            ProcessConfiguration processConfiguration = new ProcessConfiguration();
            processConfiguration.setUserId(user.getUserId());
            processConfiguration.setType(ONE);
            List<ProcessConfiguration> list = processConfigurationMapper.selectProcessConfigurationList(processConfiguration);
            if(list==null|| list.isEmpty()){
                List<CardReplacementRecord> cardReplacementRecordList = new ArrayList<>();
                return cardReplacementRecordList;
            }
            List<Long> deptIdList = new ArrayList<>();
            List<Long> deptIds = list.stream().map(ProcessConfiguration::getDeptId).collect(Collectors.toList());
            deptIdList.addAll(deptIds);
            for (Long deptId : deptIds) {
                //查询所有子部门
                List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
                if(children!=null && !children.isEmpty()){
                    List<Long> childrenIds = children.stream().map(SysDept::getDeptId).collect(Collectors.toList());
                    deptIdList.addAll(childrenIds);
                }
            }
            cardReplacementRecord.setDeptIds(deptIdList);
        }
        return cardReplacementRecordMapper.selectCardReplacementRecordList(cardReplacementRecord);
    }
}
