package com.hzs.third.pay.controller.manage;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.third.pay.constants.PayConfigConstants;
import com.hzs.third.pay.param.OnlinePayConfigParam;
import com.hzs.third.pay.vo.OnlinePayConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 在线支付配置
 * @Author: jiang chao
 * @Time: 2023/3/23 16:15
 * @Classname: OnlinePayConfigController
 * @PackageName: com.hzs.third.pay.controller.manage
 */
@RestController
@RequestMapping("/manage/online-config")
public class OnlinePayConfigController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取支付配置
     *
     * @return
     */
    @Log(module = EOperationModule.PAY_CONFIG, business = EOperationBusiness.PAY_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/get-config")
    public AjaxResult getConfig() {
        Integer pkCountry = SecurityUtils.getPkCountry();

        OnlinePayConfigVO payConfigVO = new OnlinePayConfigVO();

        // 各支付方式： true=显示，false=隐藏

        // 新汇付PC微信扫码支付
        String pay81 = String.format(PayConfigConstants.PAY_CONFIG_81, pkCountry);
        if (redisTemplate.hasKey(pay81)) {
            payConfigVO.setPay81((Boolean) redisTemplate.opsForValue().get(pay81));
        }
        // 新汇付PC支付宝扫码支付
        String pay82 = String.format(PayConfigConstants.PAY_CONFIG_82, pkCountry);
        if (redisTemplate.hasKey(pay82)) {
            payConfigVO.setPay82((Boolean) redisTemplate.opsForValue().get(pay82));
        }
        // 新汇付H5微信支付
//        String pay83 = String.format(PayConfigConstants.PAY_CONFIG_83, pkCountry);
//        if (redisTemplate.hasKey(pay83)) {
//            payConfigVO.setPay83((Boolean) redisTemplate.opsForValue().get(pay83));
//        }
//        // 新汇付H5支付宝支付
//        String pay84 = String.format(PayConfigConstants.PAY_CONFIG_84, pkCountry);
//        if (redisTemplate.hasKey(pay84)) {
//            payConfigVO.setPay84((Boolean) redisTemplate.opsForValue().get(pay84));
//        }

        return AjaxResult.success(payConfigVO);
    }

    /**
     * 保存支付配置
     *
     * @param param
     * @return
     */
    @Log(module = EOperationModule.PAY_CONFIG, business = EOperationBusiness.PAY_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/save-config")
    public AjaxResult saveConfig(@RequestBody OnlinePayConfigParam param) {
        Integer pkCountry = SecurityUtils.getPkCountry();

        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_5, pkCountry), param.getPay5());
        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_6, pkCountry), param.getPay6());

        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_32, pkCountry), param.getPay32());
        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_33, pkCountry), param.getPay33());

        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_81, pkCountry), param.getPay81());
        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_82, pkCountry), param.getPay82());
//        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_83, pkCountry), param.getPay83());
//        redisTemplate.opsForValue().set(String.format(PayConfigConstants.PAY_CONFIG_84, pkCountry), param.getPay84());

        return AjaxResult.success();
    }

}
