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.mq.IReportMessageService;

import com.tofly.gpsboot.service.paln.month.PlanmouthService;
import com.tofly.gpsboot.service.report.IReportMonthPlanServer;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.comm.Audit;
import com.tofly.pojo.plan.Planmouth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


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

/**
 * 月计划上报
 * @author Zev
 */
@Service("REPORT_TYPE_MONTHLY_PLAN")
public class ReportMonthPlanServerImpl implements IReportMonthPlanServer {

    @Autowired
    private PlanmouthService planmouthService;

    @Autowired
    private IReportMessageService iReportMessageService;
    @Autowired
    private IAuditService iAuditService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void report(ReportAuditDto dto) {
        Long id = dto.getId();
        Planmouth info = planmouthService.getById(id);
        String isSubmit = info.getIssubmit();
        String planState = info.getPlanState();
        // 上报条件 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());
            planmouthService.updateById(info);
            // 清记录->落库
            iAuditService.remove(new QueryWrapper<Audit>().lambda().eq(Audit::getAuditPlanId,id).eq(Audit::getAuditType,AuditTypeEnum.AUDIT_TYPE_MONTHLY_PLAN.getCode()));
            auditList.forEach(t -> {
                t.setAuditPlanId(id);
                t.setAuditTime(date);
                t.setAuditType(AuditTypeEnum.AUDIT_TYPE_MONTHLY_PLAN.getCode());
                iAuditService.save(t);
            });
            // 通知审核对象mq消息
            iReportMessageService.reportTypeMonthlyPlan(dto, audit.getAuditUser());
        }else {
            throw new BusinessException(Constant.REPORT_NOT_UNCOMMITTED_CODE,Constant.REPORT_NOT_UNCOMMITTED);
        }



    }
}
