package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.czg.account.dto.points.PointsGoodsSettingDTO;
import com.czg.account.entity.PointsGoodsSetting;
import com.czg.account.service.PointsGoodsSettingService;
import com.czg.enums.DeleteEnum;
import com.czg.log.annotation.OperationLog;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.validator.ValidatorUtil;
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 lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 积分商品设置
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/admin/points/goodsSetting")
public class PointsGoodsSettingController {

    private final PointsGoodsSettingService pointsGoodsSettingService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("积分商品设置-分页")
    //@SaAdminCheckPermission("pointsGoodsSetting:page")
    public CzgResult<Page<PointsGoodsSettingDTO>> getPointsGoodsSettingPage(PointsGoodsSettingDTO param) {
        Page<PointsGoodsSettingDTO> data = pointsGoodsSettingService.getPointsGoodsSettingPage(param);
        return CzgResult.success(data);
    }

    /**
     * 详情
     */
    @GetMapping("{id}")
    @OperationLog("积分商品设置-详情")
    //@SaAdminCheckPermission("pointsGoodsSetting:info")
    public CzgResult<PointsGoodsSettingDTO> getPointsGoodsSettingById(@PathVariable("id") Long id) {
        PointsGoodsSetting entity = pointsGoodsSettingService.getById(id);
        PointsGoodsSettingDTO data = BeanUtil.copyProperties(entity, PointsGoodsSettingDTO.class);
        return CzgResult.success(data);
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("积分商品设置-新增")
    //@SaAdminCheckPermission("pointsGoodsSetting:add")
    public CzgResult<Boolean> addPointsGoodsSetting(@RequestBody PointsGoodsSettingDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        ValidatorUtil.validateEntity(dto, InsertGroup.class, DefaultGroup.class);
        PointsGoodsSetting entity = BeanUtil.copyProperties(dto, PointsGoodsSetting.class);
        boolean ret = pointsGoodsSettingService.save(entity);
        return CzgResult.success(ret);
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("积分商品设置-修改")
    //@SaAdminCheckPermission("pointsGoodsSetting:update")
    public CzgResult<Boolean> updatePointsGoodsSetting(@RequestBody PointsGoodsSettingDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        ValidatorUtil.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        PointsGoodsSetting entity = new PointsGoodsSetting();
        BeanUtil.copyProperties(dto, entity, CopyOptions.create().setIgnoreNullValue(false));
        boolean ret = pointsGoodsSettingService.updateById(entity);
        return CzgResult.success(ret);
    }

    /**
     * 删除
     */
    @DeleteMapping("{id}")
    @OperationLog("积分商品设置-删除")
    //@SaAdminCheckPermission("pointsGoodsSetting:delete")
    public CzgResult<Void> deletePointsGoodsSetting(@PathVariable("id") Long id) {
        PointsGoodsSetting entity = pointsGoodsSettingService.getById(id);
        entity.setDelFlag(DeleteEnum.DELETED.value());
        pointsGoodsSettingService.updateById(entity);
        return CzgResult.success();
    }
}