package com.wms_main.excel.easypoi.service.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms_main.dao.*;
import com.wms_main.excel.easypoi.excelTemplate.*;
import com.wms_main.excel.easypoi.service.IExportExcelEasyPoi;
import com.wms_main.excel.easypoi.service.base.impl.BaseExportExcelEasyPoi;
import com.wms_main.model.dto.query.*;
import com.wms_main.model.po.*;
import com.wms_main.repository.utils.StringUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ExportExcelEasyPoi extends BaseExportExcelEasyPoi implements IExportExcelEasyPoi {
    private final ITAppDbsService tAppDbsService;// DBS服务
    private final ITAppProductService tAppProductService;// 总成服务
    private final ITAppProductExtendService tAppProductExtendService;// 配对关系服务
    private final ITAppSingleProductService tAppSingleProductService;// 单片服务
    private final ITAppWorkService appWorkService;// 工作服务
    private final ITAppWorkRecordService appWorkRecordService;// 工作记录服务
    private final ITAppKanbanService appKanbanService;// 看板服务

    /**
     * 导出DBS计划实现
     * @param dbsQuery 查询条件
     * @param response 响应
     * @throws IOException 异常
     */
    @Override
    public void doExportDbs(DbsQuery dbsQuery, HttpServletResponse response) throws IOException {
        if (dbsQuery == null) {
            throw new IOException("参数不能为空");
        }
        // DBS列表
        List<TAppDbs> dbsList = tAppDbsService.selectList(BeanUtil.copyProperties(dbsQuery, TAppDbs.class));
        // excel模版列表
        List<DbsExcelTemplate> resultList = new ArrayList<>();
        if (!dbsList.isEmpty()) {
            for (TAppDbs dbs : dbsList) {
                resultList.add(new DbsExcelTemplate(
                        dbs.getWorkOrder(),
                        dbs.getProductIdOrigin(),
                        dbs.getTestPlanStartDate(),
                        dbs.getTestPlanFinishDate(),
                        dbs.getPntPlanStartDate(),
                        dbs.getPntPlanFinishDate(),
                        dbs.getPlanProduction(),
                        dbs.getEquipmentName(),
                        dbs.getWorkShift(),
                        dbs.getFamily(),
                        dbs.getTestNo(),
                        dbs.getKittingRemark(),
                        dbs.getRemark()
                ));
            }
        } else {
            resultList.add(new DbsExcelTemplate());
        }
        doWriteExcel("DBS计划", response, resultList, DbsExcelTemplate.class);
    }

    /**
     * 导出配料单
     * @param kittingBomQuery 查询条件
     * @param response 响应
     * @throws IOException 异常
     */
    @Override
    public void doExportKittingBom(KittingBomQuery kittingBomQuery, HttpServletResponse response) throws IOException {
        if (kittingBomQuery == null || kittingBomQuery.getBomType() == null) {
            throw new IOException("参数不能为空");
        }
        // 1. 查询非服务件
        if (kittingBomQuery.getBomType() == 1) {
            List<TAppProduct> productList = tAppProductService.list(new LambdaQueryWrapper<TAppProduct>()
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getModel()), TAppProduct::getModel, kittingBomQuery.getModel())
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getProductId()), TAppProduct::getProductId, kittingBomQuery.getProductId())
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getGoodsId()), TAppProduct::getGoodsId, kittingBomQuery.getGoodsId())
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getBoxNo()), TAppProduct::getBoxNo, kittingBomQuery.getBoxNo()));
            // excel模版列表
            List<ProductExcelTemplate> resultList = new ArrayList<>();
            if (!productList.isEmpty()) {
                for (TAppProduct product : productList) {
                    resultList.add(new ProductExcelTemplate(
                            product.getModel(),
                            product.getProductId(),
                            product.getGoodsId(),
                            product.getGoodsDescription(),
                            product.getQuantity1Pair(),
                            product.getBoxNo(),
                            product.getQuantityOfPair()
                    ));
                }
            } else {
                resultList.add(new ProductExcelTemplate());
            }
            doWriteExcel("非服务件配料单", response, resultList, ProductExcelTemplate.class);
        } else if (kittingBomQuery.getBomType() == 2) {
            // 服务件配料单
            List<TAppSingleProduct> singleProductList = tAppSingleProductService.list(new LambdaQueryWrapper<TAppSingleProduct>()
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getModel()), TAppSingleProduct::getModel, kittingBomQuery.getModel())
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getProductId()), TAppSingleProduct::getProductId, kittingBomQuery.getProductId())
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getGoodsId()), TAppSingleProduct::getGoodsId, kittingBomQuery.getGoodsId())
                    .like(StringUtils.isNotEmpty(kittingBomQuery.getBoxNo()), TAppSingleProduct::getBoxNo, kittingBomQuery.getBoxNo()));
            // excel模版列表
            List<SingleProductExcelTemplate> resultList = new ArrayList<>();
            if (!singleProductList.isEmpty()) {
                for (TAppSingleProduct singleProduct : singleProductList) {
                    resultList.add(new SingleProductExcelTemplate(
                            singleProduct.getModel(),
                            singleProduct.getProductId(),
                            singleProduct.getGoodsId(),
                            singleProduct.getGoodsDescription(),
                            singleProduct.getQuantity1Pair(),
                            singleProduct.getBoxNo(),
                            singleProduct.getQuantityOfPair()
                    ));
                }
            } else {
                resultList.add(new SingleProductExcelTemplate());
            }
            doWriteExcel("服务件配料单", response, resultList, SingleProductExcelTemplate.class);
        } else {
            throw new IOException("配料单类型不在设定值范围内。");
        }
    }

    /**
     * 导出配对关系实现
     * @param kittingBomRelationQuery 查询条件
     * @param response 响应
     * @throws IOException 异常
     */
    @Override
    public void doExportKittingBomRelations(KittingBomRelationQuery kittingBomRelationQuery, HttpServletResponse response) throws IOException {
        if (kittingBomRelationQuery == null) {
            throw new IOException("参数不能为空");
        }
        // 配对关系列表
        List<TAppProductExtend> extendList = tAppProductExtendService.list(new LambdaQueryWrapper<TAppProductExtend>()
                .like(StringUtils.isNotEmpty(kittingBomRelationQuery.getModel()), TAppProductExtend::getModel, kittingBomRelationQuery.getModel())
                .like(StringUtils.isNotEmpty(kittingBomRelationQuery.getProductId()), TAppProductExtend::getProductId, kittingBomRelationQuery.getProductId())
                .like(StringUtils.isNotEmpty(kittingBomRelationQuery.getSingleProductId()), TAppProductExtend::getSingleProductId, kittingBomRelationQuery.getSingleProductId()));
        // excel模版列表
        List<ProductExtendExcelTemplate> resultList = new ArrayList<>();
        if (!extendList.isEmpty()) {
            for (TAppProductExtend bomRelation : extendList) {
                resultList.add(new ProductExtendExcelTemplate(
                        bomRelation.getModel(),
                        bomRelation.getProductId(),
                        bomRelation.getSingleProductId()
                ));
            }
        } else {
            resultList.add(new ProductExtendExcelTemplate());
        }
        doWriteExcel("配对关系", response, resultList, ProductExtendExcelTemplate.class);
    }

    /**
     * 导出工作详情洗了洗
     * @param workSummaryQuery 查询条件
     * @param response 响应
     * @throws IOException 异常
     */
    @Override
    public void doExportWorkSummaryExcel(WorkSummaryQuery workSummaryQuery, HttpServletResponse response) throws IOException {
        if (workSummaryQuery == null || workSummaryQuery.getSummaryType() == null) {
            throw new IOException("查询参数为空，数据类型必须。");
        }
        if (workSummaryQuery.getSummaryType() == 1) {
            // 未关闭订单
            List<TAppWork> poList = appWorkService.list(new LambdaQueryWrapper<TAppWork>()
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getWorkOrder()), TAppWork::getWorkOrder, workSummaryQuery.getWorkOrder())
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getProductId()), TAppWork::getProductId, workSummaryQuery.getProductId())
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getSingleProductId()), TAppWork::getSingleProductId, workSummaryQuery.getSingleProductId())
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getBoxNo()), TAppWork::getBoxNo, workSummaryQuery.getBoxNo())
                    .eq(workSummaryQuery.getWorkStatus() != null, TAppWork::getWorkStatus, workSummaryQuery.getWorkStatus())
                    .eq(workSummaryQuery.getLackStatus() != null, TAppWork::getLackStatus, workSummaryQuery.getLackStatus()));
            // excel模版列表
            List<KateWorkExcelTemplate> resultList = new ArrayList<>();
            if (!poList.isEmpty()) {
                for (TAppWork po : poList) {
                    resultList.add(new KateWorkExcelTemplate(
                            po.getWorkIndex(),
                            po.getWorkOrder(),
                            po.getPlanStartDate(),
                            po.getProductId(),
                            po.getSingleProductId(),
                            po.getBoxNo(),
                            po.getGoodsId(),
                            po.getNeedNum(),
                            po.getDistributeNum(),
                            po.getFinishNum(),
                            po.getWorkStand(),
                            po.getWorkStatus(),
                            po.getWorkUser(),
                            po.getCreateTime(),
                            po.getFinishTime(),
                            po.getLackStatus(),
                            po.getIsOut(),
                            po.getProductType()
                    ));
                }
            } else {
                resultList.add(new KateWorkExcelTemplate());
            }
            doWriteExcel("未关闭工作列表", response, resultList, KateWorkExcelTemplate.class);
        } else if (workSummaryQuery.getSummaryType() == 2) {
            // 已关闭订单
            List<TAppWorkRecord> poList = appWorkRecordService.list(new LambdaQueryWrapper<TAppWorkRecord>()
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getWorkOrder()), TAppWorkRecord::getWorkOrder, workSummaryQuery.getWorkOrder())
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getProductId()), TAppWorkRecord::getProductId, workSummaryQuery.getProductId())
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getSingleProductId()), TAppWorkRecord::getSingleProductId, workSummaryQuery.getSingleProductId())
                    .like(StringUtils.isNotEmpty(workSummaryQuery.getBoxNo()), TAppWorkRecord::getBoxNo, workSummaryQuery.getBoxNo())
                    .eq(workSummaryQuery.getWorkStatus() != null, TAppWorkRecord::getWorkStatus, workSummaryQuery.getWorkStatus())
                    .eq(workSummaryQuery.getLackStatus() != null, TAppWorkRecord::getLackStatus, workSummaryQuery.getLackStatus()));
            // excel模版列表
            List<KateWorkExcelTemplate> resultList = new ArrayList<>();
            if (!poList.isEmpty()) {
                for (TAppWorkRecord po : poList) {
                    resultList.add(new KateWorkExcelTemplate(
                            po.getWorkIndex(),
                            po.getWorkOrder(),
                            po.getPlanStartDate(),
                            po.getProductId(),
                            po.getSingleProductId(),
                            po.getBoxNo(),
                            po.getGoodsId(),
                            po.getNeedNum(),
                            po.getFinishNum(),
                            po.getFinishNum(),
                            po.getWorkStand(),
                            po.getWorkStatus(),
                            po.getWorkUser(),
                            po.getCreateTime(),
                            po.getFinishTime(),
                            po.getLackStatus(),
                            po.getIsOut(),
                            po.getProductType()
                    ));
                }
            } else {
                resultList.add(new KateWorkExcelTemplate());
            }
            doWriteExcel("已关闭工作列表", response, resultList, KateWorkExcelTemplate.class);
        } else {
            throw new IOException("数据类型不在设定值范围内。");
        }
    }

    /**
     * 导出看板数据 实现
     * @param kanbanQuery 查询条件
     * @param response 响应
     * @throws IOException 异常
     */
    @Override
    public void doExportKanbanExcel(KanbanQuery kanbanQuery, HttpServletResponse response) throws IOException {
        if (kanbanQuery == null) {
            throw new IOException("参数不能为空");
        }
        // 配对关系列表
        List<TAppKanban> kanbanList = appKanbanService.list(new LambdaQueryWrapper<TAppKanban>()
                .like(StringUtils.isNotEmpty(kanbanQuery.getGoodsId()), TAppKanban::getGoodsId, kanbanQuery.getGoodsId())
                .like(StringUtils.isNotEmpty(kanbanQuery.getKanbanId()), TAppKanban::getKanbanId, kanbanQuery.getKanbanId())
                .eq(kanbanQuery.getKanbanStatus() != null, TAppKanban::getKanbanStatus, kanbanQuery.getKanbanStatus()));
        // excel模版列表
        List<KanbanExcelTemplate> resultList = new ArrayList<>();
        if (!kanbanList.isEmpty()) {
            for (TAppKanban kanban : kanbanList) {
                resultList.add(new KanbanExcelTemplate(
                        kanban.getGoodsId(),
                        kanban.getKanbanId(),
                        kanban.getKanbanStatus() == 1 ? "满" : "空"
                ));
            }
        } else {
            resultList.add(new KanbanExcelTemplate());
        }
        doWriteExcel("看板需求", response, resultList, KanbanExcelTemplate.class);
    }
}
