package com.cpop.oam.business.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.cpop.common.utils.StringUtils;
import com.cpop.core.base.R;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.pay.framewok.config.wxPay.WxPayConfiguration;
import com.cpop.system.business.bo.ChangeWechatSharingBo;
import com.cpop.system.business.entity.Brand;
import com.cpop.system.business.service.BrandService;
import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingReceiverRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.cpop.system.business.entity.table.BrandTableDef.BRAND;

/**
 * @author DB
 * @createTime 2023/11/15 11:21
 * @description
 */
@RestController
@Api(tags = "微信支付模块")
@RequestMapping("/backstage/wxPay")
public class BackstageWxPayController {

    @Autowired
    private WxPayConfiguration wxPayConfiguration;

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private BrandService brandService;

    /**
     * @descriptions 开通微信分账
     * @author DB
     * @date 2023/11/15 11:06
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @ApiOperation("开通微信分账")
    @PutMapping("/changeBrandSharing")
    public R<Void> changeBrandSharing(@RequestBody @Validated ChangeWechatSharingBo bo) {
        //获取品牌下属商户
        Brand brand = brandService.getById(bo.getBrandId());
        if (StringUtils.isBlank(brand.getWxMchId())) {
            throw new ServiceException("当前品牌暂未绑定系统,请联系相关人员绑定");
        }
        //固定商户信息
        Map<String, Object> mapReceiver = new HashMap<>(4);
        mapReceiver.put("type", "MERCHANT_ID");
        mapReceiver.put("account", wxPayConfiguration.getProperties().getSharingAccount());
        //添加分账接收方
        ProfitSharingReceiverRequest profitSharingReceiver = new ProfitSharingReceiverRequest();
        //开启
        if (bo.getIsOpenSharing()) {
            mapReceiver.put("relation_type", "SERVICE_PROVIDER");
            mapReceiver.put("name", wxPayConfiguration.getProperties().getSharingAccountName());
            profitSharingReceiver.setReceiver(JSONObject.toJSONString(mapReceiver));
            WxPayConfig config = wxPayService.getConfig();
            config.setSubMchId(brand.getWxMchId());
            try {
                wxPayService.getProfitSharingService().addReceiver(profitSharingReceiver);
            } catch (WxPayException e) {
                throw new ServiceException(e.getMessage());
            }
        } else {
            //关闭
            profitSharingReceiver.setReceiver(JSONObject.toJSONString(mapReceiver));
            WxPayConfig config = wxPayService.getConfig();
            config.setSubMchId(brand.getWxMchId());
            try {
                wxPayService.getProfitSharingService().removeReceiver(profitSharingReceiver);
            } catch (WxPayException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        brandService.updateChain().set(BRAND.IS_OPEN_SHARING, bo.getIsOpenSharing()).where(BRAND.ID.eq(bo.getBrandId())).update();
        return R.ok();
    }
}
