package com.ym.mec.collectfee.controller;

import com.alibaba.fastjson.JSON;
import com.ym.mec.collectfee.entity.Order;
import com.ym.mec.collectfee.service.OrderService;
import com.ym.mec.collectfee.utils.GenerateNum;
import com.ym.mec.collectfee.utils.yqpay.YqPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.*;

//@ApiIgnore
@Controller
@RequestMapping("yqreg")
public class YqRegController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户（商户）注册
     *
     * @param map
     * @return
     * @throws Exception
     */
    @GetMapping("/per")
    public String per(ModelMap map) throws Exception {

        String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //主动通知地址

        String seraialNumber = GenerateNum.getInstance().GenerateOrderNo();

        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("type", "web");
        resultMap.put("returnUrl", "http://dev.dayaedu.com/");
        resultMap.put("seraialNumber", seraialNumber);
        resultMap.put("merType", "per");

        Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
        map.addAttribute("info", requestMap);
        return "/reg/register";
    }

    @GetMapping("/com")
    public String com(ModelMap map) throws Exception {
        String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //主动通知地址

        String seraialNumber = GenerateNum.getInstance().GenerateOrderNo();

        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("type", "H5");
        resultMap.put("returnUrl", "http://dev.dayaedu.com/");
        resultMap.put("seraialNumber", seraialNumber);
        resultMap.put("merType", "com");

        Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
        map.addAttribute("info", requestMap);
        return "/reg/register";
    }

    @GetMapping("/pcy")
    public String pcy(ModelMap map) throws Exception {
        String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //主动通知地址

        String seraialNumber = GenerateNum.getInstance().GenerateOrderNo();

        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("type", "H5");
        resultMap.put("returnUrl", "http://dev.dayaedu.com/");
        resultMap.put("seraialNumber", seraialNumber);
        resultMap.put("merType", "pcy");

        Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
        map.addAttribute("info", requestMap);
        return "/reg/register";
    }

    @RequestMapping("/topay")
    public String toPay(ModelMap map) throws Exception {

        String orderNo = GenerateNum.getInstance().GenerateOrderNo();
        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setCreateTime(new Date());
        order.setStatus(1);
        order.setAmount(new BigDecimal(3));

        /*卖家双乾商户号*/
        String sellerNo = "0021677"; //大雅主体商户号
        order.setAccount(sellerNo);
        /*订单信息*/
        String orderBody = "大雅乐盟培训课程";
        /*订单标题*/
        String orderSubject = "培训订单";

        String notifyUrl = "http://47.99.212.176:9000/yqpay/notify";
        String returnUrl = "http://dev.dayaedu.com";//前台页面通知地址(银联H5必填)
        String payChannels = "{\"fastpayXy\":true,\"weChatPay\":true,\"weChatPayMobile\":false,\"aliPay\":true,\"aliPayMobile\":true,\"balancePay\":false}";


        Map<String, Object> routingList = new LinkedHashMap<String, Object>();
        routingList.put("routingMerNo", "0022652"); //分佣账户
        routingList.put("routingFee", "0.1"); //分佣金额
        List<Map> tempRoutingList = new ArrayList();
        tempRoutingList.add(routingList);

        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("sellerNo", sellerNo); //收款商户号
        resultMap.put("payChannels", payChannels); //支付方式
        resultMap.put("orderBody", orderBody); //订单信息
        resultMap.put("payAmount", order.getAmount()); //支付金额
        resultMap.put("apiPayType", "1"); //*API支付类型1-即时支付,2-担保支付,3-预授权支付*/
        resultMap.put("tradeType", "0"); //*交易类型1—充值，0—收款*
        resultMap.put("merMerOrderNo", orderNo); //商户订单号
        resultMap.put("orderSubject", orderSubject); //订单标题
        resultMap.put("returnUrl", returnUrl); //前台页面地址
        resultMap.put("tempRoutingList", JSON.toJSONString(tempRoutingList));//分账设置
        Map rqMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
        map.put("info", rqMap);
        return "/reg/order";
    }

    @GetMapping("/webcom")
    public String webcom(ModelMap map) throws Exception {
        String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //主动通知地址

        String seraialNumber = GenerateNum.getInstance().GenerateOrderNo();

        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("type", "web");
        resultMap.put("returnUrl", "http://dev.dayaedu.com/");
        resultMap.put("seraialNumber", seraialNumber);
        resultMap.put("merType", "com");

        Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
        map.addAttribute("info", requestMap);
        return "/reg/register";
    }

    @GetMapping("/webper")
    public String webper(ModelMap map) throws Exception {

        String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //主动通知地址

        String seraialNumber = GenerateNum.getInstance().GenerateOrderNo();

        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("type", "web");
        resultMap.put("returnUrl", "http://dev.dayaedu.com/");
        resultMap.put("seraialNumber", seraialNumber);
        resultMap.put("merType", "per");

        Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
        map.addAttribute("info", requestMap);
        return "/reg/register";
    }

    @GetMapping("/webpcy")
    public String webpcy(ModelMap map) throws Exception {
        String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //主动通知地址

        String seraialNumber = GenerateNum.getInstance().GenerateOrderNo();

        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("type", "web");
        resultMap.put("returnUrl", "http://dev.dayaedu.com/");
        resultMap.put("seraialNumber", seraialNumber);
        resultMap.put("merType", "pcy");

        Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
        map.addAttribute("info", requestMap);
        return "/reg/register";
    }



}
