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.MerchantChannelStatus;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.ys.impl.v20210929.YsAuditServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author djh
 */
@Controller
@RequestMapping("/channelStatus")
public class MerchantChannelStatusController {

    @Autowired
    private MerchantChannelStatusService mcsService;

    @Autowired
    private MerchantAuditService ysAuditServiceV2;

    /**
     * 清理进件驳回信息中的通道数据
     *
     * @return
     */
    @PostMapping("/clearRejectedChannel")
    @ResponseBody
    public Result<Object> clearRejectedChannel(String merchantCode) {
        mcsService.clearRejectedChannel(merchantCode);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    /**
     * 发送签约短信
     *
     */
    @PostMapping("/sendSignMsg")
    @ResponseBody
    public Result<Object> sendSignMsg(String merchantCode) {
        mcsService.sendSignMsg(merchantCode);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    /**
     * 开通D0
     */
    @PostMapping("/openOnlinePay")
    @ResponseBody
    public Result<Object> openOnlinePay(String merchantCode) {
        MerchantChannelStatus mcs = mcsService.getByMerchantCode(merchantCode, 4);
        ((YsAuditServiceImpl) ysAuditServiceV2).openOnlinePay(mcs);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @PostMapping("/createNewAuditData")
    @ResponseBody
    public Result<Object> createNewAuditData(String merchantCode) {
        LambdaQueryWrapper<MerchantChannelStatus> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(MerchantChannelStatus::getMerchantCode, merchantCode)
                .eq(MerchantChannelStatus::getStatus, MerchantChannelStatus.AUDIT_STATUS_EXAMINING)
                .eq(MerchantChannelStatus::getThirdStatus, MerchantChannelStatus.AUDIT_THIRD_STATUS_WAITING);

        MerchantChannelStatus one = mcsService.getOne(qWrapper);
        MsgException.checkNonNull(one, "当前已存在待审核信息");

        LambdaQueryWrapper<MerchantChannelStatus> qWrapper2 = Wrappers.lambdaQuery();
        qWrapper2.eq(MerchantChannelStatus::getMerchantCode, merchantCode)
                .eq(MerchantChannelStatus::getChannel, 1)
                .ne(MerchantChannelStatus::getStatus, 3);

        one = mcsService.getOne(qWrapper2);
        MsgException.checkNonNull(one, "当前D1通道存在未完成进件数据");

        qWrapper.clear();
        qWrapper.eq(MerchantChannelStatus::getMerchantCode, merchantCode)
                .ne(MerchantChannelStatus::getStatus, MerchantChannelStatus.AUDIT_STATUS_EXAMINING);

        int auditSize = mcsService.count(qWrapper);

        MsgException.check(auditSize >= 2, "当前已进件两个通道");

        MerchantChannelStatus mcs = new MerchantChannelStatus();
        mcs.setMerchantCode(merchantCode);
        mcs.setStatus(MerchantChannelStatus.AUDIT_STATUS_EXAMINING);
        mcs.setThirdStatus(MerchantChannelStatus.AUDIT_THIRD_STATUS_WAITING);
        mcsService.save(mcs);

        return ResultGenerator.genSuccessResult("进件申请已生成");
    }
}
