package com.tuoheng.admin.service.report.generate;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.conver.ReportConverMapper;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.Report;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.report.GenerateReportCodeEnum;
import com.tuoheng.admin.mapper.InspectionFileHandleMapper;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.ReportMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

/**
 * 任务完成自动生成报告业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-09
 */
@Slf4j
@Service
public class GenerateReportByAutoService {

    @Autowired
    private ReportMapper reportMapper;

    /**
     * 生成报告
     *
     * @return
     */
    public JsonResult generate(Inspection inspection) {
        log.info("进入任务完成自动生成报告, inspectionId={}", inspection.getId());
        Report report = this.buildReport(inspection);

        JsonResult result = this.addReport(report);
        if (0 != result.getCode()) {
            log.info("自动生成报告业务：添加报告记录失败：{}", result.getMsg());
            throw new ServiceException("自动生成报告业务：添加报告记录失败");
        }
        return JsonResult.success();
    }

    /**
     *
     * 生成报告实体
     *
     * @param inspection
     */
    private Report buildReport(Inspection inspection) {
        Report report = ReportConverMapper.INSTANCE.fromInspectionToReport(inspection);
        // 任务编号：XJBG+日期+3位排序数字
        String code = DateUtils.generateCode("XJBG");
        report.setId("");
        report.setReportCode(code);
        report.setTenantId(inspection.getTenantId());
        report.setInspectionId(inspection.getId());
        report.setInspectionCode(inspection.getCode());
        report.setInspectionName(inspection.getName());
        report.setAutoGenerate(2);
        report.setCreateUser("");
        report.setCreateTime(DateUtils.now());
        return report;
    }

    /**
     *
     * 添加报告
     *  每次生成报告，将上一次生成的报告给覆盖，因此直接将上次的删除，重新添加一条
     *
     * @param report
     */
    private JsonResult addReport(Report report) {
        Report reportTmp = reportMapper.selectOne(Wrappers.<Report>lambdaQuery()
                            .eq(Report::getInspectionId, report.getInspectionId())
                            .eq(Report::getMark, MarkEnum.VALID.getCode()));
        if (null != reportTmp) {
            reportMapper.deleteById(reportTmp.getId());
        }
        Integer rowCount = reportMapper.insert(report);
        if (rowCount <= 0) {
            log.info("自动生成报告业务：添加报告记录失败");
            return JsonResult.error(GenerateReportCodeEnum.GENERATE_IS_FAILED.getCode(), GenerateReportCodeEnum.GENERATE_IS_FAILED.getMsg());
        }
        return JsonResult.success();
    }
}
