package com.czg.controller.admin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ConsStockFlowDTO;
import com.czg.product.dto.OcrDTO;
import com.czg.product.entity.MkOcrService;
import com.czg.product.param.ConsCheckStockParam;
import com.czg.product.param.ConsInOutStockHeadParam;
import com.czg.product.param.ConsReportDamageParam;
import com.czg.product.param.ConsStockFlowParam;
import com.czg.product.service.ConsStockFlowService;
import com.czg.product.vo.ConsCheckStockRecordVo;
import com.czg.resp.CzgResult;
import com.czg.utils.AssertUtil;
import com.czg.validator.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.List;


/**
 * 耗材进销存
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-21
 */
@RestController
@RequestMapping("/admin/product/stock")
public class ConsStockFlowController {

    @Resource
    private ConsStockFlowService consStockFlowService;
    @Resource
    private MkOcrService ocrService;

    /**
     * 入库单识别
     */
    @PostMapping("/ocr")
    public CzgResult<Integer> ocr(@RequestBody OcrDTO ocrDTO) throws Exception {
        URI uri = new URI(ocrDTO.getUrl());
        URL url = uri.toURL();
        InputStream stream = URLUtil.getStream(url);
        return CzgResult.success(ocrService.ocr(FileUtil.getName(ocrDTO.getUrl()), stream, "cons"));
    }

    /**
     * ocr识别结果
     *
     * @param id ocrId
     * @return 识别结果
     */
    @GetMapping("/ocrResult")
    public CzgResult<ConsInOutStockHeadParam> ocrResult(@RequestParam Long id) {
        return CzgResult.success(ocrService.ocrDetail(id));
    }


    /**
     * 耗材入库
     */
    @PostMapping("in")
    @OperationLog("耗材入库")
    //@SaStaffCheckPermission("yun_xu_hao_cai_ru_ku")
    //@SaAdminCheckPermission("consStockFlow:in")
    public CzgResult<ConsInOutStockHeadParam> inStock(@RequestBody ConsInOutStockHeadParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        ValidatorUtil.validateEntity(param.getBodyList(), DefaultGroup.class);
        return CzgResult.success(consStockFlowService.inStock(param));
    }

    /**
     * 耗材出库
     */
    @PostMapping("out")
    @OperationLog("耗材出库")
    //@SaStaffCheckPermission("yun_xu_hao_cai_chu_ku")
    //@SaAdminCheckPermission("consStockFlow:out")
    public CzgResult<Void> outStock(@RequestBody ConsInOutStockHeadParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        ValidatorUtil.validateEntity(param.getBodyList(), DefaultGroup.class);
        consStockFlowService.outStock(param);
        return CzgResult.success();
    }

    /**
     * 库存盘点记录
     * param conId 耗材ID
     */
    @GetMapping("checkRecord")
    @OperationLog("库存盘点记录")
    //@SaAdminCheckPermission("consStockFlow:check-record")
    public CzgResult<Page<ConsCheckStockRecordVo>> checkStock(@RequestParam Long conId) {
        AssertUtil.isNull(conId, "耗材ID不能为空");
        Page<ConsCheckStockRecordVo> data = consStockFlowService.getCheckStockRecordPage(conId);
        return CzgResult.success(data);
    }

    /**
     * 库存盘点
     */
    @PostMapping("check")
    @OperationLog("库存盘点")
    //@SaStaffCheckPermission("yun_xu_hao_cai_pan_dian")
    //@SaAdminCheckPermission("consStockFlow:check")
    public CzgResult<Void> checkStock(@RequestBody ConsCheckStockParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        consStockFlowService.checkStock(param);
        return CzgResult.success();
    }

    /**
     * 耗材报损
     */
    @PostMapping("reportDamage")
    @OperationLog("耗材报损")
    //@SaAdminCheckPermission("consStockFlow:reportDamage")
    public CzgResult<Void> reportDamage(@RequestBody List<ConsReportDamageParam> params) {
        params.forEach(item -> {
            ValidatorUtil.validateEntity(item, DefaultGroup.class);
            consStockFlowService.reportDamage(item);
        });
        return CzgResult.success();
    }

    /**
     * 耗材库存变动记录
     */
    @GetMapping("flow")
    @OperationLog("耗材库存变动记录")
    //@SaAdminCheckPermission("consStockFlow:flow")
    public CzgResult<Page<ConsStockFlowDTO>> stockFlow(ConsStockFlowParam param) {
        Page<ConsStockFlowDTO> data = consStockFlowService.findConsStockFlowPage(param);
        return CzgResult.success(data);
    }
}
