package com.fs.hisStore.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.fs.bean.AliPayBean;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.CloudHostUtils;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.utils.spring.SpringUtils;
import com.fs.company.service.ICompanyService;
import com.fs.config.cloud.CloudHostProper;
import com.fs.his.domain.FsHfpayConfig;
import com.fs.his.mapper.FsHfpayConfigMapper;
import com.fs.hisStore.domain.FsPayConfigScrm;
import com.fs.huifuPay.domain.HuiFuQueryOrderResult;
import com.fs.huifuPay.domain.HuiFuRefundResult;
import com.fs.huifuPay.sdk.opps.core.request.V2TradePaymentScanpayQueryRequest;
import com.fs.huifuPay.sdk.opps.core.request.V2TradePaymentScanpayRefundRequest;
import com.fs.huifuPay.service.HuiFuService;
import com.fs.hisStore.domain.FsStorePaymentScrm;
import com.fs.hisStore.param.FsStorePaymentParam;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import com.fs.hisStore.service.IFsStorePaymentScrmService;
import com.fs.hisStore.vo.FsStorePaymentVO;
import com.fs.system.service.ISysConfigService;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付明细Controller
 *
 * @author fs
 * @date 2022-06-25
 */
@RestController
@RequestMapping("/store/store/storePayment")
public class FsStorePaymentScrmController extends BaseController
{
    @Autowired
    private IFsStorePaymentScrmService fsStorePaymentService;
    @Autowired
    private AliPayBean aliPayBean;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IFsStoreOrderScrmService orderService;
    @Autowired
    HuiFuService huiFuService;
    @Autowired
    private CloudHostProper cloudHostProper;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询支付明细列表
     */
    @PreAuthorize("@ss.hasPermi('store:storePayment:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStorePaymentParam fsStorePayment)
    {
        startPage();
        if(!StringUtils.isEmpty(fsStorePayment.getCreateTimeRange())){
            fsStorePayment.setCreateTimeList(fsStorePayment.getCreateTimeRange().split("--"));
        }
        List<FsStorePaymentVO> list = fsStorePaymentService.selectFsStorePaymentListQuery(fsStorePayment);
        for (FsStorePaymentVO vo : list){
            vo.setUserPhone(ParseUtils.parsePhone(vo.getUserPhone()));
        }
        return getDataTable(list);
    }

    /**
     * 导出支付明细列表
     */
    @PreAuthorize("@ss.hasPermi('store:storePayment:export')")
    @Log(title = "导出支付明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStorePaymentParam fsStorePayment)
    {
        if(!StringUtils.isEmpty(fsStorePayment.getCreateTimeRange())){
            fsStorePayment.setCreateTimeList(fsStorePayment.getCreateTimeRange().split("--"));
        }
        List<FsStorePaymentVO> list = fsStorePaymentService.exportFsStorePaymentList(fsStorePayment);
        for (FsStorePaymentVO vo : list){
            vo.setUserPhone(ParseUtils.parsePhone(vo.getUserPhone()));
        }
        ExcelUtil<FsStorePaymentVO> util = new ExcelUtil<FsStorePaymentVO>(FsStorePaymentVO.class);
        return util.exportExcel(list, "storePayment");
    }

    /**
     * 获取支付明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storePayment:query')")
    @GetMapping(value = "/{paymentId}")
    public AjaxResult getInfo(@PathVariable("paymentId") Long paymentId)
    {
        return AjaxResult.success(fsStorePaymentService.selectFsStorePaymentById(paymentId));
    }


    @ApiOperation("支付手动通知")
    @PreAuthorize("@ss.hasPermi('store:storePayment:payNotify')")
    @PostMapping("/returnPayStatus")
    public R returnPayStatus(@RequestBody FsStorePaymentScrm fsStorePayment) throws Exception
    {
        FsStorePaymentScrm payment=  fsStorePaymentService.selectFsStorePaymentByTradeNo(fsStorePayment.getTradeNo());
        String payMode = payment.getPayMode();
        logger.info("手动查询"+payment);
        if (payMode.equals("hf")){
            V2TradePaymentScanpayQueryRequest request = new V2TradePaymentScanpayQueryRequest();
            request.setOrgReqDate(new SimpleDateFormat("yyyyMMdd").format(payment.getCreateTime()));
            request.setOrgHfSeqId(payment.getTradeNo());
            HuiFuQueryOrderResult o = null;
            try {
                o = huiFuService.queryOrder(request);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            logger.info("汇付返回"+o);
            if ("00000000".equals(o.getResp_code())) {
                if (o.getTrans_stat().equals("S")) {
                    String[] order=o.getOrg_req_seq_id().split("-");
                    switch (order[0]) {
                        case "store":
                            orderService.payConfirm(1,null,order[1], o.getOrg_hf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                            break;
                        case "store_remain":
                            orderService.payRemainConfirm( order[1], o.getOrg_hf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                            break;
                        case "payment":
                            fsStorePaymentService.payConfirm(order[1],o.getOrg_hf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                            break;
                    }
                    return R.ok();
                }
            }
        }
        return R.error("请检查外部订单号");
    }



    /**
     * 新增支付明细
     */
    @PreAuthorize("@ss.hasPermi('store:storePayment:add')")
    @Log(title = "支付明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStorePaymentScrm fsStorePayment)
    {
        return toAjax(fsStorePaymentService.insertFsStorePayment(fsStorePayment));
    }

    @PreAuthorize("@ss.hasPermi('store:storePayment:refund')")
    @PostMapping("refundStorePayment")
    @Transactional
    public R refundStorePayment(@RequestBody FsStorePaymentScrm fsStorePayment)
    {
        FsStorePaymentScrm payment=fsStorePaymentService.selectFsStorePaymentById(fsStorePayment.getPaymentId());
        if(payment.getStatus()!=1){
            return R.error("非法操作");
        }
        if(fsStorePayment.getRefundMoney().compareTo(new BigDecimal(0))<1){
            return R.error("退款金额必须大于0");
        }
        if(payment.getPayMoney().compareTo(fsStorePayment.getRefundMoney())==-1){
            return R.error("退款金额必须小于等于付款金额");
        }

        if(payment.getPayTypeCode().equals("weixin")){
            String json;
            if (CloudHostUtils.hasCloudHostName("康年堂")){
                json = configService.selectConfigByKey("his.pay");
            } else {
                json = configService.selectConfigByKey("store.pay");
            }
            if (StringUtils.isBlank(json)) {
                return R.error("缺少支付相关配置");
            }
            FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
            if (payment.getPayMode()!=null&&payment.getPayMode().equals("hf")){
                String huifuId="";
                FsHfpayConfigMapper fsHfpayConfigMapper = SpringUtils.getBean(FsHfpayConfigMapper.class);
                if (payment.getAppId() != null) {
                    FsHfpayConfig fsHfpayConfig = fsHfpayConfigMapper.selectByAppId(payment.getAppId());
                    huifuId = fsHfpayConfig.getHuifuId();
                } else {
                    if (("益善缘".equals(cloudHostProper.getCompanyName()))) {

                        FsHfpayConfig fsPayConfig2 = fsHfpayConfigMapper.selectByAppId("wx0d1a3dd485268521");
                        huifuId = fsPayConfig2.getHuifuId();
                    }else {
                        huifuId=fsPayConfig.getHuifuId();
                    }
                }
                V2TradePaymentScanpayRefundRequest request = new V2TradePaymentScanpayRefundRequest();
                request.setHuifuId(huifuId);
                request.setOrdAmt(payment.getPayMoney().toString());
                request.setOrgReqDate(new SimpleDateFormat("yyyyMMdd").format(payment.getCreateTime()));
                request.setReqSeqId("refund-"+payment.getPayCode());
                Map<String, Object> extendInfoMap = new HashMap<>();
                extendInfoMap.put("org_party_order_id", payment.getBankSerialNo());
                request.setExtendInfo(extendInfoMap);
                HuiFuRefundResult refund = huiFuService.refund(request);
                logger.info("退款："+refund);
                if((refund.getResp_code().equals("00000000")||refund.getResp_code().equals("00000100"))&&(refund.getTrans_stat().equals("S")||refund.getTrans_stat().equals("P"))){
                    payment.setRefundMoney(fsStorePayment.getRefundMoney());
                    payment.setStatus(-1);
                    payment.setRefundTime(new Date());
                    fsStorePaymentService.updateFsStorePayment(payment);
                    //收款 减去所有
                    if(payment.getCompanyId()!=null&&payment.getCompanyId()>0){
                        companyService.subCompanyPaymentMoney(payment);
                    }
                    return R.ok();
                }else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return R.error(refund.getResp_desc());
                }

            }else if (payment.getPayMode()!=null&&payment.getPayMode().equals("wx")){

            }

            //小雨点退款
//            payment.setRefundMoney(fsStorePayment.getRefundMoney());
//            fsStorePaymentService.updateFsStorePayment(payment);
//
//            RefundDTO refundDTO=new RefundDTO();
//            refundDTO.setMerchOrderNo(Ids.oid());
//            refundDTO.setAmount(fsStorePayment.getRefundMoney().toString());
//            refundDTO.setOrigMerchOrderNo("payment-"+payment.getPayCode());
//            return payService.refundOrder(refundDTO);
        }
        else if(payment.getPayTypeCode().equals("alipay")){
            try {
                AliPayApiConfig aliPayApiConfig;
                try {
                    aliPayApiConfig = AliPayApiConfigKit.getApiConfig(aliPayBean.getAppId());
                } catch (Exception e) {
                    aliPayApiConfig = AliPayApiConfig.builder()
                            .setAppId(aliPayBean.getAppId())
                            .setAliPayPublicKey(aliPayBean.getPublicKey())
                            .setAppCertPath(aliPayBean.getAppCertPath())
                            .setAliPayCertPath(aliPayBean.getAliPayCertPath())
                            .setAliPayRootCertPath(aliPayBean.getAliPayRootCertPath())
                            .setCharset("UTF-8")
                            .setPrivateKey(aliPayBean.getPrivateKey())
                            .setServiceUrl(aliPayBean.getServerUrl())
                            .setSignType("RSA2")
                            // 普通公钥方式
                            .build();
                    // 证书模式
//					.buildByCert();
                }
                AliPayApiConfigKit.putApiConfig(aliPayApiConfig);
                AlipayTradeRefundModel model = new AlipayTradeRefundModel();
                model.setOutTradeNo("payment-"+payment.getPayCode());
                model.setTradeNo(payment.getTradeNo());
                model.setRefundAmount(fsStorePayment.getRefundMoney().toString());
                model.setRefundReason("退款");
                String result= AliPayApi.tradeRefundToResponse(model).getBody();
                cn.hutool.json.JSONObject json= JSONUtil.parseObj(result);
                cn.hutool.json.JSONObject jsonInfo= (cn.hutool.json.JSONObject) json.get("alipay_trade_refund_response");
                String code=(String)jsonInfo.get("code");
                String msg=(String)jsonInfo.get("sub_msg");
                //{"alipay_trade_refund_response":{"code":"10000","msg":"Success","buyer_logon_id":"270***@qq.com","buyer_user_id":"2088402776950529","fund_change":"Y","gmt_refund_pay":"2022-04-27 18:27:28","out_trade_no":"goods-202204271826530001","refund_fee":"0.10","send_back_fee":"0.00","trade_no":"2022042722001450521456255417"},"sign":"hU+dy17/juMYQEQpO7Yy7jxkx9h5ebSbN3xdZr58msfOfJEUknqra6w4L37pgoZSx1Vj00jK3Ds06vrph6mSEliq3PQ37PwbZiRK3ZHaaKlz+9ndjoBTFYAxP60zLASfTq+W+dczDq4KOrvwhprFGt7YwKXGU42PgfOPb5EOgWyYUP6ivP0r06OzTo0f2lB28w6AQ4m4IQjdIL/tWbDaKl+ld8MPMLIgw5k9BmMcP8LV8ENC6+Gl1u5GwgAMjwHfk0RfB/kTFTHUTa7fgaO22w3pT8YKFMnOdKA0cVuJ2LE4SdxepqlprWJdCtLOeoQUX6PkEtoJGywPWAyjgdJ6Pg=="}
                if(code.equals("10000")){
                    payment.setRefundMoney(fsStorePayment.getRefundMoney());
                    fsStorePaymentService.updateFsStorePayment(payment);
                    fsStorePaymentService.refund(payment.getPayCode());
                    return R.ok("退款成功");

                }
                else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return R.error("退款失败:"+msg);

                }
                //退款成功
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
        }
        return R.error("非法操作");
    }


    /**
     * 修改支付明细
     */
    @PreAuthorize("@ss.hasPermi('store:storePayment:edit')")
    @Log(title = "支付明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStorePaymentScrm fsStorePayment)
    {
        return toAjax(fsStorePaymentService.updateFsStorePayment(fsStorePayment));
    }

    /**
     * 删除支付明细
     */
    @PreAuthorize("@ss.hasPermi('store:storePayment:remove')")
    @Log(title = "支付明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paymentIds}")
    public AjaxResult remove(@PathVariable Long[] paymentIds)
    {
        return toAjax(fsStorePaymentService.deleteFsStorePaymentByIds(paymentIds));
    }
}
