package com.ruoyi.web.v1.controller;

import cn.hutool.core.date.DateUtil;


import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.web.v1.DTO.EntranceReportDTO;
import com.ruoyi.web.v1.DTO.PurchaserProduceTotalDTO;
import com.ruoyi.web.v1.DTO.SlaughterReportDTO;
import com.ruoyi.web.v1.model.request.*;

import com.ruoyi.web.v1.model.response.*;
import com.ruoyi.web.v1.service.IDataReportService;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.common.model.ExportConfig;
import com.ruoyi.web.common.model.ExportField;
import com.ruoyi.web.common.service.IExcelExportService;
import com.ruoyi.web.common.service.IExportFieldConfigService;
import com.ruoyi.web.v2.entity.JsStockInfoRequest;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 数据报表Controller
 *
 * @author coede
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/app/dataReport")
public class DataReportController extends BaseController {
    @Autowired
    private IDataReportService dataReportService;

    @Autowired
    private IExportFieldConfigService exportFieldConfigService;

    @Autowired
    private IExcelExportService excelExportService;

    /**
     * 获取入场报表
     */
//    // @PreAuthorize("@ss.hasPermi('app:dataReport:getEntranceReport')")
    @GetMapping(value = "/getEntranceReport")
    public TableDataInfo getEntranceReport(ReqEntranceReport reqEntranceReport)
    {
        startPage();
        List<EntranceReportDTO> list = dataReportService.selectEntranceReport(reqEntranceReport);
        return getDataTable(list);
    }

    /**
     * 获取检查检疫无害化报表
     */
//    // @PreAuthorize("@ss.hasPermi('app:dataReport:getInspectionReport')")
    @GetMapping(value = "/getInspectionReport")
    public TableDataInfo getInspectionReport(ReqEntranceReport reqEntranceReport)
    {
        startPage();
        List<EntranceReportDTO> list = dataReportService.selectEntranceReport(reqEntranceReport);
        return getDataTable(list);
    }

    /**
     * 获取生产报表
     */
    // @PreAuthorize("@ss.hasPermi('app:dataReport:getSlaughterReport')")
    @GetMapping(value = "/getSlaughterReport")
    public TableDataInfo getSlaughterReport(ReqSlaughterReport reqSlaughterReport)
    {
        startPage();
        List<SlaughterReportDTO> list = dataReportService.selectSlaughterReport(reqSlaughterReport);
        return getDataTable(list);
    }

    /**
     * 获取称重统计
     */
    // @PreAuthorize("@ss.hasPermi('app:dataReport:getWeightReport')")
    @GetMapping(value = "/getWeightReport")
    public TableDataInfo getWeightReport(ReqGetPorkSideProduceRecord req)
    {
        startPage();
        List<PurchaserProduceTotalDTO> list = dataReportService.selectPurchaserProduceTotal(req);
        return getDataTable(list);
    }

    /**
     * 生猪屠宰情况日汇总表  2025-10-09
     *
     * @param req 查询条件
     * @return 集合
     */
    @GetMapping(value = "/getDailySlaughterReport")
    public TableDataInfo getDailySlaughterReport(GetDailySlaughterReportReq req)
    {
        startPage();
        List<GetDailySlaughterReportResp> list = dataReportService.selectDailySlaughterReport(req);
        return getDataTable(list);
    }

    /**
     * 生猪入场查验情况表  2025-10-15
     *
     * @param req 查询条件
     * @return 集合
     */
    @GetMapping(value = "/getDailyEntranceReport")
    public TableDataInfo getDailyEntranceReport(GetDailyEntranceReportReq req)
    {
        startPage();
        List<GetDailyEntranceReportResp> list = dataReportService.selectDailyEntranceReport(req);
        return getDataTable(list);
    }

    /**
     * 违禁药物检测及屠宰检验情况表  2025-10-10
     *
     * @param req 查询条件
     * @return 集合
     */
    @GetMapping(value = "/getDailyDrugCheckAndSlaughterReport")
    public TableDataInfo getDailyDrugCheckAndSlaughterReport(GetDailyDrugCheckAndSlaughterReportReq req)
    {
        startPage();
        List<GetDailyDrugCheckAndSlaughterReportResp> list = dataReportService.selectDailyDrugCheckAndSlaughterReport(req);
        return getDataTable(list);
    }

