package com.ruoyi.postCheck.service.impl;

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

import com.ruoyi.common.constant.PostCheckedProblemEnum;
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.PostCheckedProblem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostCheckedReportMapper;
import com.ruoyi.postCheck.domain.PostCheckedReport;
import com.ruoyi.postCheck.service.IPostCheckedReportService;
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 PostCheckedReportServiceImpl extends ServiceImpl<PostCheckedReportMapper, PostCheckedReport> implements IPostCheckedReportService
{
    @Autowired
    private PostCheckedReportMapper postCheckedReportMapper;

    /**
     * 查询岗检报告管理
     *
     * @param id 岗检报告管理主键
     * @return 岗检报告管理
     */
    @Override
    public PostCheckedReport selectPostCheckedReportById(Long id)
    {
        return postCheckedReportMapper.selectPostCheckedReportById(id);
    }

    /**
     * 查询岗检报告管理列表
     *
     * @param postCheckedReport 岗检报告管理
     * @return 岗检报告管理
     */
    @Override
    public List<PostCheckedReport> selectPostCheckedReportList(PostCheckedReport postCheckedReport)
    {
        return postCheckedReportMapper.selectPostCheckedReportList(postCheckedReport);
    }

    /**
     * 新增岗检报告管理
     *
     * @param postCheckedReport 岗检报告管理
     * @return 结果
     */
    @Override
    public int insertPostCheckedReport(PostCheckedReport postCheckedReport)
    {
        //TODO 自增主键修改为-MyBatis-Plus雪花算法(截取5位解决VUE精度丢失BUG)-系统登录日志
        if (postCheckedReport.getId() == null)
            postCheckedReport.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
        // postCheckedReport.setCreateTime(DateUtils.getNowDate());
        postCheckedReport.setCreateDefault();
        postCheckedReport.setStatus(PostCheckedReportEnum.STATUS_WAIT.getValue()); // 去创建自己的枚举类
        postCheckedReport.setCreateUserId(SecurityUtils.getUserId());

        postCheckedReport.setReportNo("BG-"+DateUtils.dateTimeNow());
        return postCheckedReportMapper.insertPostCheckedReport(postCheckedReport);
    }

    /**
     * 修改岗检报告管理
     *
     * @param postCheckedReport 岗检报告管理
     * @return 结果
     */
    @Override
    public int updatePostCheckedReport(PostCheckedReport postCheckedReport)
    {
        // postCheckedReport.setUpdateTime(DateUtils.getNowDate());
        postCheckedReport.setUpdateDefault();
        return postCheckedReportMapper.updatePostCheckedReport(postCheckedReport);
    }

    /**
     * 批量删除岗检报告管理
     *
     * @param ids 需要删除的岗检报告管理主键
     * @return 结果
     */
    @Override
    public int deletePostCheckedReportByIds(Long[] ids)
    {
        return postCheckedReportMapper.deletePostCheckedReportByIds(ids);
    }

    /**
     * 删除岗检报告管理信息
     *
     * @param id 岗检报告管理主键
     * @return 结果
     */
    @Override
    public int deletePostCheckedReportById(Long id)
    {
        return postCheckedReportMapper.deletePostCheckedReportById(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();
        PostCheckedReport data = new PostCheckedReport();
        data.setId(id);
        data.setStatus(status);
        data.setStatusByUserId(sysUser.getUserId());
        data.setStatusByUserName(sysUser.getUserName());
        data.setStatusApproveTime(new Date());
        data.setStatusReason(statusReason);
        return postCheckedReportMapper.updatePostCheckedReport(data);
    }
}
