package com.jeeplus.test.centerservice.service;

import cn.hutool.core.util.ObjectUtil;
import com.jeeplus.centerservice.enums.TaskAliasEnum;
import com.jeeplus.flowable.service.FlowTaskService;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.centerservice.dto.TranspondDTO;
import com.jeeplus.test.cw.common.CommonApi;
import com.jeeplus.test.reimbursement.reimbursementInfo.service.ReimbursementInfoService;
import com.jeeplus.test.reimbursement.reimbursementInfo.service.dto.RetureListDto;
import com.jeeplus.test.reimbursement.reimbursementInfo.service.dto.SaveInfoDto;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class TranspondService {

    @Resource
    private ReimbursementInfoService reimbursementInfoService; // 评估-报销service

    @Resource
    private TaskService taskService;

    @Resource
    private FlowTaskService flowTaskService;

    @Autowired
    private CommonApi commonApi;

    public Object detailDistribute(String id, String processDefKey) {
        TaskAliasEnum taskAliasEnum = TaskAliasEnum.getByCcpmContains(processDefKey);
        // 评估-报销
        if (TaskAliasEnum.REIMBURSEMENT.getCpaTaskAlias().equals(taskAliasEnum.getCpaTaskAlias())) {
            SaveInfoDto byId = reimbursementInfoService.findById(id);
            return byId;
        }
        return null;
    }

    public Map<String,Object> auditDistribute(TranspondDTO transpondDTO) {
        Map<String,Object> result = new HashMap<>();
        result.put("success", null);
        if (Objects.nonNull(transpondDTO) && StringUtils.isNotBlank(transpondDTO.getId()) && StringUtils.isNotBlank(transpondDTO.getFlag()) && StringUtils.isNotBlank(transpondDTO.getProcessDefKey())) {
            String id = transpondDTO.getId(); // 报销数据id
            String flag = transpondDTO.getFlag(); // 审核标记  yes通过  no驳回
            TaskAliasEnum taskAliasEnum = TaskAliasEnum.getByCcpmContains(transpondDTO.getProcessDefKey()); // 获取流程key枚举
            if (ObjectUtil.isNotEmpty(taskAliasEnum)) {
                if (TaskAliasEnum.REIMBURSEMENT.getCpaTaskAlias().equals(taskAliasEnum.getCpaTaskAlias())) { // 评估-报销流程
                    SaveInfoDto reim = reimbursementInfoService.findById(id);
                    if (Objects.nonNull(reim) && StringUtils.isNotBlank(reim.getType()) && "2".equals(reim.getType())) { // 确保当时数据处于“待审核”状态
                        if ( StringUtils.isNotBlank(reim.getProcInsId())) {
                            try {
                                RetureListDto upStatusParam = new RetureListDto();
                                upStatusParam.setId(id);
                                if ("yes".equals(flag)) {
                                    String taskName = commonApi.getTaskName(reim.getProcInsId());
                                    if (StringUtils.isNotBlank(taskName) && "公司领导审批".equals(taskName)) {
                                        // “公司领导审批” 节点审核通过时，才会将审核状态改为 “审核通过”
                                        upStatusParam.setType("5"); // 审核状态设置为审核完成
                                    } else {
                                        upStatusParam.setType("2");
                                    }
                                } else {
                                    upStatusParam.setType("4"); // 审核状态设置为审核驳回
                                }
                                reimbursementInfoService.updateStatusById(upStatusParam); // 修改数据的审核状态
                                flowTaskService.auditByProcInsIdAndFlag(reim.getProcInsId(), flag, null, transpondDTO.getComment()); // 审核操作
                                result.put("success", "false");
                                result.put("message", "操作成功");
                            }catch (Exception e) {
                                e.printStackTrace();
                                result.put("success", "false");
                                result.put("message", "操作失败");
                            }
                        }
                    } else {
                        result.put("success", "false");
                        result.put("message", "操作失败，数据已发生改变或不存在，请刷新数据");
                    }
                }
            }
        }
        if (ObjectUtil.isEmpty(result.get("success"))) {
            result.put("success", "false");
            result.put("message", "操作失败");
        }
        return result;
    }
}
