package com.czg.controller.admin;

import com.czg.PageQueryParam;
import com.czg.log.annotation.OperationLog;
import com.czg.market.dto.MkDiscountActivityDTO;
import com.czg.market.service.MkDiscountActivityService;
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/discountActivity")
public class ADiscountActivityController {
    @Resource
    private MkDiscountActivityService discountActivityService;

    /**
     * 分页
     */
    @GetMapping("page")
    public CzgResult<Page<MkDiscountActivityDTO>> getActivityPage(PageQueryParam param) {
        Page<MkDiscountActivityDTO> data = discountActivityService.getActivityPage(param, StpKit.USER.getShopId());
        return CzgResult.success(data);
    }


    /**
     * 新增
     */
    @PostMapping
    @OperationLog("满减活动-新增")
    public CzgResult<Void> addActivity(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) MkDiscountActivityDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        discountActivityService.addActivity(dto);
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("满减活动-修改")
    public CzgResult<Void> updateCoupon(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) MkDiscountActivityDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        discountActivityService.updateActivityById(dto);
        return CzgResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @OperationLog("满减活动--删除")
    public CzgResult<String> deleteCoupon(@RequestParam Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        discountActivityService.deleteActivity(id);
        return CzgResult.success();
    }

    /**
     * 分页
     */
    @GetMapping
    public CzgResult<MkDiscountActivityDTO> getActivity(@RequestParam Long shopId) {
        MkDiscountActivityDTO data = discountActivityService.getActivityByShopId(shopId);
        return CzgResult.success(data);
    }


}
