package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.ad.ShopAdDTO;
import com.czg.account.entity.ShopAd;
import com.czg.account.service.ShopAdService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序广告管理
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/shopAd")
public class ShopAdController {
    @Resource
    private ShopAdService shopAdService;

    /**
     * 小程序广告列表
     * @param showPosition 展示位置 home首页，make_order点餐页
     * @param status 状态 0未启用，1已启用
     * @return 列表
     */
    @SaAdminCheckPermission(parentName = "小程序广告",value = "shopAd:list", name = "小程序广告列表")
    @GetMapping
    public CzgResult<List<ShopAd>> list(String showPosition, Integer status) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(ShopAd::getShopId, StpKit.USER.getShopId());
        if (StrUtil.isNotBlank(showPosition)) {
            queryWrapper.eq(ShopAd::getShowPosition, showPosition);
        }

        queryWrapper.eq(ShopAd::getStatus, status);
        return CzgResult.success(shopAdService.list(queryWrapper));
    }

    /**
     * 小程序广告详情
     * @param id adId
     * @return 详情
     */
    @SaAdminCheckPermission(parentName = "小程序广告",value = "shopAd:detail", name = "小程序广告详情")
    @GetMapping("/detail")
    public CzgResult<ShopAd> detail(@RequestParam Integer id) {
        return CzgResult.success(shopAdService.getOne(new QueryWrapper().eq(ShopAd::getId, id).eq(ShopAd::getShopId, StpKit.USER.getShopId())));
    }

    /**
     * 小程序广告编辑
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "小程序广告",value = "shopAd:edit", name = "小程序广告编辑")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated(UpdateGroup.class) ShopAdDTO shopAdEditDTO) {
        ShopAd shopAd = BeanUtil.copyProperties(shopAdEditDTO, ShopAd.class);
        long count = shopAdService.count(new QueryWrapper().eq(ShopAd::getShopId, StpKit.USER.getShopId()).like(ShopAd::getShowPosition, shopAdEditDTO.getShowPosition()).ne(ShopAd::getId, shopAdEditDTO.getId()));
        if (count > 0) {
            return CzgResult.failure("小程序此位置已存在广告");
        }
        return CzgResult.success(shopAdService.update(shopAd, new QueryWrapper().eq(ShopAd::getId, shopAdEditDTO.getId()).eq(ShopAd::getShopId, StpKit.USER.getShopId())));
    }


    /**
     * 小程序广告添加
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "小程序广告",value = "shopAd:add", name = "小程序广告添加")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated(InsertGroup.class) ShopAdDTO shopAdEditDTO) {
        long count = shopAdService.count(new QueryWrapper().eq(ShopAd::getShopId, StpKit.USER.getShopId()).like(ShopAd::getShowPosition, shopAdEditDTO.getShowPosition()));
        if (count > 0) {
            return CzgResult.failure("小程序此位置已存在广告");
        }
        ShopAd shopAd = BeanUtil.copyProperties(shopAdEditDTO, ShopAd.class);
        shopAd.setShopId(StpKit.USER.getShopId());
        return CzgResult.success(shopAdService.save(shopAd));
    }

    /**
     * 小程序广告删除
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "小程序广告",value = "shopAd:del", name = "小程序广告删除")
    @DeleteMapping
    public CzgResult<Boolean> delete(@RequestBody @Validated(UpdateGroup.class) ShopAdDTO shopAd) {
        return CzgResult.success(shopAdService.remove(new QueryWrapper().eq(ShopAd::getShopId, StpKit.USER.getShopId()).eq(ShopAd::getId, shopAd.getId())));
    }
}
