package com.ruoyi.web.controller.app;

import com.ruoyi.app.DTO.PorkProduceDTO;
import com.ruoyi.app.domain.EntranceBatch;
import com.ruoyi.app.domain.request.ProductTraceParam;
import com.ruoyi.app.domain.request.ReqEntranceAndDistribute;
import com.ruoyi.app.domain.request.TimeRangeParam;
import com.ruoyi.app.service.IDataStatisticsService;
import com.ruoyi.app.service.IEntranceBatchService;
import com.ruoyi.app.service.IProductTraceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品追溯
 *
 * @author coede
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/app/productTrace")
public class ProductTraceController extends BaseController {
    @Autowired
    private IProductTraceService productTraceService;
    @Autowired
    private IEntranceBatchService entranceBatchService;

    /**
     * 获取追溯信息信息
     */
    @PreAuthorize("@ss.hasPermi('app:productTrace:getProductTrace')")
    @GetMapping(value = "/getProductTrace")
    public AjaxResult getProductTrace(ProductTraceParam productTraceParam) {
        if (ObjectUtils.allFieldsNull(productTraceParam)) {
            //获取最近的一条
            EntranceBatch entranceBatch = entranceBatchService.selectLatelyEntrance();
            if (entranceBatch == null) {
                return error("暂无入场信息");
            }
            productTraceParam = new ProductTraceParam();
            productTraceParam.setAnimalCertNo(entranceBatch.getAnimalCertNo());
        }
        return success(productTraceService.selectProductTrace(productTraceParam));
    }

    /**
     * 获取分割生产信息
     */
    @PreAuthorize("@ss.hasPermi('app:productTrace:getPorkProduce')")
    @GetMapping(value = "/getPorkProduce")
    public AjaxResult getPorkProduce(ReqEntranceAndDistribute reqEntranceAndDistribute){
        return success(productTraceService.selectPorkProduceByBatch(reqEntranceAndDistribute));
    }

}