package com.jeeplus.centerservice.service.ccpm;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jeeplus.centerservice.enums.TaskAliasEnum;
import com.jeeplus.centerservice.service.ccpm.flow.FlowRequest;
import com.jeeplus.centerservice.utils.ConvertServiceUtil;
import com.jeeplus.centerservice.utils.GenerateFormUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

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

    @Resource
    private CcpmRequest ccpmRequest;

    @Resource
    private FlowRequest flowRequest;

    /**
     * 查询待办详情
     * @param id 详情id
     * @param processDefKey  流程key：例如 13、102...
     * @return
     */
    public Map<String, Object> getByIdGenerate(String id, String processDefKey) {
        LinkedHashMap<String, Object> reqMap = null;
        Map<String, Object> respMap = null;
        if ("13".equals(processDefKey) ||"102".equals(processDefKey) ||"108".equals(processDefKey) || "109".equals(processDefKey)){
            Object response = ccpmRequest.getByIdGenerate(id, processDefKey); // 报销详情
            reqMap = JSONObject.parseObject(JSON.toJSONString(response), LinkedHashMap.class, Feature.OrderedField);
            respMap = GenerateFormUtil.convertToForm(reqMap);
        }else { //发票详情
            Object response = ccpmRequest.getInvoiceByIdGenerate(id, processDefKey);
            String jsonString = JSON.toJSONString(response, SerializerFeature.WriteMapNullValue);
            reqMap = JSONObject.parseObject(jsonString, LinkedHashMap.class, Feature.OrderedField);
//            respMap = GenerateFormUtil.convertToForm(reqMap);
            respMap = reqMap;
        }
        if (ObjectUtil.isNotEmpty(respMap) && ObjectUtil.isNotEmpty(respMap.get("processInstanceId"))) {
            String processInstanceId = respMap.get("processInstanceId").toString();
            // 流程流向图
            Object flowChart = flowRequest.getFlowChart(processInstanceId);
            if (Objects.nonNull(flowChart)) {
                List<Map<String, Object>> flowCharts = (List)JSONArray.parseArray(JSON.toJSONString(flowChart)); // 格式转换
                respMap.put("flowChart",flowCharts);
            }
            // 流程历史
            Object histoicFlow = flowRequest.getHistoicFlowList(processInstanceId);
            if (Objects.nonNull(histoicFlow)) {
                List<Map<String, Object>> histoicFlows = (List)JSONArray.parseArray(JSON.toJSONString(histoicFlow));
                List<Map<String, Object>> histoicFlowList = ConvertServiceUtil.convertHisFlowList(histoicFlows); // 格式转换
                respMap.put("histoicFlow",histoicFlowList);
            }
        }

        return respMap;
    }

    /**
     * 报销审核通过、驳回
     * @param id
     * @param flag
     * @return
     */
    public Map<String, Object> reimAudit(String id, String flag, String comment, String processDefKey) {
        Map<String, Object> resultMap = new HashMap<>();
        Object response = ccpmRequest.getById(id, processDefKey); // 报销详情
        if (Objects.nonNull(response)) {
            Map<String, Object> resp = JSONObject.parseObject(JSON.toJSONString(response));
            Object result = ccpmRequest.saveAuditFunc(resp, flag, comment, processDefKey); // 审核通过、审核驳回
            if (Objects.nonNull(result)) {
                resultMap = JSONObject.parseObject(JSON.toJSONString(result));
            } else {
                resultMap.put("success", false);
                resultMap.put("message","操作失败");
            }
        } else {
            resultMap.put("success", false);
            resultMap.put("message","操作失败");
        }
        return resultMap;

    }

    /**
     * 删除报销待办
     * @param id
     * @return
     */
    public Map<String, Object> deleteReim(String id) {
        Map<String, Object> resultMap = new HashMap<>();
        Object result = flowRequest.deleteNotify(id, TaskAliasEnum.REIMBURSEMENT.getCcpmTaskAlias()); // 13代表报销模块
        if (Objects.nonNull(result)) {
            resultMap.putAll(JSONObject.parseObject(JSON.toJSONString(result)));
        } else {
            resultMap.put("code", 0);
            resultMap.put("msg", "报销信息删除失败");
        }
        return resultMap;
    }
}
