package com.zd.laboratory.service.impl;

import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.laboratory.domain.LabContainer;
import com.zd.laboratory.domain.LabHazardSubjectRelation;
import com.zd.laboratory.mapper.LabContainerMapper;
import com.zd.laboratory.service.ILabContainerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 压力容器Service业务层处理
 *
 * @author zd
 * @date 2021-09-01
 */
@Service
public class LabContainerServiceImpl implements ILabContainerService {
    @Autowired
    private LabContainerMapper labContainerMapper;

    /**
     * 查询压力容器
     *
     * @param id 压力容器主键
     * @return 压力容器
     */
    @Override
    public LabContainer selectLabContainerById(Long id) {
        return labContainerMapper.selectLabContainerById(id);
    }

    /**
     * 查询压力容器列表
     *
     * @param labContainer 压力容器
     * @return 压力容器
     */
    @Override
    public List<LabContainer> selectLabContainerList(LabContainer labContainer) {
        return labContainerMapper.selectLabContainerList(labContainer);
    }

    /**
     * 新增压力容器
     *
     * @param labContainer 压力容器
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLabContainer(LabContainer labContainer) {
        labContainer.setCreateTime(DateUtils.getNowDate());
        int rows = labContainerMapper.insertLabContainer(labContainer);
        return rows;
    }

    /**
     * 修改压力容器
     *
     * @param labContainer 压力容器
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLabContainer(LabContainer labContainer) {
        labContainer.setUpdateTime(DateUtils.getNowDate());
        labContainerMapper.deleteLabHazardSubjectRelationByHazardId(labContainer.getId());
        insertLabHazardSubjectRelation(labContainer);
        return labContainerMapper.updateLabContainer(labContainer);
    }

    /**
     * 批量删除压力容器
     *
     * @param ids 需要删除的压力容器主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLabContainerByIds(Long[] ids) {
        labContainerMapper.deleteLabHazardSubjectRelationByHazardIds(ids);
        return labContainerMapper.deleteLabContainerByIds(ids);
    }

    /**
     * 删除压力容器信息
     *
     * @param id 压力容器主键
     * @return 结果
     */
    @Override
    public int deleteLabContainerById(Long id) {
        labContainerMapper.deleteLabHazardSubjectRelationByHazardId(id);
        return labContainerMapper.deleteLabContainerById(id);
    }

    /**
     * 新增危险源和实验室关联信息
     *
     * @param labContainer 压力容器对象
     */
    public void insertLabHazardSubjectRelation(LabContainer labContainer) {
        List<LabHazardSubjectRelation> labHazardSubjectRelationList = labContainer.getLabHazardSubjectRelationList();
        Long id = labContainer.getId();
        if (StringUtils.isNotNull(labHazardSubjectRelationList)) {
            List<LabHazardSubjectRelation> list = new ArrayList<LabHazardSubjectRelation>();
            for (LabHazardSubjectRelation labHazardSubjectRelation : labHazardSubjectRelationList) {
                labHazardSubjectRelation.setHazardId(id);
                list.add(labHazardSubjectRelation);
            }
            if (list.size() > 0) {
                labContainerMapper.batchLabHazardSubjectRelation(list);
            }
        }
    }
}
