package com.mortals.xhx.busiz;

import com.alibaba.fastjson.JSON;
import com.mortals.xhx.base.framework.config.InterceptorConfig;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.busiz.message.FlowMessageReq;
import com.mortals.xhx.busiz.onethinglist.OneThineInfo;
import com.mortals.xhx.common.code.ApiRespCodeEnum;
import com.mortals.xhx.common.code.ApproveStateEnum;
import com.mortals.xhx.common.code.FlowStatusNotifyType;
import com.mortals.xhx.feign.flowable.IApiFlowInstanceFeign;
import com.mortals.xhx.feign.rsp.ApiResp;
import com.mortals.xhx.module.member.service.MemberService;
import com.mortals.xhx.module.yth.model.YthOnethingEntity;
import com.mortals.xhx.module.yth.service.YthOnethingService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 流程回调接口
 *
 * @author: zxfei
 * @date: 2021/8/24 20:28
 */
@RestController
@CommonsLog
@RequestMapping("/api/flow/")
public class FlowCallbackApiController {

    @Autowired
    private YthOnethingService onethingService;

    @Autowired
    private ParamService paramService;
    @Autowired
    private IApiFlowInstanceFeign apiFlowInstanceFeign;
    @Autowired
    private MemberService memberService;
    @Autowired
    private InterceptorConfig config;
    @Autowired
    private UploadService uploadService;

    /**
     * 流程消息回调
     *
     * @param req
     * @return
     */
    @PostMapping("message/callback")
    public ApiResp<List<OneThineInfo>> FlowMessageBack(@RequestBody FlowMessageReq req) {
        log.info("receive callback :"+JSON.toJSONString(req));

        ApiResp<List<OneThineInfo>> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        try {
            if(FlowStatusNotifyType.FLOW_START.getValue()==req.getType()){

            }else if(FlowStatusNotifyType.FLOW_END.getValue()==req.getType()){
                //更新业务流程审核状态
                String businessKey = req.getBusinessKey();

                YthOnethingEntity ythOnethingEntity = onethingService.get(Long.parseLong(businessKey));
                ythOnethingEntity.setStatus(ApproveStateEnum.AUDIT_FIN.getValue());
                ythOnethingEntity.setUpdateTime(new Date());
                ythOnethingEntity.setUpdateUser("system");
                onethingService.update(ythOnethingEntity);
            }else if(FlowStatusNotifyType.TASK_CREATE.getValue()==req.getType()){

            }else if(FlowStatusNotifyType.TASK_COMPLETE.getValue()==req.getType()){

            }



            log.info("接收通知消息:"+ JSON.toJSONString(req));

        } catch (Exception e) {
            log.error("消息接收失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return rsp;
        }

        return rsp;
    }


}