    /**
     * 生猪屠宰环节无害化处理表  2025-10-11
     *
     * @param req 查询条件
     * @return 集合
     */
    @GetMapping(value = "/getDailyHarmlessReport")
    public TableDataInfo getDailyHarmlessReport(GetDailyHarmlessReportReq req)
    {
        startPage();
        List<GetDailyHarmlessReportResp> list = dataReportService.selectDailyHarmlessReport(req);
        return getDataTable(list);
    }

    /**
     * 生猪产品出场情况表  2025-10-12
     *
     * @param req 查询条件
     * @return 集合
     */
    @GetMapping(value = "/getDailyOutputReport")
    public TableDataInfo getDailyOutputReport(GetDailyOutputReportReq req)
    {
        startPage();
        List<GetDailyOutputReportResp> list = dataReportService.selectDailyOutputReport(req);
        return getDataTable(list);
    }
    /**
     * 转运
     *
     * @param req 查询条件
     * @return 集合
     */
    @GetMapping(value = "/getZhuanYunReport")
    public TableDataInfo getZhuanYunReport(GetDailyOutputReportReq req)
    {
        startPage();
        List<GetZhuanYunReportResp> list = dataReportService.getZhuanYunReport(req);
        return getDataTable(list);
    }
    /**
     * 召回
     *
     * @param req 查询条件
     * @return 生猪召回
     */
    @GetMapping(value = "/getZhaoHuiReport")
    public TableDataInfo getZhaoHuiReport( GetDailyOutputReportReq req)
    {
        startPage();
        List<GetZhaoHuiResp> list = dataReportService.getZhaoHuiReport(req);
        return getDataTable(list);
    }
    /**
     * 宰前
     *
     * @param req 查询条件
     * @return 宰前
     */
    @GetMapping(value = "/getZaiQianReport")
    public TableDataInfo getZaiQianReport( GetDailyOutputReportReq req)
    {
        startPage();
        List<GetZaiQianResp> list = dataReportService.getZaiQianReport(req);
        return getDataTable(list);
    }

    /**
     * 环境样品
     *
     * @param req 查询条件
     * @return 环境样品
     */
    @GetMapping(value = "/getYangPinReport")
    public TableDataInfo getYangPinReport( GetDailyOutputReportReq req)
    {
        startPage();
        List<GetYangPinResp> list = dataReportService.getYangPinReport(req);
        return getDataTable(list);
    }

    /**
     * 生猪入厂查验情况登记表
     *
     * @param req 查询条件
     * @return 生猪入厂查验情况登记表
     */
    @GetMapping(value = "/getDaiZaiReport")
    public TableDataInfo getDaiZaiReport( GetDailyOutputReportReq req)
    {
        startPage();
        List<JingYangPinResp> list = dataReportService.getDaiZaiReport(req);
        return getDataTable(list);
    }
    /**
     * 生猪入厂查验情况登记表
     *
     * @param req 查询条件
     * @return 生猪入厂查验情况登记表
     */
    @GetMapping(value = "/getRuChangReport")
    public TableDataInfo getRuChangReport( GetDailyOutputReportReq req)
    {
        startPage();
        List<RunChagnResp> list = dataReportService.getRuChangReport(req);
        return getDataTable(list);
    }
    /**
     * 生猪产品出厂记录表
     *
     * @param req 查询条件
     * @return 生猪产品出厂记录表
     */
    @GetMapping(value = "/getChuChangReport")
    public TableDataInfo getChuChangReport( GetDailyOutputReportReq req)
    {
        startPage();
        List<ChuChagnResp> list = dataReportService.getChuChangReport(req);
        return getDataTable(list);
    }
    @GetMapping(value = "/getFuJianReport")
    public TableDataInfo getFuJianReport( GetDailyOutputReportReq req)
    {
        startPage();
        List<FuJianResp> list = dataReportService.getFuJianReport(req);
        return getDataTable(list);
    }


