package com.zd.laboratory.service.impl;

import java.util.List;

import com.zd.common.core.domain.per.PerPrefix;
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.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.domain.LabGradeManageRule;
import com.zd.laboratory.domain.LabSubject;
import com.zd.laboratory.mapper.LabGradeManageRuleMapper;
import com.zd.laboratory.mapper.LabGradeManageRuleUserMapper;
import com.zd.laboratory.mapper.LabSubjectMapper;
import com.zd.laboratory.service.ILabGradeManageRuleService;
import com.zd.laboratory.service.ILabGradeManageRuleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabGradeManageMapper;
import com.zd.laboratory.domain.LabGradeManage;
import com.zd.laboratory.service.ILabGradeManageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分级管控管理Service业务层处理
 *
 * @author liubo
 * @date 2022-02-10
 */
@Service
public class LabGradeManageServiceImpl implements ILabGradeManageService {
    @Autowired
    private LabSubjectMapper labSubjectMapper;
    @Autowired
    private LabGradeManageMapper labGradeManageMapper;
    @Autowired
    private LabGradeManageRuleMapper labGradeManageRuleMapper;
    @Autowired
    private ILabGradeManageRuleService labGradeManageRuleService;
    @Autowired
    private LabGradeManageRuleUserMapper labGradeManageRuleUserMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询分级管控管理
     *
     * @param id 分级管控管理主键
     * @return 分级管控管理
     */
    @Override
    public LabGradeManage selectLabGradeManageById(Long id) {
        LabGradeManage labGradeManage = labGradeManageMapper.selectLabGradeManageById(id);

        LabGradeManageRule labGradeManageRule = new LabGradeManageRule();
            labGradeManageRule.setGradeManageId(id);
        labGradeManage.setRuleList(labGradeManageRuleMapper.selectLabGradeManageRuledList(labGradeManageRule));

        return labGradeManage;
    }

    /**
     * 查询分级管控管理列表
     *
     * @param labGradeManage 分级管控管理
     * @return 分级管控管理
     */
    @Override
    @DataScope(deptAlias = "t" , userAlias = "t" , permi = PerPrefix.LABORATORY_GRADEMANAGE)
    public List<LabGradeManage> selectLabGradeManageList(LabGradeManage labGradeManage) {
        List<LabGradeManage> list = labGradeManageMapper.selectLabGradeManageList(labGradeManage);
        return list;
    }

    /**
     * 新增分级管控管理
     *
     * @param labGradeManage 分级管控管理
     * @return 结果
     */
    @Override
    public LabGradeManage insertLabGradeManage(LabGradeManage labGradeManage) {
        // 校验同一个实验室只能设置一次
        //checkSubConfig(labGradeManage.getSubs(),null);

        labGradeManage.setCreateBy(SecurityUtils.getUsername());
        labGradeManage.setUserId(SecurityUtils.getUserId());
        labGradeManage.setCreateTime(DateUtils.getNowDate());
        labGradeManage.setUpdateBy(SecurityUtils.getUsername());
        labGradeManage.setUpdateTime(DateUtils.getNowDate());

        labGradeManageMapper.insertLabGradeManage(labGradeManage);

        labGradeManageRuleService.insertLabGradeManageRuled(labGradeManage.getRuleList(), labGradeManage);

        return labGradeManage;
    }

    /**
     * 修改分级管控管理
     *
     * @param labGradeManage 分级管控管理
     * @return 结果
     */
    @Override
    public LabGradeManage updateLabGradeManage(LabGradeManage labGradeManage) {
        // 校验同一个实验室只能设置一次
        //checkSubConfig(labGradeManage.getSubs(),labGradeManage.getId());

        labGradeManage.setUpdateTime(DateUtils.getNowDate());
        labGradeManage.setUpdateBy(SecurityUtils.getUsername());
        labGradeManageMapper.updateLabGradeManage(labGradeManage);

        labGradeManageRuleService.insertLabGradeManageRuled(labGradeManage.getRuleList(), labGradeManage);
        return labGradeManage;
    }

    @Deprecated
    private void checkSubConfig(List<String> subs, Long id) {

        int count;
        for (String subId : subs) {
            count = labGradeManageMapper.selectCountBySubId(subId, id);

            if(count > 0){
                LabSubject labSubject = labSubjectMapper.selectLabSubjectById(Long.parseLong(subId));
                throw new ServiceException("实验室：" + labSubject == null? "" : labSubject.getName() + "已经配置，不能重复配置！");
            }
        }


    }

    /**
     * 批量删除分级管控管理
     *
     * @param ids 需要删除的分级管控管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteLabGradeManageByIds(Long[] ids) {

        for (Long gradeManageId : ids) {
            labGradeManageRuleMapper.deleteByGradeManageId(gradeManageId);

            labGradeManageRuleUserMapper.deleteByGradeManageId(gradeManageId);
        }

        return labGradeManageMapper.deleteLabGradeManageByIds(ids);
    }

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

}
