package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.czg.BaseQueryParam;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.constants.SystemConstants;
import com.czg.exception.CzgException;
import com.czg.market.dto.MkPointsGoodsDTO;
import com.czg.market.entity.MkPointsGoods;
import com.czg.market.entity.ShopCoupon;
import com.czg.market.service.MkPointsGoodsService;
import com.czg.market.service.ShopCouponService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 积分商城
 *
 * @author ww
 */
@RestController
@RequestMapping("/admin/pointsGoods")
public class PointsGoodsController {

    @Resource
    private MkPointsGoodsService pointsGoodsSettingService;
    @Resource
    private ShopCouponService shopCouponService;

    /**
     * 积分:商品:列表
     */
    @GetMapping("page")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:goods:info", name = "积分-商品-列表")
    public CzgResult<Page<MkPointsGoods>> getPointsGoodsSettingPage(BaseQueryParam param) {
        Page<MkPointsGoods> data = pointsGoodsSettingService.getPointsGoodsPage(param, StpKit.USER.getShopId());
        return CzgResult.success(data);
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:goods:info", name = "积分-商品-列表")
    public CzgResult<MkPointsGoods> getPointsGoodsSettingById(@PathVariable("id") Long id) {
        return CzgResult.success(pointsGoodsSettingService.getById(id));
    }

    /**
     * 积分:商品:新增/修改
     */
    @PostMapping
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:goods:up", name = "积分-商品-新增/修改")
    public CzgResult<Boolean> addPointsGoodsSetting(@RequestBody @Validated MkPointsGoodsDTO dto) {
        dto.setShopId(StpKit.USER.getShopId());
        if (dto.getCouponId() != null) {
            ShopCoupon coupon = shopCouponService.getById(dto.getCouponId());
            if (coupon == null || coupon.getIsDel() == SystemConstants.OneZero.ONE || coupon.getStatus() != 1) {
                throw new CzgException("操作失败，优惠券已删除或未启用");
            }
            if ("custom".equals(coupon.getValidType())) {
                if (coupon.getValidEndTime().isBefore(LocalDateTime.now())) {
                    throw new CzgException("操作失败，对应的优惠券已过期");
                }
            }
        }
        MkPointsGoods entity = BeanUtil.copyProperties(dto, MkPointsGoods.class);
        boolean ret;
        if (entity.getId() == null) {
            ret = pointsGoodsSettingService.saveOrUpdate(entity);
        } else {
            ret = pointsGoodsSettingService.updateById(entity, false);
        }
        return CzgResult.success(ret);
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:goods:delete", name = "积分-商品-删除")
    public CzgResult<Void> deletePointsGoodsSetting(@PathVariable("id") Long id) {
        MkPointsGoods entity = pointsGoodsSettingService.getById(id);
        entity.setDelFlag(SystemConstants.OneZero.ONE);
        pointsGoodsSettingService.updateById(entity);
        return CzgResult.success();
    }
}