    @GetMapping(value = "/getZaiHouReport")
    public TableDataInfo getZaiHouReport( GetDailyOutputReportReq req)
    {
        startPage();

        List<ZaiHouResp> list = dataReportService.getZaiHouReport(req);
        return getDataTable(list);
    }
    @GetMapping(value = "/getXunChaReport")
    public TableDataInfo getXunChaReport( GetDailyOutputReportReq req)
    {
        startPage();
        Date startTime = req.getStartTime();
        if (ObjectUtil.isNotEmpty(startTime)){
            req.setEndTime(DateUtil.endOfDay(startTime));
            req.setStartTime(DateUtil.beginOfDay(startTime));
        }
        List<XunChaDel> list = dataReportService.getXunChaReport(req);
        List list1 = new ArrayList();
        for (XunChaDel xunChaDel : list) {
            Map map = new HashMap();
            map.put("patrolTime",xunChaDel.getPatrolTime());
            String patrolList = xunChaDel.getPatrolList();
            List<XunChaItem> xunChaItems = JSONUtil.toList(patrolList, XunChaItem.class);
            for (XunChaItem xunChaItem : xunChaItems) {
                map.put(xunChaItem.getName(),xunChaItem.getContent());
            }
            map.put("abnormalCondition",xunChaDel.getAbnormalCondition());
            map.put("dealMethod",xunChaDel.getDealMethod());
            map.put("dealResult",xunChaDel.getDealResult());
            map.put("patrolUser",xunChaDel.getPatrolUser());
            map.put("patrolList",xunChaDel.getPatrolList());
            list1.add(map);
        }
        return getDataTable(list1);
    }
    @GetMapping(value = "/gysReport")
    public TableDataInfo gysReport( GetDailyOutputReportReq req)
    {
        startPage();

        List<GysResp> list = dataReportService.gysReport(req);
        return getDataTable(list);
    }
    @GetMapping(value = "/tuzaiShengChanReport")
    public TableDataInfo tuzaiShengChanReport( GetDailyOutputReportReq req)
    {
        startPage();

        List<TuZaiShengChanResp> list = dataReportService.tuzaiShengChanReport(req);
        return getDataTable(list);
    }

    /**
     * 获取报表字段  2025-10-13
     *
     * @return 集合
     */
    @GetMapping(value = "/getReportFields")
    public AjaxResult getReportFields(@RequestParam(value ="templateName",defaultValue = "") String templateName)
    {
        List<ExportField> list = new ArrayList<>();
        if(Objects.equals(templateName, "DailySlaughterReport")){
           list = exportFieldConfigService.getDailySlaughterReportFields();
        }else if(Objects.equals(templateName, "DailyDrugCheckAndSlaughterReport")){
            list = exportFieldConfigService.getDailyDrugCheckAndSlaughterReportFields();
        }else if(Objects.equals(templateName, "DailyHarmlessReport")){
            list = exportFieldConfigService.getDailyHarmlessReportFields();
        } else if(Objects.equals(templateName, "DailyOutputReport")){
            list = exportFieldConfigService.getDailyOutputReportFields();
        }else if(Objects.equals(templateName, "DailyEntranceReport")){
            list = exportFieldConfigService.getDailyEntranceReportFields();
        }else if (Objects.equals(templateName, "stockReport")){
            list = exportFieldConfigService.getStockReportFields();
        }
        else if(Objects.equals(templateName, "yangPinReport")){ //环境样品检测记录表
            list = exportFieldConfigService.getYangPinReportFields();
        }
        else if(Objects.equals(templateName, "chuChangReport")){ //生猪产品出厂记录表
             list = exportFieldConfigService.getChuChangReportFields();
        }
        else if(Objects.equals(templateName, "zhaoHuiReport")){ //生猪产品召回记录表
             list = exportFieldConfigService.getZhaoHuiReportFields();
        }
        else if(Objects.equals(templateName, "daiZaiReport")){ //生猪待宰静养记录表
             list = exportFieldConfigService.getDaiZaiReportFields();
        }
//        else if(Objects.equals(templateName, "gysReport")){ //供应商评价表
//             list = exportFieldConfigService.getGysReportFields();
//        }
        else if(Objects.equals(templateName, "ruChangReport")){ //生猪入厂查验情况登记表
             list = exportFieldConfigService.getRuChangReportFields();
        }
        else if(Objects.equals(templateName, "zaiHouReport")){ //宰后检验记录表
             list = exportFieldConfigService.getZaiHouReportFields();
        }
        else if(Objects.equals(templateName, "zaiQianReport")){ //生猪宰前检验记录表
             list = exportFieldConfigService.getZaiQianReportFields();
        }
        else if(Objects.equals(templateName, "tuzaiShengChanReport")){ //屠宰生产记录表
             list = exportFieldConfigService.getTuzaiShengChanReportFields();
        }
        else if(Objects.equals(templateName, "zhuanYunReport")){ //无害化暂存转运记录表
             list = exportFieldConfigService.getZhuanYunReportFields();
        }
        else if(Objects.equals(templateName, "xunChaReport")){ //现场巡查记录
             list = exportFieldConfigService.getXunChaReportFields();
        }
        else if(Objects.equals(templateName, "fuJianReport")){ //样品复检
             list = exportFieldConfigService.getFuJianReportFields();
        }
        else{
           return error("不存在的报表类型");
        }
        return success(list);
    }

