package com.czg.controller.admin;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import com.czg.config.RabbitPublisher;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ProdGroupDTO;
import com.czg.product.service.ProdGroupService;
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 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-17
 */
@RestController
@RequestMapping("/admin/prod/group")
public class ProdGroupController {

    @Resource
    private ProdGroupService prodGroupService;

    @Resource
    private RabbitPublisher rabbitPublisher;

    @Resource
    private ShopSyncService shopSyncService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("商品分组-分页")
    //@SaAdminCheckPermission("prodGroup:page")
    public CzgResult<Page<ProdGroupDTO>> getProdGroupPage(ProdGroupDTO param) {
        Page<ProdGroupDTO> data = prodGroupService.getProdGroupPage(param);
        return CzgResult.success(data);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @OperationLog("商品分组-列表")
    //@SaAdminCheckPermission("prodGroup:list")
    public CzgResult<List<ProdGroupDTO>> getProdGroupList(ProdGroupDTO param) {
        List<ProdGroupDTO> data = prodGroupService.getProdGroupList(param);
        return CzgResult.success(data);
    }

    /**
     * 详情
     *
     * @param id 分组id
     */
    @GetMapping("{id}")
    @OperationLog("商品分组-详情")
    //@SaAdminCheckPermission("prodGroup:info")
    public CzgResult<ProdGroupDTO> getProdGroupById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ProdGroupDTO data = prodGroupService.getProdGroupById(id);
        return CzgResult.success(data);
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("商品分组-新增")
    //@SaAdminCheckPermission("prodGroup:add")
    public CzgResult<Void> addProdGroup(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) ProdGroupDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        prodGroupService.addProdGroup(dto);
        asyncToBranchShop(dto.getId());
        ThreadUtil.execAsync(() -> {
            rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId));
        });
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("商品分组-修改")
    //@SaAdminCheckPermission("prodGroup:update")
    public CzgResult<Void> updateProdGroup(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) ProdGroupDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        prodGroupService.updateProdGroup(dto,true);
        asyncToBranchShop(dto.getId());
        ThreadUtil.execAsync(() -> {
            rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId));
        });
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("update")
    @OperationLog("商品分组-修改")
    //@SaAdminCheckPermission("prodGroup:update")
    public CzgResult<Void> updateProdGroupV2(@RequestBody ProdGroupDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        prodGroupService.updateProdGroup(dto,false);
        asyncToBranchShop(dto.getId());
        ThreadUtil.execAsync(() -> {
            rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId));
        });
        return CzgResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("{id}")
    @OperationLog("商品分组-删除")
    //@SaAdminCheckPermission("prodGroup:delete")
    public CzgResult<Void> deleteProdGroup(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        Long shopId = StpKit.USER.getShopId(0L);
        prodGroupService.deleteProdGroup(shopId, id);
        ThreadUtil.execAsync(() -> {
            rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId));
        });
        return CzgResult.success();
    }

    /**
     * 禁用
     */
    @PostMapping("disable/{id}")
    @OperationLog("商品分组-禁用")
    //@SaAdminCheckPermission("prodGroup:able")
    public CzgResult<Void> disableProdGroup(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        Long shopId = StpKit.USER.getShopId(0L);
        prodGroupService.disableProdGroup(shopId, id);
        asyncToBranchShop(id);
        ThreadUtil.execAsync(() -> {
            rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId));
        });
        return CzgResult.success();
    }

    /**
     * 启用
     */
    @PostMapping("enable/{id}")
    @OperationLog("商品分组-启用")
    //@SaAdminCheckPermission("prodGroup:able")
    public CzgResult<Void> enableProdGroup(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        Long shopId = StpKit.USER.getShopId(0L);
        prodGroupService.enableProdGroup(shopId, id);
        asyncToBranchShop(id);
        ThreadUtil.execAsync(() -> {
            rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId));
        });
        return CzgResult.success();
    }

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