//package com.mortals.xhx.module.task.service.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.mortals.framework.exception.AppException;
//import com.mortals.framework.model.Context;
//import com.mortals.framework.model.PageInfo;
//import com.mortals.framework.model.Result;
//import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
//import com.mortals.xhx.base.framework.config.InterceptorConfig;
//import com.mortals.xhx.base.system.user.model.UserEntity;
//import com.mortals.xhx.base.system.user.model.UserQuery;
//import com.mortals.xhx.base.system.user.service.UserService;
//import com.mortals.xhx.common.code.FlowStatusEnum;
//import com.mortals.xhx.common.code.YesNoEnum;
//import com.mortals.xhx.common.pdu.api.Page;
//import com.mortals.xhx.common.pdu.flow.*;
//import com.mortals.xhx.common.utils.BeanUtil;
//import com.mortals.xhx.feign.flowable.IApiFlowInstanceFeign;
//import com.mortals.xhx.feign.flowable.IApiFlowTaskFeign;
//
//import com.mortals.xhx.module.deploy.model.DeployFormQuery;
//import com.mortals.xhx.module.deploy.service.DeployFormService;
//
//import com.mortals.xhx.module.task.dao.TaskProcessDao;
//import com.mortals.xhx.module.task.model.TaskProcessEntity;
//import com.mortals.xhx.module.task.model.TaskProcessQuery;
//import com.mortals.xhx.module.task.service.TaskProcessService;
//import lombok.extern.apachecommons.CommonsLog;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.ObjectUtils;
//
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//
//@Service("taskProcessService")
//@CommonsLog
//public class TaskProcessServiceImpl extends AbstractCRUDServiceImpl<TaskProcessDao, TaskProcessEntity, Long> implements TaskProcessService {
//
//    @Autowired
//    private IApiFlowTaskFeign apiFlowTaskFeign;
//
//    @Autowired
//    private IApiFlowInstanceFeign iApiFlowInstanceFeign;
//    @Autowired
//    private DeployFormService deployFormService;
//
//    @Autowired
//    private UserService userService;
//    @Autowired
//    private MemberService memberService;
//
//    @Autowired
//    private InterceptorConfig config;
//
//    @Override
//    public Result<TaskProcessEntity> getProcessList(TaskProcessQuery query, PageInfo pageInfo, Context context) {
//
//        Result<TaskProcessEntity> result = new Result<>();
//        FlowTaskPdu flowTaskPdu = new FlowTaskPdu();
//        flowTaskPdu.setUserCode(context.getUser().getLoginName());
//        try {
//            if (!ObjectUtils.isEmpty(query)) {
//                BeanUtils.copyProperties(query, flowTaskPdu, BeanUtil.getNullPropertyNames(query));
//            }
//
//            BaseFlowReq<BaseQuery> baseQueryReq = new BaseFlowReq<>();
//            Page page = new Page();
//            page.setPer(pageInfo.getPrePageResult());
//            page.setSize(pageInfo.getCurrPage());
//            BaseQuery baseQuery = new BaseQuery();
//            baseQuery.setPlatformSn(config.getTenantId());
//
//            //设置微信用户查询
//            baseQuery.setUserCode(context.getUser().getLoginName());
//            if (!ObjectUtils.isEmpty(query.getOpenId())) {
//                baseQuery.setUserCode(memberService.selectOne(new MemberQuery().openId(query.getOpenId()), null).getId().toString());
//            }
//            baseQueryReq.setPage(page);
//            baseQueryReq.setQuery(baseQuery);
//
//
//            ApiResp<TaskRsp> resp = apiFlowTaskFeign.myProcess(baseQueryReq);
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("获取流程定义列表异常：" + resp.getMsg());
//                throw new AppException("获取流程定义列表异常：" + resp.getMsg());
//            }
//
//            if (!ObjectUtils.isEmpty(resp.getData())) {
//                List<TaskProcessEntity> collect = resp.getData().getList().stream().map(item -> {
//                    TaskProcessEntity taskProcessEntity = new TaskProcessEntity();
//                    BeanUtils.copyProperties(item, taskProcessEntity, BeanUtil.getNullPropertyNames(item));
//                    taskProcessEntity.setProcDefVersion("v" + item.getProcDefVersion());
//                    if (ObjectUtils.isEmpty(item.getFinishTime())) {
//                        taskProcessEntity.setFlowStatus(FlowStatusEnum.FLOW_RUNNING.getValue());
//                    } else {
//                        taskProcessEntity.setFlowStatus(FlowStatusEnum.FLOW_FINISH.getValue());
//                    }
//                    return taskProcessEntity;
//                }).collect(Collectors.toList());
//                result.setList(collect);
//
//                Page curPage = resp.getData().getPage();
//                pageInfo.setTotalResult(curPage.getTotal());
//                result.setPageInfo(pageInfo);
//            } else {
//                result.setList(Collections.emptyList());
//            }
//        } catch (Exception e) {
//            throw new AppException("获取流程定义列表异常：" + e.getMessage());
//        }
//
//        return result;
//    }
//
//    @Override
//    public List<FlowViewerPdu> getFlowViewer(String procInsId) {
//
//        List<FlowViewerPdu> flowViewerList = null;
//        try {
//            ProcessCommonReq processCommonReq = new ProcessCommonReq();
//            processCommonReq.setProcessInstanceId(procInsId);
//
//            ApiResp<List<FlowViewerPdu>> resp = iApiFlowInstanceFeign.getFlowViewer(processCommonReq);
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                throw new AppException("获取流程列表异常：" + resp.getMsg());
//            }
//            flowViewerList = resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e);
//        }
//
//        return flowViewerList;
//    }
//
//    @Override
//    public Map<String, Object> getProcessVariables(String taskId) {
//        try {
//            log.error("获取流程变量 taskId：" + taskId);
//            CommonTaskReq commonTaskReq = new CommonTaskReq();
//            commonTaskReq.setTaskId(taskId);
//            ApiResp<Map<String, Object>> resp = apiFlowTaskFeign.processVariables(commonTaskReq);
//            log.info("获取流程变量 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("获取流程变量异常：" + resp.getMsg());
//                throw new AppException("获取流程变量异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException("获取流程变量异常：" + e.getMessage());
//        }
//
//    }
//
//    @Override
//    public FlowNextPdu getNextFlowNode(String data, Context context) {
//        try {
//            FlowTaskVoPdu flowTaskVoPdu = JSONObject.parseObject(data, FlowTaskVoPdu.class);
//
//            CommonTaskReq commonTaskReq = new CommonTaskReq();
//            commonTaskReq.setTaskId(flowTaskVoPdu.getTaskId());
//
//            ApiResp<FlowNextPdu> resp = apiFlowTaskFeign.nextFlowNode(commonTaskReq);
//
//            log.info("获取下一节点 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("获取下一节点异常：" + resp.getMsg());
//                throw new AppException("获取下一节点异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException("获取下一节点异常：" + e.getMessage());
//        }
//    }
//
//    @Override
//    public Result<FlowTaskPdu> getFinishList(TaskProcessQuery query, PageInfo pageInfo, Context context) {
//        return null;
//    }
//
//    @Override
//    public Map<String, Object> flowRecord(String procInsId, String deployId, Context context) {
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//
//            BaseFlowReq<BaseQuery> baseQueryReq = new BaseFlowReq<>();
//            Page page = new Page();
//            page.setPer(-1);
//            page.setSize(999);
//            BaseQuery baseQuery = new BaseQuery();
//            baseQuery.setPlatformSn(config.getTenantId());
//            baseQuery.setProcessInstanceId(procInsId);
//            baseQuery.setDeployId(deployId);
//            baseQuery.setUserCode(context.getUser().getLoginName());
//            baseQueryReq.setPage(page);
//            baseQueryReq.setQuery(baseQuery);
//
//            ApiResp<TaskRsp> resp = apiFlowTaskFeign.flowRecord(baseQueryReq);
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("获取办理列表异常：" + resp.getMsg());
//                throw new AppException("获取办理列表异常：" + resp.getMsg());
//            }
//
//            if (!ObjectUtils.isEmpty(resp.getData())) {
//
//                resp.getData().getList().stream().peek(item -> {
//                    UserQuery userQuery = new UserQuery();
//                    userQuery.setLoginName(item.getAssigneeId());
//                    UserEntity userEntity = userService.selectOne(userQuery, context);
//                    item.setAssigneeName(userEntity.getRealName());
//
//                }).count();
//
//                map.put("flowList", resp.getData());
//            }
//            // 第一次申请获取初始化表单
//            if (!ObjectUtils.isEmpty(deployId)) {
//                DeployFormQuery deployFormQuery = new DeployFormQuery();
//                deployFormQuery.setDeployId(deployId);
//                FormEntity formEntity = deployFormService.find(deployFormQuery, context).stream().findFirst().map(item -> formService.get(item.getFormId(), context)).orElseGet(() -> null);
//                if (ObjectUtils.isEmpty(formEntity)) {
//                    throw new AppException("请先配置流程表单");
//                }
//                map.put("formData", JSONObject.parseObject(formEntity.getFormContent()));
//            }
//
//            return map;
//        } catch (Exception e) {
//            throw new AppException("获取办理列表异常：" + e.getMessage());
//        }
//
//    }
//
//    @Override
//    public String stopProcess(ProcessStopReq processStopReq, Context context) {
//        try {
//            processStopReq.setUserCode(context.getUser().getLoginName());
//
//            ApiResp<String> resp = iApiFlowInstanceFeign.stopProcess(processStopReq);
//            log.info("取消申请 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("取消申请异常：" + resp.getMsg());
//                throw new AppException("取消申请异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String complete(CompleteTaskReq completeTaskReq, Context context) {
//        try {
//            completeTaskReq.setUserCode(context.getUser().getLoginName());
//
//            ApiResp<String> resp = apiFlowTaskFeign.complete(completeTaskReq);
//            log.info("审核任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("审核任务异常：" + resp.getMsg());
//                throw new AppException("审核任务异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String reject(RejectTaskReq rejectTaskReq, Context context) {
//        try {
//            rejectTaskReq.setUserCode(context.getUser().getLoginName());
//            ApiResp<String> resp = apiFlowTaskFeign.reject(rejectTaskReq);
//            log.info("拒绝任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("拒绝任务异常：" + resp.getMsg());
//                throw new AppException("拒绝异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String taskReturn(ReturnTaskReq returnTaskReq, Context context) {
//        try {
//            returnTaskReq.setUserCode(context.getUser().getLoginName());
//            ApiResp<String> resp = apiFlowTaskFeign.taskReturn(returnTaskReq);
//            log.info("回退任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("回退任务异常：" + resp.getMsg());
//                throw new AppException("回退任务异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String addSignTask(AddSignTaskReq req, Context context) {
//        try {
//            req.setUserCode(context.getUser().getLoginName());
//            ApiResp<String> resp = apiFlowTaskFeign.addSignTask(req);
//            log.info("加签任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("加签任务异常：" + resp.getMsg());
//                throw new AppException(resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String delete(ProcessDeleteReq processDeleteReq, Context context) {
//        try {
//            processDeleteReq.setUserCode(context.getUser().getLoginName());
//            ApiResp<String> resp = iApiFlowInstanceFeign.deleteProcess(processDeleteReq);
//
//            log.info("删除实例 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("删除实例异常：" + resp.getMsg());
//                throw new AppException("删除实例：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String claim(FlowTaskVoPdu flowTaskVoPdu, Context context) {
//        try {
//            //todo
//            //ApiRespPdu<String> resp = apiFlowTaskFeign.claim(flowTaskVoPdu);
//            ApiResp<String> resp = null;
//            log.info("签收任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("签收任务异常：" + resp.getMsg());
//                throw new AppException("签收异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String unClaim(FlowTaskVoPdu flowTaskVoPdu, Context context) {
//        try {
//            // ApiRespPdu<String> resp = apiFlowTaskFeign.unClaim(flowTaskVoPdu);
//            //todo
//            ApiResp<String> resp = null;
//            log.info("拒绝签收任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("拒绝签收任务异常：" + resp.getMsg());
//                throw new AppException("拒绝签收异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String delegateTask(DelegateTaskReq delegateTaskReq, Context context) {
//        try {
//            delegateTaskReq.setUserCode(context.getUser().getLoginName());
//            ApiResp<String> resp = apiFlowTaskFeign.delegateTask(delegateTaskReq);
//            log.info("委派任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("委派任务异常：" + resp.getMsg());
//                throw new AppException("委派异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String assignTask(TurnTaskReq turnTaskReq, Context context) {
//        try {
//            turnTaskReq.setUserCode(context.getUser().getLoginName());
//            ApiResp<String> resp = apiFlowTaskFeign.assignTask(turnTaskReq);
//            log.info("转办任务 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("转办任务异常：" + resp.getMsg());
//                throw new AppException("转办异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public List<FlowUserTaskPdu> getReturnList(CommonTaskReq commonTaskReq, Context context) {
//        try {
//            commonTaskReq.setUserCode(context.getUser().getLoginName());
//            ApiResp<List<FlowUserTaskPdu>> resp = apiFlowTaskFeign.findReturnTaskList(commonTaskReq);
//            log.info("获取所有可回退的节点 resp：" + JSONObject.toJSONString(resp));
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("获取所有可回退的节点异常：" + resp.getMsg());
//                throw new AppException("拒获取所有可回退的节点异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    @Override
//    public String processReadImage(ProcessCommonReq processCommonReq, Context context) {
//        try {
//            ApiResp<String> resp = iApiFlowInstanceFeign.processReadImage(processCommonReq);
//            if (YesNoEnum.NO.getValue() == resp.getCode()) {
//                log.error("获取图片异常：" + resp.getMsg());
//                throw new AppException("获取图片异常：" + resp.getMsg());
//            }
//            return resp.getData();
//        } catch (Exception e) {
//            throw new AppException(e.getMessage());
//        }
//    }
//}