package com.czg.controller.admin;

import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopUserService;
import com.czg.market.dto.MkConsumeDiscountDTO;
import com.czg.market.entity.MkShopConsumeDiscountRecord;
import com.czg.market.service.MkConsumeDiscountService;
import com.czg.market.service.MkShopConsumeDiscountRecordService;
import com.czg.market.vo.MkConsumeDiscountVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 新客立减
 * @author Administrator
 */

@RestController
@RequestMapping("/admin/consumeDiscount")
public class ConsumeDiscountController {
    @Resource
    private MkConsumeDiscountService consumeDiscountService;
    @Resource
    private MkShopConsumeDiscountRecordService shopConsumeDiscountRecordService;
    @DubboReference
    private ShopUserService shopUserService;

    /**
     * 配置信息获取
     * 权限标识: activate:list
     */
//    @SaAdminCheckPermission(value = "member:detail", name = "会员配置列表")
    @GetMapping
    public CzgResult<MkConsumeDiscountVO> detail() {
        return CzgResult.success(consumeDiscountService.detail(StpKit.USER.getShopId()));
    }

    /**
     * 配置信息修改
     * @return 是否成功
     */
//    @SaAdminCheckPermission(value = "member:edit", name = "会员配置列表")
    @PostMapping
    public CzgResult<Boolean> edit(@Validated @RequestBody MkConsumeDiscountDTO consumeDiscountDTO) {
        return CzgResult.success(consumeDiscountService.edit(StpKit.USER.getShopId(), consumeDiscountDTO));
    }

    /**
     * 根据用户id获取新客立减金额，返回null代表不可用
     * @param shopId 店铺id
     * @param orderId 订单id 可选
     * @return 新客立减金额
     */
    @GetMapping("/getDiscountByUserId")
    public CzgResult<MkShopConsumeDiscountRecord> getDiscountByUserId(@RequestParam Long shopId, @RequestParam Long shopUserId, @RequestParam(required = false) Long orderId) {
        ShopUser shopUser = shopUserService.getById(shopUserId);
        AssertUtil.isNull(shopUser, "用户不存在");
        return CzgResult.success(shopConsumeDiscountRecordService.getDiscount(shopId, shopUser.getUserId(), orderId));
    }


}
