package com.czg.controller.admin;

import cn.hutool.core.thread.ThreadUtil;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ShopProdCategoryDTO;
import com.czg.product.service.ShopProdCategoryService;
import com.czg.product.service.ShopSyncService;
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 jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 店铺商品分类
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-10
 */
@RestController
@RequestMapping("/admin/prod/category")
public class ShopProdCategoryController {

    @Resource
    private ShopProdCategoryService shopProdCategoryService;

    @Resource
    private ShopSyncService shopSyncService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("商品分类-分页")
    public CzgResult<Page<ShopProdCategoryDTO>> getShopProdCategoryPage(ShopProdCategoryDTO param) {
        Page<ShopProdCategoryDTO> data = shopProdCategoryService.getShopProdCategoryPage(param);
        return CzgResult.success(data);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @OperationLog("商品分类-列表")
    //@SaAdminCheckPermission("shopProdCategory:list")
    public CzgResult<List<ShopProdCategoryDTO>> getShopProdCategoryList(ShopProdCategoryDTO param) {
        List<ShopProdCategoryDTO> data = shopProdCategoryService.getShopProdCategoryList(param);
        return CzgResult.success(data);
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @OperationLog("商品分类-导出")
    public void exportShopProdCategory(ShopProdCategoryDTO param, HttpServletResponse response) {
        shopProdCategoryService.exportShopProdCategory(param, response);
    }

    /**
     * 详情
     */
    @GetMapping("{id}")
    @OperationLog("商品分类-详情")
    //@SaAdminCheckPermission("shopProdCategory:info")
    public CzgResult<ShopProdCategoryDTO> getShopProdCategoryById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ShopProdCategoryDTO data = shopProdCategoryService.getShopProdCategoryById(id);
        return CzgResult.success(data);
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("商品分类-新增")
    //@SaAdminCheckPermission("shopProdCategory:add")
    public CzgResult<Void> addShopProdCategory(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) ShopProdCategoryDTO dto) {
        shopProdCategoryService.addShopProdCategory(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("商品分类-修改")
    //@SaAdminCheckPermission("shopProdCategory:update")
    public CzgResult<Void> updateShopProdCategory(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) ShopProdCategoryDTO dto) {
        shopProdCategoryService.updateShopProdCategory(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("{id}")
    @OperationLog("商品分类-删除")
    //@SaAdminCheckPermission("shopProdCategory:delete")
    public CzgResult<Void> deleteShopProdCategory(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopProdCategoryService.deleteShopProdCategory(id);
        return CzgResult.success();
    }

    /**
     * 禁用
     */
    @PostMapping("disable/{id}")
    @OperationLog("商品分类-禁用")
    //@SaAdminCheckPermission("shopProdCategory:able")
    public CzgResult<Void> disableShopProdCategory(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopProdCategoryService.disableShopProdCategory(id);
        asyncToBranchShop(id);
        return CzgResult.success();
    }

    /**
     * 启用
     */
    @PostMapping("enable/{id}")
    @OperationLog("商品分类-启用")
    //@SaAdminCheckPermission("shopProdCategory:able")
    public CzgResult<Void> enableShopProdCategory(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopProdCategoryService.enableShopProdCategory(id);
        asyncToBranchShop(id);
        return CzgResult.success();
    }


    private void asyncToBranchShop(Long id) {
        long shopId = StpKit.USER.getShopId(0L);
        long sysUserId = StpKit.USER.getLoginIdAsLong();
        ThreadUtil.execAsync(() -> shopSyncService.syncCategoryBySourceShop(shopId, id, sysUserId));
    }

}