package com.tofly.gpsboot.service.report.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.AuditSateEnum;
import com.tofly.gpsboot.common.system.AuditTypeEnum;
import com.tofly.gpsboot.common.system.ReportSateEnum;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.comm.IAuditService;
import com.tofly.gpsboot.service.maintain.RepairplanService;
import com.tofly.gpsboot.service.maintain.RepairplandetailService;
import com.tofly.gpsboot.service.mq.IReportMessageService;

import com.tofly.gpsboot.service.report.IReportRepairMonthPlanServer;

import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.comm.Audit;
import com.tofly.pojo.maintain.Repairplan;


import com.tofly.pojo.maintain.Repairplandetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 抢修月计划上报
 *
 * @author Zev
 */
@Service("REPORT_REPAIR_MONTH_PLAN")
public class ReportRepairMonthPlanServerImpl implements IReportRepairMonthPlanServer {

    @Autowired
    private RepairplanService repairplanService;

    @Autowired
    private IReportMessageService iReportMessageService;

    @Autowired
    private RepairplandetailService repairplandetailService;

    @Autowired
    private IAuditService iAuditService;

    @Override
    public void report(ReportAuditDto dto) throws BusinessException {
        Long id = dto.getId();
        Repairplan info = repairplanService.getById(id);
        String planState = info.getPlanState();
        String isSubmit = info.getIssubmit();
        // 判断当前上报对象的详情是否存在详情上报内容,若为空则不允许上报
        List<Repairplandetail> list = repairplandetailService.list(new QueryWrapper<Repairplandetail>().lambda().eq(Repairplandetail::getYearId, info.getId()));
        if (null == list || list.size()==0) {
            throw new BusinessException(Constant.PLAN_REPAIR_MONTH_DETAIL_NULL_CODE, Constant.PLAN_REPAIR_MONTH_DETAIL_NULL);
        }
        // 上报条件 1.当前对象未上报
        // 2.当前对象已上报 审核状态为不通过
        if (ReportSateEnum.REPORT_UNCOMMITTED.getCode().equals(isSubmit) || AuditSateEnum.AUDIT_NO.getCode().equals(planState)) {
            List<Audit> auditList = JSONArray.parseArray(JSON.toJSONString(dto.getAuditDtoList()), Audit.class);
            // 审批链处理
            List<Audit> collect = auditList.stream().filter(a -> 0 == a.getAuditLevel()).collect(Collectors.toList());
            Audit audit = collect.get(0);
            // (获取一级审批的审批人默认为待审批)
            audit.setAuditState(AuditSateEnum.AUDIT_UNCOMMITTED.getCode());
            // 修改当前上报状态为 已上报,上报时间,修改审核状态为 未审批
            Date date = DateTimeUtil.convertLDTToDate(LocalDateTime.now());
            info.setSubmitTime(date);
            info.setSubmitUser(SecurityUtils.getUserId());
            info.setIssubmit(ReportSateEnum.REPORT_SUBMITTED.getCode());
            info.setPlanState(AuditSateEnum.AUDIT_UNCOMMITTED.getCode());
            repairplanService.updateById(info);
            // 清记录->落库
            iAuditService.remove(new QueryWrapper<Audit>().lambda().eq(Audit::getAuditPlanId, id).eq(Audit::getAuditType,AuditTypeEnum.AUDIT_TYPE_REPAIR_MONTH_PLAN.getCode()));
            auditList.forEach(t -> {
                t.setAuditPlanId(id);
                t.setAuditTime(date);
                t.setAuditType(AuditTypeEnum.AUDIT_TYPE_REPAIR_MONTH_PLAN.getCode());
                iAuditService.save(t);
            });
            // 通知审核对象mq消息
            iReportMessageService.reportRepairMonthPlan(dto, audit.getAuditUser());
        } else {
            throw new BusinessException(Constant.REPORT_NOT_UNCOMMITTED_CODE, Constant.REPORT_NOT_UNCOMMITTED);

        }

    }
}
