package com.zd.laboratory.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.domain.LabGradeManage;
import com.zd.laboratory.domain.LabGradeManageRuleUser;
import com.zd.laboratory.mapper.LabGradeManageRuleUserMapper;
import com.zd.laboratory.mapper.LabSubjectMapper;
import com.zd.laboratory.mqtt.constants.MqttConstants;
import com.zd.laboratory.mqtt.entiy.MessageBody;
import com.zd.laboratory.mqtt.service.impl.CommonSend;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabGradeManageRuleMapper;
import com.zd.laboratory.domain.LabGradeManageRule;
import com.zd.laboratory.service.ILabGradeManageRuleService;

/**
 * 分级管控规则Service业务层处理
 *
 * @author liubo
 * @date 2022-02-10
 */
@Service
public class LabGradeManageRuleServiceImpl implements ILabGradeManageRuleService {
    @Autowired
    private LabGradeManageRuleMapper labGradeManageRuleMapper;
    @Autowired
    private LabGradeManageRuleUserMapper labGradeManageRuleUserMapper;
    @Autowired
    private LabSubjectMapper labSubjectMapper;
    @Autowired
    private CommonSend commonSend;

    /**
     * 查询分级管控规则
     *
     * @param id 分级管控规则主键
     * @return 分级管控规则
     */
    @Override
    public LabGradeManageRule selectLabGradeManageRuledById(Long id) {
        return labGradeManageRuleMapper.selectLabGradeManageRuledById(id);
    }

    /**
     * 查询分级管控规则列表
     *
     * @param labGradeManageRule 分级管控规则
     * @return 分级管控规则
     */
    @Override
    public List<LabGradeManageRule> selectLabGradeManageRuledList(LabGradeManageRule labGradeManageRule) {
        return labGradeManageRuleMapper.selectLabGradeManageRuledList(labGradeManageRule);
    }

    /**
     * 新增分级管控规则
     *
     * @param labGradeManageRule 分级管控规则
     * @return 结果
     */
    @Override
    public int insertLabGradeManageRuled(LabGradeManageRule labGradeManageRule) {
        //设置其他公共字段
        SaveUtil.setCommonAttr(labGradeManageRule);
        return labGradeManageRuleMapper.insertLabGradeManageRuled(labGradeManageRule);
    }

    /**
     * 修改分级管控规则
     *
     * @param labGradeManageRule 分级管控规则
     * @return 结果
     */
    @Override
    public int updateLabGradeManageRuled(LabGradeManageRule labGradeManageRule) {
        labGradeManageRule.setUpdateTime(DateUtils.getNowDate());
        labGradeManageRule.setUpdateBy(SecurityUtils.getUsername());
        return labGradeManageRuleMapper.updateLabGradeManageRuled(labGradeManageRule);
    }

    /**
     * 批量删除分级管控规则
     *
     * @param ids 需要删除的分级管控规则主键
     * @return 结果
     */
    @Override
    public int deleteLabGradeManageRuledByIds(Long[] ids) {
        return labGradeManageRuleMapper.deleteLabGradeManageRuledByIds(ids);
    }

    /**
     * 删除分级管控规则信息
     *
     * @param id 分级管控规则主键
     * @return 结果
     */
    @Override
    public int deleteLabGradeManageRuledById(Long id) {
        return labGradeManageRuleMapper.deleteLabGradeManageRuledById(id);
    }

    @Override
    public void insertLabGradeManageRuled(List<LabGradeManageRule> ruleList, LabGradeManage labGradeManage) {

        if(CollectionUtils.isEmpty(ruleList)){
            return;
        }

        if(labGradeManage.getId() == null) {
            throw new ServiceException("参数异常，ID不能为空！");
        }

        labGradeManageRuleMapper.deleteByGradeManageId(labGradeManage.getId());

        labGradeManageRuleUserMapper.deleteByGradeManageId(labGradeManage.getId());

        List<LabGradeManageRuleUser> allRuleUseList = new ArrayList<>();
        List<LabGradeManageRuleUser> ruleUseList;

        Map<Integer, List<Long>> userWork = new HashMap<>();
        for (LabGradeManageRule labGradeManageRule : ruleList) {
            labGradeManageRule.setGradeManageId(labGradeManage.getId());
            SaveUtil.setCommonAttr(labGradeManageRule);

            labGradeManageRuleMapper.insertLabGradeManageRuled(labGradeManageRule);
            ruleUseList = labGradeManageRule.getRuleUserList();
            // 同一规则下周期不能重复
            if(CollectionUtils.isNotEmpty(ruleUseList)){
                List<Long> userIds = new ArrayList<>();
                for (LabGradeManageRuleUser labGradeManageRuleUser : ruleUseList) {

                    SaveUtil.setCommonAttr(labGradeManageRuleUser);
                    labGradeManageRuleUser.setUpdateTime(DateUtils.getNowDate());
                    if(labGradeManageRuleUser.getType() == 2){
                        // 当天进入实验室人员 周期只能选一天
                        labGradeManageRuleUser.setCycle("一天");

                    }
                    labGradeManageRuleUser.setGradeManageId(labGradeManage.getId());
                    labGradeManageRuleUser.setGradeManageRuleId(labGradeManageRule.getId());
                    SaveUtil.setCommonAttr(labGradeManageRule);

                    if(labGradeManageRuleUser.getType() == 3 && StringUtils.isNotBlank(labGradeManageRuleUser.getUserIds())){
                        userIds.addAll(Arrays.asList(labGradeManageRuleUser.getUserIds() .split(",")).stream().map(s -> (Long.parseLong(s.trim()))).collect(Collectors.toList()));
                    }else if(labGradeManageRuleUser.getType() == 1){
                        //userIds.addAll(labSubjectMapper.selectAdminIds(labGradeManage.getSubs()));
                    }
                }

                if(userIds.size() > 0){
                    userWork.put(labGradeManage.getSubs().size(), userIds);
                }
                allRuleUseList.addAll(ruleUseList);
            }

        }
        if(allRuleUseList.size() > 0){
            labGradeManageRuleUserMapper.insertBatch(allRuleUseList);
        }

        // 生成工作通知
        /*userWork.forEach((k,v) -> {
            v.forEach(u ->{
                commonSend.send(MqttConstants.LAB_MANAGE_WORK+u,
                    new MessageBody("您当前有" + k + "条管控工作需完成，请点击查看详情。", 1));}
            );
        });*/

    }
}
