package com.czg.controller.admin;

import cn.hutool.core.thread.ThreadUtil;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ShopProdUnitDTO;
import com.czg.product.service.ShopProdUnitService;
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 lombok.AllArgsConstructor;
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/unit")
public class ShopProdUnitController {

    @Resource
    private ShopProdUnitService shopProdUnitService;

    @Resource
    private ShopSyncService shopSyncService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("商品单位-分页")
    //@SaAdminCheckPermission("shopProdUnit:page")
    public CzgResult<Page<ShopProdUnitDTO>> getShopProdUnitPage(ShopProdUnitDTO param) {
        Page<ShopProdUnitDTO> data = shopProdUnitService.getShopProdUnitPage(param);
        return CzgResult.success(data);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @OperationLog("商品单位-列表")
    //@SaAdminCheckPermission("shopProdUnit:list")
    public CzgResult<List<ShopProdUnitDTO>> getShopProdUnitList(ShopProdUnitDTO param) {
        List<ShopProdUnitDTO> data = shopProdUnitService.getShopProdUnitList(param);
        return CzgResult.success(data);
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @OperationLog("商品单位-导出")
    public void exportProduct(ShopProdUnitDTO param, HttpServletResponse response) {
        shopProdUnitService.exportShopProdUnit(param, response);
    }

    /**
     * 详情
     */
    @GetMapping("{id}")
    @OperationLog("商品单位-详情")
    //@SaAdminCheckPermission("shopProdUnit:info")
    public CzgResult<ShopProdUnitDTO> getShopProdUnitById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ShopProdUnitDTO data = shopProdUnitService.getShopProdUnitById(id);
        return CzgResult.success(data);
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("商品单位-新增")
    //@SaAdminCheckPermission("shopProdUnit:add")
    public CzgResult<Void> addShopProdUnit(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) ShopProdUnitDTO dto) {
        Long id = shopProdUnitService.addShopProdUnit(dto);
        asyncToBranchShop(id);
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("商品单位-修改")
    //@SaAdminCheckPermission("shopProdUnit:update")
    public CzgResult<Void> updateShopProdUnit(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) ShopProdUnitDTO dto) {
        shopProdUnitService.updateShopProdUnit(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

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

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

    /**
     * 启用
     */
    @PostMapping("enable/{id}")
    @OperationLog("商品单位-启用")
    //@SaAdminCheckPermission("shopProdUnit:able")
    public CzgResult<Void> enableShopProdUnit(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopProdUnitService.enableShopProdUnit(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.syncUnitBySourceShop(shopId, id, sysUserId);
        });
    }

}