package com.czg.controller.admin;

import cn.hutool.core.thread.ThreadUtil;
import com.czg.enums.CrudEnum;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ConsInfoDTO;
import com.czg.product.param.ConsInfoParam;
import com.czg.product.param.ConsSubUnitParam;
import com.czg.product.service.ConsInfoService;
import com.czg.product.service.ShopSyncService;
import com.czg.product.vo.ConsStatisticsVo;
import com.czg.product.vo.ConsStockRecord;
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 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/cons")
public class ConsInfoController {

    @Resource
    private ConsInfoService consInfoService;

    @Resource
    private ShopSyncService shopSyncService;

    /**
     * 分页
     */
    @GetMapping("page")
    //@SaAdminCheckPermission("consInfo:page")
    public CzgResult<Page<ConsInfoDTO>> getConsInfoPage(ConsInfoDTO param) {
        Page<ConsInfoDTO> data = consInfoService.getConsInfoPage(param);
        return CzgResult.success(data);
    }

    /**
     * 列表
     */
    @GetMapping("consStock")
    public CzgResult<List<ConsStockRecord>> getConsStockList(Long shopId) {
        return CzgResult.success(consInfoService.getConsStockList(shopId));
    }


    /**
     * 列表
     */
    @GetMapping("list")
    //@SaAdminCheckPermission("consInfo:list")
    public CzgResult<List<ConsInfoDTO>> getConsInfoList(ConsInfoDTO param) {
        List<ConsInfoDTO> data = consInfoService.getConsInfoList(param);
        return CzgResult.success(data);
    }

    /**
     * 详情
     *
     * @param id 耗材信息id
     */
    @GetMapping("{id}")
    //@SaAdminCheckPermission("consInfo:info")
    public CzgResult<ConsInfoDTO> getConsInfoById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ConsInfoDTO data = consInfoService.getConsInfoById(id);
        return CzgResult.success(data);
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("耗材信息-新增")
    //@SaAdminCheckPermission("consInfo:add")
    public CzgResult<Void> addConsInfo(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) ConsInfoDTO dto) {
        consInfoService.addConsInfo(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("耗材信息-修改")
    //@SaAdminCheckPermission("consInfo:update")
    public CzgResult<Void> updateConsInfo(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) ConsInfoDTO dto) {
        consInfoService.updateConsInfo(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 删除
     *
     * @param id 耗材信息id
     */
    //@DeleteMapping("{id}")
    //@OperationLog("耗材信息-删除")
    //@SaAdminCheckPermission("consInfo:delete")
    public CzgResult<Void> deleteConsInfo(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        consInfoService.deleteConsInfo(id);
        return CzgResult.success();
    }

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

    /**
     * 启用
     *
     * @param id 耗材信息id
     */
    @PostMapping("enable/{id}")
    @OperationLog("耗材信息-启用")
    //@SaAdminCheckPermission("consInfo:able")
    public CzgResult<Void> enableConsInfo(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        consInfoService.enableConsInfo(id);
        asyncToBranchShop(id);
        return CzgResult.success();
    }

    /**
     * 库存开关
     *
     * @param id      耗材信息id
     * @param isStock 库存开关 1-开启 0-关闭
     */
    @PostMapping("onOff")
    @OperationLog("耗材信息-库存开关")
    //@SaAdminCheckPermission("consInfo:on-off")
    public CzgResult<Void> onOffConsInfo(@RequestParam Long id, @RequestParam Integer isStock) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        AssertUtil.isNull(isStock, "库存开关值不能为空");
        consInfoService.onOffConsInfo(id, isStock);
        return CzgResult.success();
    }


    /**
     * 编辑副单位
     */
    @PostMapping("modifySubUnit")
    @OperationLog("耗材信息-编辑副单位")
    //@SaAdminCheckPermission("consInfo:update")
    public CzgResult<Void> modifySubUnit(@RequestBody ConsSubUnitParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        if (CrudEnum.ADD.value().equals(param.getModifyFlag())) {
            ValidatorUtil.validateEntity(param, InsertGroup.class);
        }
        consInfoService.modifySubUnit(param);
        asyncToBranchShop(param.getId());
        return CzgResult.success();
    }

    /**
     * 统计
     */
    @GetMapping("statistics")
    //@SaAdminCheckPermission("consInfo:info")
    public CzgResult<ConsStatisticsVo> getConsInfoById(ConsInfoParam param) {
        Long shopId = StpKit.USER.getShopId(0L);
        param.setShopId(shopId);
        ConsStatisticsVo data = consInfoService.statistics(param);
        return CzgResult.success(data);
    }

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

}
