package com.zd.laboratory.service.impl;

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.laboratory.domain.LabApproval;
import com.zd.laboratory.domain.LabSecurityGroup;
import com.zd.laboratory.domain.LabSecurityMaterial;
import com.zd.laboratory.domain.LabSubjectSecurityconfigure;
import com.zd.laboratory.domain.vo.LabApprovalVO;
import com.zd.laboratory.domain.vo.LabSecurityGroupVO;
import com.zd.laboratory.mapper.LabApprovalMapper;
import com.zd.laboratory.mapper.LabSecurityGroupMapper;
import com.zd.laboratory.mapper.LabSecurityMaterialMapper;
import com.zd.laboratory.mapper.LabSubjectSecurityconfigureMapper;
import com.zd.laboratory.service.ILabSecurityGroupService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 安全准入资格Service业务层处理
 *
 * @author zd
 * @date 2021-08-31
 */
@Service
public class LabSecurityGroupServiceImpl implements ILabSecurityGroupService {
    @Autowired
    private LabSecurityGroupMapper labSecurityGroupMapper;

    @Autowired
    private LabSecurityMaterialMapper labSecurityMaterialMapper;

    @Autowired
    private LabSubjectSecurityconfigureMapper labSubjectSecurityconfigureMapper;

    @Autowired
    private LabApprovalMapper labApprovalMapper;
    @Autowired
    private TokenService tokenService;


    /**
     * 查询安全准入资格
     *
     * @param id 安全准入资格主键
     * @return 安全准入资格
     */
    @Override
    public LabSecurityGroupVO selectLabSecurityGroupById(Long id) {
        LabSecurityGroupVO securityGroupVo = labSecurityGroupMapper.selectLabSecurityGroupById(id);
        LabSecurityMaterial securityMaterial = new LabSecurityMaterial();
        securityMaterial.setConfigureId(securityGroupVo.getId());
        List<LabSecurityMaterial> meterialList = labSecurityMaterialMapper.selectLabSecurityMaterialList(securityMaterial);
        securityGroupVo.setMeterialList(meterialList);
        LabSubjectSecurityconfigure securityConfig = new LabSubjectSecurityconfigure();
        securityConfig.setGroupconfigureId(securityGroupVo.getId());
        List<LabSubjectSecurityconfigure> subjectList = labSubjectSecurityconfigureMapper.selectLabSubjectSecurityconfigureList(securityConfig);
        securityGroupVo.setSubjectList(subjectList);
        return securityGroupVo;
    }

    /**
     * 查询安全准入资格列表
     *
     * @param labSecurityGroup 安全准入资格
     * @return 安全准入资格
     */
    @Override
    @DataScope(deptAlias = "z")
    public List<LabSecurityGroup> selectLabSecurityGroupList(LabSecurityGroup labSecurityGroup) {
        return labSecurityGroupMapper.selectLabSecurityGroupList(labSecurityGroup);
    }

    /**
     * 新增安全准入资格
     *
     * @param labSecurityGroupVo 安全准入资格
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLabSecurityGroup(LabSecurityGroupVO labSecurityGroupVo) {

        SysUser user = tokenService.getLoginUser().getSysUser();

        LabSecurityGroup labSecurityGroup = new LabSecurityGroup();
        labSecurityGroup.setConfigureName(labSecurityGroupVo.getConfigureName());
        labSecurityGroup.setEffectiveTime(labSecurityGroupVo.getEffectiveTime());

        if(user.getDept() != null){
            labSecurityGroup.setDeptId(user.getDeptId());
            labSecurityGroup.setDeptName(user.getDept().getDeptName());
        }
        labSecurityGroup.setUserId(user.getUserId());
        labSecurityGroup.setCreateBy(user.getNickName());
        labSecurityGroup.setCreateTime(DateUtils.getNowDate());
        int flag = labSecurityGroupMapper.insertLabSecurityGroup(labSecurityGroup);
        return securityGroup(labSecurityGroupVo, labSecurityGroup, flag);
    }

    private int securityGroup(LabSecurityGroupVO labSecurityGroupVo, LabSecurityGroup labSecurityGroup, int flag) {
        if(labSecurityGroupVo.getMeterialList().size()>0){
            for(LabSecurityMaterial meterial : labSecurityGroupVo.getMeterialList()){
                if("".equals(meterial.getMaterialName()) || null == meterial.getMaterialName()){
                    throw new ServiceException("所需材料名称不能为空！");
                }
            }
        }else{
            throw new ServiceException("所需材料不能为空！");
        }
        if(labSecurityGroupVo.getSubjectList().size()==0){
            throw new ServiceException("所选实验室不能为空！");
        }

        for (LabSecurityMaterial meterial : labSecurityGroupVo.getMeterialList()) {
            meterial.setId(null);
            meterial.setConfigureId(labSecurityGroup.getId());
            labSecurityMaterialMapper.insertLabSecurityMaterial(meterial);
        }
        for (LabSubjectSecurityconfigure subSecConfigure : labSecurityGroupVo.getSubjectList()) {
            subSecConfigure.setId(null);
            subSecConfigure.setGroupconfigureId(labSecurityGroup.getId());
            labSubjectSecurityconfigureMapper.insertLabSubjectSecurityconfigure(subSecConfigure);
        }
        return flag;
    }

    /**
     * 修改安全准入资格
     *
     * @param labSecurityGroupVo 安全准入资格
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateLabSecurityGroup(LabSecurityGroupVO labSecurityGroupVo) {
        LabSecurityGroup labSecurityGroup = new LabSecurityGroup();
        labSecurityGroup.setId(labSecurityGroupVo.getId());
        labSecurityGroup.setConfigureName(labSecurityGroupVo.getConfigureName());
        labSecurityGroup.setEffectiveTime(labSecurityGroupVo.getEffectiveTime());
        labSecurityGroup.setUpdateBy(SecurityUtils.getUsername());
        labSecurityGroup.setUpdateTime(DateUtils.getNowDate());
        int flag = labSecurityGroupMapper.updateLabSecurityGroup(labSecurityGroup);
        //删除准入资格下的材料表数据
        labSecurityMaterialMapper.deleteLabSecurityMaterialByConfigureId(labSecurityGroup.getId());
        //删除准入资格下对应的实验室关联表数据
        labSubjectSecurityconfigureMapper.deleteLabSubjectSecurityconfigureByConfigId(labSecurityGroup.getId());
        return securityGroup(labSecurityGroupVo, labSecurityGroup, flag);
    }

    /**
     * 批量删除安全准入资格
     *
     * @param ids 需要删除的安全准入资格主键
     * @return 结果
     */
    @Override
    public int deleteLabSecurityGroupByIds(Long[] ids) {
        return labSecurityGroupMapper.deleteLabSecurityGroupByIds(ids);
    }

    /**
     * 删除安全准入资格信息
     *
     * @param id 安全准入资格主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteLabSecurityGroupById(Long id) {
        LabApproval labApproval = new LabApproval();
        labApproval.setSecurityGroupId(id);
        List<LabApprovalVO> approvalList = labApprovalMapper.selectLabApprovalListBySecurityGroupId(labApproval);
        if(approvalList.size()>0){
            throw new ServiceException("当前准入资格已被使用，不能删除！");
        }
        //删除准入资格下的材料表数据
        labSecurityMaterialMapper.deleteLabSecurityMaterialByConfigureId(id);
        //删除准入资格下对应的实验室关联表数据
        labSubjectSecurityconfigureMapper.deleteLabSubjectSecurityconfigureByConfigId(id);
        return labSecurityGroupMapper.deleteLabSecurityGroupById(id);
    }
}
