package com.yanzhu.jh.work.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DeptTypeEnum;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.yanzhu.jh.project.domain.SurProject;
import com.yanzhu.jh.project.mapper.SurProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yanzhu.jh.work.domain.WorkTrainDept;
import com.yanzhu.jh.work.mapper.WorkTrainMapper;
import com.yanzhu.jh.work.domain.WorkTrain;
import com.yanzhu.jh.work.service.IWorkTrainService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 演练培训Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-09
 */
@Service
public class WorkTrainServiceImpl implements IWorkTrainService 
{
    @Autowired
    private WorkTrainMapper workTrainMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SurProjectMapper surProjectMapper;

    /**
     * 查询演练培训
     * 
     * @param id 演练培训主键
     * @return 演练培训
     */
    @Override
    public WorkTrain selectWorkTrainById(Long id)
    {
        return workTrainMapper.selectWorkTrainById(id);
    }

    /**
     * 查询演练培训列表
     * 
     * @param workTrain 演练培训
     * @return 演练培训
     */
    @Override
    public List<WorkTrain> selectWorkTrainList(WorkTrain workTrain)
    {
        return workTrainMapper.selectWorkTrainList(workTrain);
    }

    /**
     * 查询演练培训列表
     *
     * @param workTrain 演练培训
     * @return 演练培训集合
     */
    @Override
    public List<WorkTrain> selectBgscreenWorkTrainList(WorkTrain workTrain) {
        return workTrainMapper.selectBgscreenWorkTrainList(workTrain);
    }

    /**
     * 查询演练培训列表
     *
     * @param workTrain 演练培训
     * @return 演练培训集合
     */
    @Override
    public List<WorkTrain> selectBgscreenWorkTrainListv2(WorkTrain workTrain) {
        return workTrainMapper.selectBgscreenWorkTrainListv2(workTrain);
    }

    /**
     * 新增演练培训
     * 
     * @param workTrain 演练培训
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkTrain(WorkTrain workTrain)
    {
        workTrain.setCreateBy(SecurityUtils.getUsername());
        workTrain.setCreateTime(DateUtils.getNowDate());
        int rows = workTrainMapper.insertWorkTrain(workTrain);
        insertWorkTrainDept(workTrain);
        return rows;
    }

    /**
     * 修改演练培训
     * 
     * @param workTrain 演练培训
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkTrain(WorkTrain workTrain)
    {
        workTrain.setUpdateBy(SecurityUtils.getUsername());
        workTrain.setUpdateTime(DateUtils.getNowDate());
        workTrainMapper.deleteWorkTrainDeptByTrainId(workTrain.getId());
        insertWorkTrainDept(workTrain);
        return workTrainMapper.updateWorkTrain(workTrain);
    }

    /**
     * 批量删除演练培训
     * 
     * @param ids 需要删除的演练培训主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkTrainByIds(Long[] ids)
    {
        workTrainMapper.deleteWorkTrainDeptByTrainIds(ids);
        return workTrainMapper.deleteWorkTrainByIds(ids);
    }

    /**
     * 删除演练培训信息
     * 
     * @param id 演练培训主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkTrainById(Long id)
    {
        workTrainMapper.deleteWorkTrainDeptByTrainId(id);
        return workTrainMapper.deleteWorkTrainById(id);
    }

    /**
     * 新增演练培训协同部门信息
     * 
     * @param workTrain 演练培训对象
     */
    public void insertWorkTrainDept(WorkTrain workTrain)
    {
        List<WorkTrainDept> workTrainDeptList = workTrain.getWorkTrainDeptList();
        Long id = workTrain.getId();
        //添加当前部门为默认为组织部门
        if (StringUtils.isNull(workTrainDeptList)){
            workTrainDeptList = new ArrayList<>();
        }
        WorkTrainDept dept = new WorkTrainDept();
        SysDept sysDept = sysDeptMapper.selectDeptById(SecurityUtils.getDeptId());
        if(DeptTypeEnum.JTGS.getCode().equals(sysDept.getTypeFlag()) || DeptTypeEnum.ZGS.getCode().equals(sysDept.getTypeFlag())){
            SurProject surProject = surProjectMapper.selectSurProjectById(workTrain.getProjectId());
            dept.setDeptId(surProject.getDeptId());
        }else{
            dept.setDeptId(sysDept.getDeptId());
        }
        dept.setIsMain(PublicStateEnum.YES.getCode());
        workTrainDeptList.add(dept);

        if (StringUtils.isNotNull(workTrainDeptList))
        {
            List<WorkTrainDept> list = new ArrayList<WorkTrainDept>();
            for (WorkTrainDept workTrainDept : workTrainDeptList)
            {
                workTrainDept.setTrainId(id);
                if(workTrainDept.getIsMain()==null){
                    workTrainDept.setIsMain(PublicStateEnum.NO.getCode());
                }
                list.add(workTrainDept);
            }
            if (list.size() > 0)
            {
                workTrainMapper.batchWorkTrainDept(list);
            }
        }
    }
}
