package cn.pluss.platform.controller.merchant;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.channel.MerchantAuditService;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.vo.AuditResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.concurrent.ExecutorService;

/**
 * 这里作为进件接口的入口，
 * 由于进件接口的复杂性，以及耗时操作，将进件的流程分多个接口
 *
 * @author djh
 */
@RequestMapping("/merAudit")
@RestController
public class MerchAuditController {

    @Autowired
    private MerchantAuditService ysAuditServiceV2;

    @Autowired
    private MerchantChannelStatusService mcsService;

    @Autowired
    private MerchantBaseInfoService mbiService;

    @Autowired
    private ExecutorService executorService;

    /**
     * 通用进件接口
     *
     * @param userId  用户id
     * @param channel 通道id
     * @return Result
     */
    @PostMapping
    public Result<Object> merchAudit(String userId, String channel) {
        MerchantBaseInfo mbi = mbiService.getMerchantBaseInfoByUserId(userId);
        MerchantChannelStatus mcs = mcsService.getByMerchantCode(mbi.getMerchantCode(), 4);

        if (mcs == null) {
            throw new MsgException("商户尚未发起进件");
        }

        if (!Objects.equals(mcs.getStatus(), MerchantChannelStatus.AUDIT_STATUS_EXAMINING)
            || !(Objects.equals(mcs.getThirdStatus(), MerchantChannelStatus.AUDIT_THIRD_STATUS_WAITING))) {
            throw new MsgException("当前状态不允许进件");
        }

        if ("4".equals(channel)) {
            ysAuditServiceV2.merchantAudit(userId, false);
        }
        AuditResult auditResult = new AuditResult("发起进件成功, 稍后查看进件状态", null, null);
        return ResultGenerator.genSuccessResult(auditResult);
    }

    @PostMapping("/ys/v1")
    public Result<Object> auditYs(String userId) {

        return ResultGenerator.genSuccessResult("进件成功", null);
    }

//    @PostMapping("/ys/v2/step1")
//    public Result<Object> auditYsV2Step1(String userId) {
//        ((YsAuditServiceImpl) ysAuditServiceV2).step1(userId);
//        AuditResult auditResult = new AuditResult("基础资料提交成功", "/merAudit/ys/v2/step2", "开始上传图片");
//        return ResultGenerator.genSuccessResult(auditResult);
//    }
//
//    @PostMapping("/ys/v2/step2")
//    public Result<Object> auditYsV2Step2(String userId) {
//        ((YsAuditServiceImpl) ysAuditServiceV2).step2(userId);
//        AuditResult auditResult = new AuditResult("图片上传完成", "/merAudit/ys/v2/step3", "准备进件提交");
//        return ResultGenerator.genSuccessResult(auditResult);
//    }
//
//    @PostMapping("/ys/v2/step3")
//    public Result<Object> auditYsV2Step3(String userId) {
//        ((YsAuditServiceImpl) ysAuditServiceV2).step3(userId);
//        AuditResult auditResult = new AuditResult("银盛通道进件完成", null, null);
//        return ResultGenerator.genSuccessResult(auditResult);
//    }
}
