package com.czg.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ShopProdSpecDTO;
import com.czg.product.service.ShopProdSpecService;
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.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品规格
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-13
 */
@RestController
@RequestMapping("/admin/prod/spec")
public class ShopProdSpecController {

    @Resource
    private ShopProdSpecService shopProdSpecService;

    @Resource
    private ShopSyncService shopSyncService;


    @GetMapping("list")
    @OperationLog("商品规格-列表")
    //@SaAdminCheckPermission("shopProductSpec:list")
    public CzgResult<List<ShopProdSpecDTO>> getShopProdSpecList(ShopProdSpecDTO param) {
        List<ShopProdSpecDTO> data = shopProdSpecService.getShopProdSpecList(param);
        return CzgResult.success(data);
    }

    @GetMapping("{id}")
    @OperationLog("商品规格-详情")
    //@SaAdminCheckPermission("shopProductSpec:info")
    public CzgResult<ShopProdSpecDTO> getShopProdSpecById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ShopProdSpecDTO data = shopProdSpecService.getShopProdSpecById(id);
        return CzgResult.success(data);
    }

    @PostMapping
    @OperationLog("商品规格-新增")
    //@SaAdminCheckPermission("shopProductSpec:add")
    public CzgResult<Void> addShopProdSpec(@RequestBody ShopProdSpecDTO dto) {
        //效验数据
        ValidatorUtil.validateEntity(dto, InsertGroup.class, DefaultGroup.class);
        shopProdSpecService.addShopProdSpec(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    @PutMapping
    @OperationLog("商品规格-修改")
    //@SaAdminCheckPermission("shopProductSpec:update")
    public CzgResult<Void> updateShopProdSpec(@RequestBody ShopProdSpecDTO dto) {
        //效验数据
        ValidatorUtil.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        shopProdSpecService.updateShopProdSpec(dto);
        List<Long> affectedIdList = dto.getAffectedIdList();
        if (CollUtil.isNotEmpty(affectedIdList)) {
            affectedIdList.forEach(this::asyncToBranchShop);
        }
        return CzgResult.success();
    }

    @DeleteMapping("{id}")
    @OperationLog("商品规格-删除")
    //@SaAdminCheckPermission("shopProductSpec:delete")
    public CzgResult<Void> deleteShopProdSpec(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopProdSpecService.deleteShopProdSpec(id);
        return CzgResult.success();
    }

    @PostMapping("disable/{id}")
    @OperationLog("商品规格-禁用")
    //@SaAdminCheckPermission("shopProductSpec:able")
    public CzgResult<Void> disableShopProdSpec(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopProdSpecService.disableShopProdSpec(id);
        asyncToBranchShop(id);
        return CzgResult.success();
    }

    @PostMapping("enable/{id}")
    @OperationLog("商品规格-启用")
    //@SaAdminCheckPermission("shopProductSpec:able")
    public CzgResult<Void> enableShopProdSpec(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopProdSpecService.enableShopProdSpec(id);
        asyncToBranchShop(id);
        return CzgResult.success();
    }

    @PostMapping("quickAdd")
    @OperationLog("商品规格-快捷添加")
    //@SaAdminCheckPermission("shopProductSpec:add")
    public CzgResult<Void> quickAddShopProdSpec(@RequestBody ShopProdSpecDTO dto) {
        shopProdSpecService.quickAddShopProdSpec(dto);
        List<Long> affectedIdList = dto.getAffectedIdList();
        if (CollUtil.isNotEmpty(affectedIdList)) {
            affectedIdList.forEach(this::asyncToBranchShop);
        }
        return CzgResult.success();
    }

    @GetMapping("/tree/{id}")
    @OperationLog("商品规格-快捷编辑详情")
    //@SaAdminCheckPermission("shopProductSpec:info")
    public CzgResult<ShopProdSpecDTO> getShopProdSpecTreeById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ShopProdSpecDTO data = shopProdSpecService.getShopProdSpecTreeById(id);
        return CzgResult.success(data);
    }

    @PutMapping("quickUpdate")
    @OperationLog("商品规格-快捷更新")
    //@SaAdminCheckPermission("shopProductSpec:update")
    public CzgResult<Void> quickUpdateShopProdSpec(@RequestBody ShopProdSpecDTO dto) {
        shopProdSpecService.quickUpdateShopProdSpec(dto);
        List<Long> affectedIdList = dto.getAffectedIdList();
        if (CollUtil.isNotEmpty(affectedIdList)) {
            affectedIdList.forEach(this::asyncToBranchShop);
        }
        return CzgResult.success();
    }

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