package com.czg.controller.admin;

import com.czg.account.dto.ShopActivateDTO;
import com.czg.account.service.ShopActivateService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.config.RedisCst;
import com.czg.exception.CzgException;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.util.WechatAuthUtil;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 店铺充值活动管理
 *
 * @author ww
 * @description
 */

@RestController
@RequestMapping("/admin/activate")
public class ShopActivateController {
    @Resource
    private ShopActivateService shopActivateService;
    @Resource
    private RedisService redisService;
    @Resource
    private WechatAuthUtil wechatUtil;

    /**
     * 店铺充值活动列表
     * 权限标识: activate:list
     */
    @SaAdminCheckPermission(value = "activate:list", name = "店铺充值活动列表")
    @GetMapping
    public CzgResult<List<ShopActivateDTO>> detail(@RequestParam(required = false) Long shopId) {
        return CzgResult.success(shopActivateService.getList(shopId));
    }

    /**
     * 店铺充值活动新增
     * 权限标识: activate:add
     */
    @SaAdminCheckPermission(value = "activate:add", name = "店铺充值活动新增")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated ShopActivateDTO activateDTO) {
        activateDTO.setShopId(StpKit.USER.getShopId());
        return CzgResult.success(shopActivateService.add(activateDTO));
    }

    /**
     * 店铺充值活动修改
     * 权限标识: activate:edit
     */
    @SaAdminCheckPermission(value = "activate:edit", name = "店铺充值活动修改")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopActivateDTO activateDTO) {
        return CzgResult.success(shopActivateService.edit(activateDTO));
    }

    /**
     * 获取会员码
     * @param params shopId 必填
     *               env_version 存在即生成体验版
     */
    @PostMapping("/getVipCode")
    public CzgResult<Object> getVipCode(@RequestBody Map<String, Object> params) throws Exception {
        if (CollectionUtils.isEmpty(params) || !params.containsKey("shopId")) {
            throw new CzgException("参数错误");
        }
        String redisKey = RedisCst.SHOP_VIP_CODE + params.get("shopId");
        if (redisService.hasKey(redisKey)) {
            return CzgResult.success(redisService.get(redisKey));
        }
        return CzgResult.success(wechatUtil.getFetchQrCode(params));
    }
}
