package cn.ysk.cashier.mybatis.rest;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.dto.CouponDto;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.entity.TbActivate;
import cn.ysk.cashier.mybatis.service.TbActivateService;
import cn.ysk.cashier.mybatis.service.TbShopCouponService;
import cn.ysk.cashier.service.WxService;
import cn.ysk.cashier.utils.CacheKey;
import cn.ysk.cashier.utils.RedisUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "/shop/storage")
@RequestMapping("/shop/storage")
public class StorageController {

    private final TbShopCouponService tbShopCouponService;
    private final TbActivateService tbActivateService;
    private final RedisUtils redisUtils;
    private final WxService wxService;

    @GetMapping("/findActivate")
    public ResponseEntity<Object> findActivate(@RequestParam String shopId){
        return new ResponseEntity<>(tbActivateService.findActivate(shopId), HttpStatus.OK);
    }

    @Log("修改活动数据")
    @PostMapping("/upActivate")
    public ResponseEntity<Object> upActivate(@RequestBody TbActivate activate){
        tbActivateService.modifyActivate(activate);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //获取订单可用优惠券
    @RequestMapping("find")
    public ResponseEntity<Object> find(@RequestBody CouponDto param) {
        return tbShopCouponService.find(param);
    }

    /**
     * @param params
     *      shopId 必填
     *      env_version 存在即生成体验版
     * @return
     */
    @PostMapping("/getwxacode")
    public ResponseEntity<Object> getwxacode(@RequestBody Map<String, Object> params) {
        if (CollectionUtils.isEmpty(params) || !params.containsKey("shopId"))
            throw new BadRequestException("参数错误");
        if (redisUtils.hasKey(CacheKey.VIPCODE + params.get("shopId")))
            return new ResponseEntity<>(redisUtils.get(CacheKey.VIPCODE + params.get("shopId")), HttpStatus.OK);
        return new ResponseEntity<>(wxService.getFetchQRCode(params), HttpStatus.OK);
    }
}
