package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.util.SybConstants;
import com.ssm.core.util.SybPayUtil;
import com.ssm.manage.entity.StoreAllinpayInfo;
import com.ssm.manage.query.StoreAllinpayInfoRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreAllinpayinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "通联第三方支付controller", tags = {"通联第三方支付相关接口"})
@RestController
@RequestMapping("/demay/ssm/allipay")
@Slf4j
public class SsmAllipayController {
    @Autowired
    private SsmStoreAllinpayinfoService ssmStoreAllinpayinfoService;

    @ApiOperation(value = "通联第三方统一支付接口", notes = "美发版-通联第三方统一支付接口", response = GenericResponse.class)
    @RequestMapping(path="/pay", method= RequestMethod.POST)
    public GenericResponse pay(@RequestParam Map<String,String> formMap) {
        GenericResponse genericResponse = new GenericResponse<>();
        StoreAllinpayInfoRequest storeAllinpayInfoRequest = new StoreAllinpayInfoRequest();
        storeAllinpayInfoRequest.setStoreId(Long.parseLong(formMap.get("storeId")));
        StoreAllinpayInfo storeAllinpayInfo = ssmStoreAllinpayinfoService.selectList(storeAllinpayInfoRequest).get(0);
        String reqsn = String.valueOf(System.currentTimeMillis());
        Map map = new HashMap();
        Long trxamt = Long.parseLong(formMap.get("trxamt"));
        String acct = formMap.get("acct");
        String sub_appid = formMap.get("sub_appid");
        try{
            map = new SybPayUtil().pay(trxamt, reqsn, SybConstants.SYB_PAYTYPE, "", "", acct, "","",
                    "","","","", sub_appid, "", "", "", "",
                    "", "","", storeAllinpayInfo);
        }catch (Exception e){
            log.error("SsmAllipayController.pay:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (map.get("retcode").equals("SUCCESS")) {
            genericResponse.setData(map);
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "通联第三方查询接口", notes = "美发版-通联第三方查询接口", response = GenericResponse.class)
    @RequestMapping(path="/query", method= RequestMethod.POST)
    public GenericResponse query(@RequestParam Map<String,String> formMap) {
        GenericResponse genericResponse = new GenericResponse<>();
        StoreAllinpayInfoRequest storeAllinpayInfoRequest = new StoreAllinpayInfoRequest();
        storeAllinpayInfoRequest.setStoreId(Long.parseLong(formMap.get("storeId")));
        StoreAllinpayInfo storeAllinpayInfo = ssmStoreAllinpayinfoService.selectList(storeAllinpayInfoRequest).get(0);

        Map map = new HashMap();
        String trxid = formMap.get("trxid");
        try{
            map = new SybPayUtil().query("", trxid, storeAllinpayInfo);
        }catch (Exception e){
            log.error("SsmAllipayController.query:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (map.get("retcode").equals("SUCCESS")) {
            genericResponse.setData(map);
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @RequestMapping(path="/cancel", method= RequestMethod.POST)
    public GenericResponse cancel(@RequestParam Map<String,String> formMap) {
        GenericResponse genericResponse = new GenericResponse<>();
        StoreAllinpayInfoRequest storeAllinpayInfoRequest = new StoreAllinpayInfoRequest();
        storeAllinpayInfoRequest.setStoreId(Long.parseLong(formMap.get("storeId")));
        StoreAllinpayInfo storeAllinpayInfo = ssmStoreAllinpayinfoService.selectList(storeAllinpayInfoRequest).get(0);
        Map map = new HashMap();
        String reqsn = String.valueOf(System.currentTimeMillis());
        String oldtrxid = formMap.get("oldtrxid");
        Long trxamt = Long.parseLong(formMap.get("trxamt"));
        try{
            map = new SybPayUtil().cancel(trxamt, reqsn, oldtrxid, "", storeAllinpayInfo);
        }catch (Exception e){
            log.error("SsmAllipayController.cancel:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (map.get("retcode").equals("SUCCESS")) {
            genericResponse.setData(map);
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }


    @RequestMapping(path="/refund", method= RequestMethod.POST)
    public GenericResponse refund(@RequestParam Map<String,String> formMap) {
        GenericResponse genericResponse = new GenericResponse<>();
        StoreAllinpayInfoRequest storeAllinpayInfoRequest = new StoreAllinpayInfoRequest();
        storeAllinpayInfoRequest.setStoreId(Long.parseLong(formMap.get("storeId")));
        StoreAllinpayInfo storeAllinpayInfo = ssmStoreAllinpayinfoService.selectList(storeAllinpayInfoRequest).get(0);
        Map map = new HashMap();
        String reqsn = String.valueOf(System.currentTimeMillis());
        String oldtrxid = formMap.get("oldtrxid");
        Long trxamt = Long.parseLong(formMap.get("trxamt"));
        try{
            map = new SybPayUtil().refund(trxamt, reqsn, oldtrxid, "", storeAllinpayInfo);
        }catch (Exception e){
            log.error("SsmAllipayController.refund:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (map.get("retcode").equals("SUCCESS")) {
            genericResponse.setData(map);
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }
}
