package com.czg.controller.admin;

import cn.hutool.core.thread.ThreadUtil;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ConsGroupDTO;
import com.czg.product.service.ConsGroupService;
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-20
 */
@RestController
@RequestMapping("/admin/product/consGroup")
public class ConsGroupController {

    @Resource
    private ConsGroupService consGroupService;
    @Resource
    private ShopSyncService shopSyncService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("耗材分组-分页")
    //@SaAdminCheckPermission("consGroup:page")
    public CzgResult<Page<ConsGroupDTO>> getConsGroupPage(ConsGroupDTO param) {
        Page<ConsGroupDTO> data = consGroupService.getConsGroupPage(param);
        return CzgResult.success(data);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @OperationLog("耗材分组-列表")
    //@SaAdminCheckPermission("consGroup:list")
    public CzgResult<List<ConsGroupDTO>> getConsGroupList(ConsGroupDTO param) {
        List<ConsGroupDTO> data = consGroupService.getConsGroupList(param);
        return CzgResult.success(data);
    }

    /**
     * 详情
     *
     * @param id 分组id
     */
    @GetMapping("{id}")
    @OperationLog("耗材分组-详情")
    //@SaAdminCheckPermission("consGroup:info")
    public CzgResult<ConsGroupDTO> getConsGroupById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ConsGroupDTO data = consGroupService.getConsGroupById(id);
        return CzgResult.success(data);
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("耗材分组-新增")
    //@SaAdminCheckPermission("consGroup:add")
    public CzgResult<Void> addConsGroup(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) ConsGroupDTO dto) {
        consGroupService.addConsGroup(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("耗材分组-修改")
    //@SaAdminCheckPermission("consGroup:update")
    public CzgResult<Void> updateConsGroup(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) ConsGroupDTO dto) {
        consGroupService.updateConsGroup(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 禁用
     *
     * @param id 分组id
     */
    @PostMapping("disable/{id}")
    @OperationLog("耗材分组-禁用")
    //@SaAdminCheckPermission("consGroup:able")
    public CzgResult<Void> disableConsGroup(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        consGroupService.disableConsGroup(id);
        asyncToBranchShop(id);
        return CzgResult.success();
    }

    /**
     * 启用
     *
     * @param id 分组id
     */
    @PostMapping("enable/{id}")
    @OperationLog("耗材分组-启用")
    //@SaAdminCheckPermission("consGroup:able")
    public CzgResult<Void> enableConsGroup(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        consGroupService.enableConsGroup(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.syncConsGroupBySourceShop(shopId, id, sysUserId);
        });
    }
}