package com.zd.laboratory.service.impl;

import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.DictUtils;
import com.zd.common.security.utils.SaveUtil;
import com.zd.laboratory.domain.LabSubjectDengerRelation;
import com.zd.laboratory.mapper.LabSubjectDengerRelationMapper;
import com.zd.laboratory.service.ILabSubjectDengerRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 实验室安全隐患关联Service业务层处理
 *
 * @author liubo
 * @date 2021-11-13
 */
@Service
public class LabSubjectDengerRelationServiceImpl implements ILabSubjectDengerRelationService {
    @Autowired
    private LabSubjectDengerRelationMapper labSubjectDengerRelationMapper;
    @Autowired
    private LabSubjectManagerService labSubjectManagerService;

    /**
     * 查询实验室安全隐患关联
     *
     * @param id 实验室安全隐患关联主键
     * @return 实验室安全隐患关联
     */
    @Override
    public LabSubjectDengerRelation selectLabSubjectDengerRelationById(Long id) {
        return labSubjectDengerRelationMapper.selectLabSubjectDengerRelationById(id);
    }

    /**
     * 查询实验室安全隐患关联列表
     *
     * @param labSubjectDengerRelation 实验室安全隐患关联
     * @return 实验室安全隐患关联
     */
    @Override
    public List<LabSubjectDengerRelation> selectLabSubjectDengerRelationList(LabSubjectDengerRelation labSubjectDengerRelation) {
        return labSubjectDengerRelationMapper.selectLabSubjectDengerRelationList(labSubjectDengerRelation);
    }

    /**
     * 新增实验室安全隐患关联
     *
     * @param labSubjectDengerRelation 实验室安全隐患关联
     * @return 结果
     */
    @Override
    public int insertLabSubjectDengerRelation(LabSubjectDengerRelation labSubjectDengerRelation) {
        //设置其他公共字段
        SaveUtil.setCommonAttr(labSubjectDengerRelation);
        return labSubjectDengerRelationMapper.insertLabSubjectDengerRelation(labSubjectDengerRelation);
    }

    /**
     * 修改实验室安全隐患关联
     *
     * @param labSubjectDengerRelation 实验室安全隐患关联
     * @return 结果
     */
    @Override
    public int updateLabSubjectDengerRelation(LabSubjectDengerRelation labSubjectDengerRelation) {
        labSubjectDengerRelation.setUpdateTime(DateUtils.getNowDate());
        labSubjectDengerRelation.setUpdateBy(SecurityUtils.getUsername());
        return labSubjectDengerRelationMapper.updateLabSubjectDengerRelation(labSubjectDengerRelation);
    }

    /**
     * 批量删除实验室安全隐患关联
     *
     * @param ids 需要删除的实验室安全隐患关联主键
     * @return 结果
     */
    @Override
    public int deleteLabSubjectDengerRelationByIds(Long[] ids) {
        return labSubjectDengerRelationMapper.deleteLabSubjectDengerRelationByIds(ids);
    }

    /**
     * 删除实验室安全隐患关联信息
     *
     * @param id 实验室安全隐患关联主键
     * @return 结果
     */
    @Override
    public int deleteLabSubjectDengerRelationById(Long id) {
        return labSubjectDengerRelationMapper.deleteLabSubjectDengerRelationById(id);
    }

    /**
     * 查询实验室安全隐患统计
     */
    @Override
    public List<Map<String, Object>> selectLabSubjectDengerRelationStatistics(LabSubjectDengerRelation labSubjectDengerRelation) {
        List<Map<String, Object>> list = labSubjectDengerRelationMapper.selectLabSubjectDengerRelationStatistics(labSubjectDengerRelation);

        for (Map<String, Object> map : list) {
            if(map.get("dangerId") != null){
                map.put("dangerName", DictUtils.getDictCacheLable("hidden_danger", map.get("dangerId")+""));
            }
        }

        return list;
    }

    /**
     * 查询实验室安全隐患关联列表
     */
    @Override
    public List<LabSubjectDengerRelation> selectSubjectDengerRelationInfo(LabSubjectDengerRelation labSubjectDengerRelation) {
        List<LabSubjectDengerRelation> list = labSubjectDengerRelationMapper.selectSubjectDengerRelationInfo(labSubjectDengerRelation);
        for (LabSubjectDengerRelation relation : list) {
            relation.setDangerName(DictUtils.getDictCacheLable("hidden_danger", relation.getDangerId()));
            relation.setSubDept(DictUtils.getDictCacheLable("subject_class",relation.getSubDept()));
            // 风险等级
            //relation.setSubClassVO(labSubjectManagerService.getSubClassVO(relation.getSubjectId()));
            relation.setSubClassVO(null);
        }
        return list;
    }
}
