package cn.pluss.platform.controller.jft;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.JftMercPaymentChannelService;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.user.impl.GeneralPushUtil;
import cn.pluss.platform.yinsheng.v20210929.ReqMethod;
import cn.pluss.platform.ys.impl.v20210929.YsConfigV2;
import cn.pluss.platform.ysExtension.v20220615.YsServiceV2;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;

/**
 * <p>
 * 缴费通进件信息 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2022-02-22
 */
@Controller
@RequestMapping("/jftPaymentChannel")
public class JftMercPaymentChannelController extends BaseModelController<JftMercPaymentChannelService, JftMercPaymentChannel> {

    @Autowired
    private YsServiceV2 ysServiceV2;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private GeneralPushUtil generalPushUtil;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index(HttpServletRequest request) {
        return "jft/channelList";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        if (id != null) {
            JftMercPaymentChannel entity = baseService.getOne(new QueryWrapper<JftMercPaymentChannel>().eq("id",id));
            request.setAttribute("entity", entity);
        }
        return "jft/channelDetail";
    }

    @Override
    public Result<Page<JftMercPaymentChannel>> page(Page<JftMercPaymentChannel> page, JftMercPaymentChannel condition, OrderItem orderItem) {
        orderItem.setColumn("createTime");
        orderItem.setAsc(false);
        return super.page(page, condition, orderItem);
    }

    @PostMapping("/sendMsg")
    @ResponseBody
    public Result<?> sendMsg(String userId) {
        JftMercPaymentChannel jpc = baseService.getByUserId(userId);
        if (jpc.getStatus().equals(MerchantChannelStatus.AUDIT_STATUS_SUCCESS)) {
            throw new MsgException("已完成进件！");
        }

        JSONObject data = new JSONObject();
        data.put("sysFlowId", jpc.getApplicationId());
        data.put("isSendMsg", "N");
        JSONObject respResult = ysServiceV2.req(YsConfigV2.MER_PROTOCOL_MSG, ReqMethod.MERCHANT_SEND_MSG, data);

        if ("00".equals(respResult.getString("code"))) {
            jpc.getExtra().put("sysSignFlowId", respResult.getString("sysSignFlowId"));
            jpc.getExtra().put("signUrl", respResult.getString("signUrl"));

            Notice notice = new Notice(1, -1, userId);
            String uniqueKey = "YS_QY" + System.currentTimeMillis();
            String title = "电子协议签约通知";
            String content = "尊敬的客户您好，请点此通知完成签约意愿授权。请注意，同意授权后，支付通道才会进行商户资料审核，否则会一直等待审核。";
            String url = respResult.getString("signUrl");
            notice.setUniqueKey(uniqueKey);
            notice.setNoticeCode(uniqueKey);
            notice.setTitle(title);
            notice.setUrl(url);
            notice.setDesc(content);
            noticeService.save(notice);

            generalPushUtil.sendAllPlatByAlias(Collections.singletonList(userId), title, content, "1");

            jpc.setStatus(MerchantChannelStatus.AUDIT_STATUS_WAITING_SIGN);
            baseService.updateById(jpc);
        }

        return ResultGenerator.genSuccessResult("操作成功", null);
    }
}
