package com.ruoyi.postCheck.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.PostAssessmentRulesEnum;
import com.ruoyi.common.constant.PostCheckedReportEnum;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.postCheck.domain.PostCheckedReport;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostAssessmentRulesMapper;
import com.ruoyi.postCheck.domain.PostAssessmentRules;
import com.ruoyi.postCheck.service.IPostAssessmentRulesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

/**
 * 考核规则Service业务层处理
 *
 * @author csg
 * @date 2023-12-28
 */
@Service
public class PostAssessmentRulesServiceImpl extends ServiceImpl<PostAssessmentRulesMapper, PostAssessmentRules> implements IPostAssessmentRulesService
{
    @Autowired
    private PostAssessmentRulesMapper postAssessmentRulesMapper;

    /**
     * 查询考核规则
     *
     * @param id 考核规则主键
     * @return 考核规则
     */
    @Override
    public PostAssessmentRules selectPostAssessmentRulesById(Long id)
    {
        return postAssessmentRulesMapper.selectPostAssessmentRulesById(id);
    }

    /**
     * 查询考核规则列表
     *
     * @param postAssessmentRules 考核规则
     * @return 考核规则
     */
    @Override
    public List<PostAssessmentRules> selectPostAssessmentRulesList(PostAssessmentRules postAssessmentRules)
    {
        return postAssessmentRulesMapper.selectPostAssessmentRulesList(postAssessmentRules);
    }

    /**
     * 新增考核规则
     *
     * @param postAssessmentRules 考核规则
     * @return 结果
     */
    @Override
    public int insertPostAssessmentRules(PostAssessmentRules postAssessmentRules)
    {
        //TODO 自增主键修改为-MyBatis-Plus雪花算法(截取5位解决VUE精度丢失BUG)-系统登录日志
        if (postAssessmentRules.getId() == null)
            postAssessmentRules.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
        // postAssessmentRules.setCreateTime(DateUtils.getNowDate());
        postAssessmentRules.setCreateDefault();
        postAssessmentRules.setStatus(PostAssessmentRulesEnum.STATUS_WAIT.getValue()); // 去创建自己的枚举类
        postAssessmentRules.setCreateUserId(SecurityUtils.getUserId());
        return postAssessmentRulesMapper.insertPostAssessmentRules(postAssessmentRules);
    }

    /**
     * 修改考核规则
     *
     * @param postAssessmentRules 考核规则
     * @return 结果
     */
    @Override
    public int updatePostAssessmentRules(PostAssessmentRules postAssessmentRules)
    {
        // postAssessmentRules.setUpdateTime(DateUtils.getNowDate());
        postAssessmentRules.setUpdateDefault();
        return postAssessmentRulesMapper.updatePostAssessmentRules(postAssessmentRules);
    }

    /**
     * 批量删除考核规则
     *
     * @param ids 需要删除的考核规则主键
     * @return 结果
     */
    @Override
    public int deletePostAssessmentRulesByIds(Long[] ids)
    {
        return postAssessmentRulesMapper.deletePostAssessmentRulesByIds(ids);
    }

    /**
     * 删除考核规则信息
     *
     * @param id 考核规则主键
     * @return 结果
     */
    @Override
    public int deletePostAssessmentRulesById(Long id)
    {
        return postAssessmentRulesMapper.deletePostAssessmentRulesById(id);
    }

    /**
     * 岗检报告审核
     *
     * @param id           主键
     * @param status       状态：3-通过；4-驳回；
     * @param statusReason 未通过理由
     * @return
     */
    @Override
    public int statusApprove(Long id, Integer status, String statusReason) {
        if (id == null || status == null) {
            return 0;
        }

        //状态错误
        if (status != PostCheckedReportEnum.STATUS_PASS.getValue() && status != PostCheckedReportEnum.STATUS_BACK.getValue()) {
            return 0;
        }

        //驳回时必须填写原因
        if (status == PostCheckedReportEnum.STATUS_BACK.getValue() && StringUtils.isBlank(statusReason)) {
            return 0;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();
        PostAssessmentRules data = new PostAssessmentRules();
        data.setId(id);
        data.setStatus(status);
        data.setStatusApproveTime(new Date());
        data.setStatusReason(statusReason);
        data.setStatusByUserId(sysUser.getUserId());
        data.setStatusByUserName(sysUser.getUserName());
        data.setStatusApproveTime(DateUtils.getNowDate());
        return postAssessmentRulesMapper.updatePostAssessmentRules(data);
    }
}
