package com.czg.controller.admin;

import com.czg.PageQueryParam;
import com.czg.log.annotation.OperationLog;
import com.czg.market.dto.MkLimitTimeDiscountDTO;
import com.czg.market.entity.MkLimitTimeDiscount;
import com.czg.market.service.MkLimitTimeDiscountService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.czg.validator.group.DefaultGroup;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 限时折扣
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/limitTimeDiscount")
public class ALimitTimeDiscountController {
    @Resource
    private MkLimitTimeDiscountService limitTimeDiscountService;

    /**
     * 限时折扣-分页
     */
    @GetMapping("page")
    public CzgResult<Page<MkLimitTimeDiscountDTO>> getLimitTimeDiscountPage(PageQueryParam param) {
        Page<MkLimitTimeDiscountDTO> data = limitTimeDiscountService.getLimitTimeDiscountPage(param, StpKit.USER.getShopId());
        return CzgResult.success(data);
    }


    /**
     * 限时折扣-新增
     */
    @PostMapping
    @OperationLog("限时折扣-新增")
    public CzgResult<Void> addLimitTimeDiscount(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) MkLimitTimeDiscountDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        limitTimeDiscountService.addLimitTimeDiscount(dto);
        return CzgResult.success();
    }

    /**
     * 限时折扣-修改
     */
    @PutMapping
    @OperationLog("限时折扣-修改")
    public CzgResult<Void> updateLimitTimeDiscount(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) MkLimitTimeDiscountDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        limitTimeDiscountService.updateLimitTimeDiscountById(dto);
        return CzgResult.success();
    }

    /**
     * 限时折扣-删除
     */
    @DeleteMapping
    @OperationLog("限时折扣-删除")
    public CzgResult<String> deleteLimitTimeDiscount(@RequestParam Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        limitTimeDiscountService.deleteLimitTimeDiscount(id);
        return CzgResult.success();
    }

    /**
     * 获取限时折扣
     */
    @GetMapping
    public CzgResult<MkLimitTimeDiscount> getActivity(@RequestParam Long shopId) {
        MkLimitTimeDiscount data = limitTimeDiscountService.getLimitTimeDiscountByShopId(shopId);
        return CzgResult.success(data);
    }

}