    /**
     * 根据字段导出报表  2025-10-13
     *
     * @return 集合
     */
    @PostMapping(value = "/exportDailyReport")
    public void  exportDailyReport(HttpServletResponse response,@RequestBody ExportReportReq exportConfig)
    {
        try {
            List<?> dataList = new ArrayList<>();
            if(Objects.equals(exportConfig.getTemplateName(), "DailySlaughterReport")){
                GetDailySlaughterReportReq req =   new GetDailySlaughterReportReq();
                req.setStartTime(exportConfig.getStartTime());
                req.setEndTime(exportConfig.getEndTime());
                dataList = dataReportService.selectDailySlaughterReport(req);
            }else if(Objects.equals(exportConfig.getTemplateName(), "DailyDrugCheckAndSlaughterReport")){
                GetDailyDrugCheckAndSlaughterReportReq req =   new GetDailyDrugCheckAndSlaughterReportReq();
                req.setStartTime(exportConfig.getStartTime());
                req.setEndTime(exportConfig.getEndTime());
                dataList = dataReportService.selectDailyDrugCheckAndSlaughterReport(req);
            }else if(Objects.equals(exportConfig.getTemplateName(), "DailyHarmlessReport")){
                GetDailyHarmlessReportReq req =   new GetDailyHarmlessReportReq();
                req.setStartTime(exportConfig.getStartTime());
                req.setEndTime(exportConfig.getEndTime());
                dataList = dataReportService.selectDailyHarmlessReport(req);
            }else if(Objects.equals(exportConfig.getTemplateName(), "DailyOutputReport")){
                GetDailyOutputReportReq req =   new GetDailyOutputReportReq();
                req.setStartTime(exportConfig.getStartTime());
                req.setEndTime(exportConfig.getEndTime());
                dataList = dataReportService.selectDailyOutputReport(req);
            }else if(Objects.equals(exportConfig.getTemplateName(), "DailyEntranceReport")){
                GetDailyEntranceReportReq req =   new GetDailyEntranceReportReq();
                req.setStartTime(exportConfig.getStartTime());
                req.setEndTime(exportConfig.getEndTime());
                dataList = dataReportService.selectDailyEntranceReport(req);
            } else if(Objects.equals(exportConfig.getTemplateName(), "stockReport")){ //化学用品登记
                dataList = dataReportService.selectStockReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "yangPinReport")){ //环境样品检测记录表
                dataList = dataReportService.selectyangPinReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "chuChangReport")){ //生猪产品出厂记录表
                dataList = dataReportService.selectchuChangReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "zhaoHuiReport")){ //生猪产品召回记录表
                dataList = dataReportService.selectzhaoHuiReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "daiZaiReport")){ //生猪待宰静养记录表
                dataList = dataReportService.selectdaiZaiReport(exportConfig);
            }
//            else if(Objects.equals(exportConfig.getTemplateName(), "gysReport")){ //供应商评价表
//                dataList = dataReportService.selectgysReport(exportConfig);
//            }
            else if(Objects.equals(exportConfig.getTemplateName(), "ruChangReport")){ //生猪入厂查验情况登记表
                dataList = dataReportService.selectruChangReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "zaiHouReport")){ //宰后检验记录表
                dataList = dataReportService.selectzaiHouReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "zaiQianReport")){ //生猪宰前检验记录表
                dataList = dataReportService.selectzaiQianReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "tuzaiShengChanReport")){ //屠宰生产记录表
                dataList = dataReportService.selecttuzaiShengChanReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "zhuanYunReport")){ //无害化暂存转运记录表
                dataList = dataReportService.selectzhuanYunReport(exportConfig);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "xunChaReport")){ //现场巡查记录
                dataList = dataReportService.selectXunChaReport(exportConfig,response);
            }
            else if(Objects.equals(exportConfig.getTemplateName(), "fuJianReport")){ //样品复检
                dataList = dataReportService.selectFuJianReport(exportConfig);
            }
            else{
                throw new RuntimeException("不存在的报表类型");
            }
            excelExportService.exportExcel(dataList, exportConfig, response);
            //return  success("导出成功");
        } catch (Exception e) {
            throw new RuntimeException("导出数据失败", e);
        }

    }
}
