package com.ruoyi.postCheck.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ruoyi.common.constant.PostPlanEnum;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostPlanInfoMapper;
import com.ruoyi.postCheck.domain.PostPlanInfo;
import com.ruoyi.postCheck.service.IPostPlanInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 岗检计划明细表Service业务层处理
 *
 * @author csg
 * @date 2023-12-19
 */
@Service
public class PostPlanInfoServiceImpl extends ServiceImpl<PostPlanInfoMapper, PostPlanInfo> implements IPostPlanInfoService {
    @Autowired
    private PostPlanInfoMapper postPlanInfoMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 查询岗检计划明细表
     *
     * @param id 岗检计划明细表主键
     * @return 岗检计划明细表
     */
    @Override
    public PostPlanInfo selectPostPlanInfoById(Long id) {
        return postPlanInfoMapper.selectPostPlanInfoById(id);
    }

    /**
     * 查询岗检计划明细表列表
     *
     * @param postPlanInfo 岗检计划明细表
     * @return 岗检计划明细表
     */
    @Override
    public List<PostPlanInfo> selectPostPlanInfoList(PostPlanInfo postPlanInfo) {
        return postPlanInfoMapper.selectPostPlanInfoList(postPlanInfo);
    }

    /**
     * 新增岗检计划明细表：涉及部门和岗位是必选参数
     *
     * @param postPlanInfo 岗检计划明细表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPostPlanInfo(PostPlanInfo postPlanInfo) {
        if (postPlanInfo.getCheckedDeptId() == null || postPlanInfo.getCheckedPostId() == null) {
            throw new BaseException("涉及部门和涉及岗位的值为空，请重新选择！");
        }
        postPlanInfo.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
        postPlanInfo.setCreateDefault();
        postPlanInfo.setStatus(PostPlanEnum.INFO_STATUS_NO.getValue());
        postPlanInfo.setCreateUserId(SecurityUtils.getUserId());

        SysDept sysDept = sysDeptService.selectDeptById(postPlanInfo.getCheckedDeptId());
        SysPost sysPost = sysPostService.selectPostById(postPlanInfo.getCheckedPostId());
        int userCount = sysPostService.countUserByPost(postPlanInfo.getCheckedPostId());
        //Version
        postPlanInfo.setCheckedDeptType(sysDept.getType());
        postPlanInfo.setCheckedDeptName(sysDept.getDeptName());
        postPlanInfo.setCheckedPostName(sysPost.getPostName());
        postPlanInfo.setCheckedPostUserCount(userCount);

        return postPlanInfoMapper.insertPostPlanInfo(postPlanInfo);
    }

    /**
     * 修改岗检计划明细表
     *
     * @param postPlanInfo 岗检计划明细表
     * @return 结果
     */
    @Override
    public int updatePostPlanInfo(PostPlanInfo postPlanInfo) {
        postPlanInfo.setUpdateDefault();

        SysDept sysDept = sysDeptService.selectDeptById(postPlanInfo.getCheckedDeptId());
        SysPost sysPost = sysPostService.selectPostById(postPlanInfo.getCheckedPostId());
        int userCount = sysPostService.countUserByPost(postPlanInfo.getCheckedPostId());

        //Version
        postPlanInfo.setCheckedDeptType(sysDept.getType());
        postPlanInfo.setCheckedDeptName(sysDept.getDeptName());
        postPlanInfo.setCheckedPostName(sysPost.getPostName());
        postPlanInfo.setCheckedPostUserCount(userCount);

        return postPlanInfoMapper.updatePostPlanInfo(postPlanInfo);
    }

    /**
     * 批量删除岗检计划明细表
     *
     * @param ids 需要删除的岗检计划明细表主键
     * @return 结果
     */
    @Override
    public int deletePostPlanInfoByIds(Long[] ids) {
        return postPlanInfoMapper.deletePostPlanInfoByIds(ids);
    }

    /**
     * 删除岗检计划明细表信息
     *
     * @param id 岗检计划明细表主键
     * @return 结果
     */
    @Override
    public int deletePostPlanInfoById(Long id) {
        return postPlanInfoMapper.deletePostPlanInfoById(id);
    }

    /**
     * 根据计划ID查询岗位的检查时间和检查人
     *
     * @param planId
     * @return
     */
    @Override
    public List<PostPlanInfo> selectCheckTimeAndUser(Long planId) {
        return postPlanInfoMapper.selectCheckTimeAndUser(planId);
    }

    /**
     * 查询当年待检查岗位的集合
     * 条件：planStatus
     * 条件：params.beginTime
     * 条件：params.endTime
     * @param shouldCheckedParam
     * @return
     */
    @Override
    public List<PostPlanInfo> selectShouldCheckedList(PostPlanInfo shouldCheckedParam) {
        return postPlanInfoMapper.selectShouldCheckedList(shouldCheckedParam);
    }
}
