/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.statisticalReport.info.modular.conReportinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.common.exception.CommonException;
import com.unis.emergencySupport.modular.es.entity.EsDeliveryEnterprise;
import com.unis.emergencySupport.modular.es.entity.EsDeliveryEnterpriseGrain;
import com.unis.emergencySupport.modular.es.entity.EsNetworkEntry;
import com.unis.emergencySupport.modular.es.entity.EsNetworkEntryGrain;
import com.unis.emergencySupport.modular.es.entity.EsProcessEnterprise;
import com.unis.emergencySupport.modular.es.entity.EsProcessEnterpriseGrain;
import com.unis.emergencySupport.modular.es.entity.vo.EsNetworkEntryVO;
import com.unis.emergencySupport.modular.es.service.EsDeliveryEnterpriseGrainService;
import com.unis.emergencySupport.modular.es.service.EsDeliveryEnterpriseService;
import com.unis.emergencySupport.modular.es.service.EsNetworkEntryGrainService;
import com.unis.emergencySupport.modular.es.service.EsNetworkEntryService;
import com.unis.emergencySupport.modular.es.service.EsProcessEnterpriseGrainService;
import com.unis.emergencySupport.modular.es.service.EsProcessEnterpriseService;
import com.unis.emergencySupport.modular.esstorageenterprise.entity.BizEsStorageEnterprise;
import com.unis.emergencySupport.modular.esstorageenterprise.service.BizEsStorageEnterpriseService;
import com.unis.emergencySupport.modular.esstorageenterprisegrain.entity.BizEsStorageEnterpriseGrain;
import com.unis.emergencySupport.modular.esstorageenterprisegrain.service.BizEsStorageEnterpriseGrainService;
import com.unis.policy.info.modular.alPolicyFileReport.entity.AlPolicyFileReport;
import com.unis.rotationPlan.info.modular.businessrpreport.util.Grain;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.ConReportinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.CrkData;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.EsNetworkEntryPageReportinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.EsNetworkEntryReportinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.LqData;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.PlanReportinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.QualityInspection;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.QualityInspectionItem;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.ReserveReportDetailsinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.ReserveReportStorehouseinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.ReserveReportinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.entity.vo.StorehouseCapacityReportinfo;
import com.unis.statisticalReport.info.modular.conReportinfo.mapper.ConReportinfoMapper;
import com.unis.statisticalReport.info.modular.conReportinfo.param.EsNetworkEntryReportinfoParam;
import com.unis.statisticalReport.info.modular.conReportinfo.param.PlanReportinfoParam;
import com.unis.statisticalReport.info.modular.conReportinfo.param.ReserveReportinfoParam;
import com.unis.statisticalReport.info.modular.conReportinfo.param.StorehouseCapacityReportinfoParam;
import com.unis.statisticalReport.info.modular.conReportinfo.service.ConReportinfoService;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 统计报表Service接口实现类
 *
 * @author TS
 * @date  2024/05/31 10:02
 **/
@Service
public class ConReportinfoServiceImpl extends ServiceImpl<ConReportinfoMapper, ConReportinfo> implements ConReportinfoService {

    @Resource
    private  ConReportinfoMapper conReportinfoMapper;

    @Autowired
    private EsDeliveryEnterpriseService esDeliveryEnterpriseService;
    @Autowired
    private EsDeliveryEnterpriseGrainService esDeliveryEnterpriseGrainService;

    @Autowired
    private EsNetworkEntryGrainService esNetworkEntryGrainService;
    @Autowired
    private EsNetworkEntryService esNetworkEntryService;

    @Autowired
    private EsProcessEnterpriseGrainService esProcessEnterpriseGrainService;
    @Autowired
    private EsProcessEnterpriseService esProcessEnterpriseService;

    @Autowired
    private BizEsStorageEnterpriseService bizEsStorageEnterpriseService;
    @Autowired
    private BizEsStorageEnterpriseGrainService bizEsStorageEnterpriseGrainService;
    /**
     * 获取储备统计图表数据
     *
     * @param param
     * @return
     */
    @Override
    public List<ReserveReportinfo> getReserveChartDatas(ReserveReportinfoParam param) {
        if (param.getType() == null || param.getType() <= 0){
            throw new CommonException("统计图表类型错误，type值为：{}",  param.getType());
        }
        if (param.getType() == 3 && StringUtils.isBlank(param.getPzName())){
            throw new CommonException("品种名称不能为空");
        }
        return conReportinfoMapper.getReserveChartDatas(param);
    }

    /**
     * 获取储备统计图表-仓房数据
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<ReserveReportStorehouseinfo> findStorehousePage(ReserveReportinfoParam pageParam) {
            QueryWrapper<ReserveReportStorehouseinfo> queryWrapper = new QueryWrapper<>();
            //分页相关
            if (StringUtils.isNotBlank(pageParam.getOrgName())) {
                queryWrapper.like("d.org_name", pageParam.getOrgName());
            }
            if (StringUtils.isNotBlank(pageParam.getStorehouseName())) {
                queryWrapper.like("e.storehouse_name", pageParam.getStorehouseName());
            }
            if (StringUtils.isNotBlank(pageParam.getPz())) {
                queryWrapper.eq("a.pz", pageParam.getPz());
            }
            if (StringUtils.isNotBlank(pageParam.getHwxz())) {
                queryWrapper.eq("a.hwxz", pageParam.getHwxz());
            }
        Page<ReserveReportStorehouseinfo> p = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        IPage<ReserveReportStorehouseinfo> page = conReportinfoMapper.findStorehousePage(p, queryWrapper);
        return page;
    }

    /**
     * 获取储备统计图表-仓房数据详情
     *
     * @param orgId
     * @param storehouseId
     * @param warehouseId
     * @return
     */
    @Override
    public ReserveReportDetailsinfo getStorehouseDetails(String orgId, String storehouseId, String warehouseId) {
        ReserveReportDetailsinfo detailsinfo = new ReserveReportDetailsinfo();
        //粮温数据
        LqData lqData = conReportinfoMapper.findLqData(orgId,storehouseId);
        detailsinfo.setLqData(lqData);
        //质检记录
        //1.查询主表记录，时间最近的一条质检数据
        QualityInspection qualityInspection = conReportinfoMapper.findQualityInspection( orgId,warehouseId);
        //2.查询从表数据
        if (qualityInspection != null){
            List<QualityInspectionItem> itemList = conReportinfoMapper.findQualityInspectionItems(qualityInspection.getId());
            qualityInspection.setList(itemList);
        }
        detailsinfo.setQualityInspection(qualityInspection);
        //3.出入库记录
        List<CrkData> crkDataList = conReportinfoMapper.findCrkDataList(orgId,warehouseId);
        detailsinfo.setCrkDataList(crkDataList);
        return detailsinfo;
    }

    /**
     * 获取仓容统计图表数据
     *
     * @return
     */
    @Override
    public List<StorehouseCapacityReportinfo> getStorehouseChartDatas() {
        return conReportinfoMapper.getStorehouseChartDatas();
    }

    /**
     * 获取仓容统计图表-仓房数据
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<StorehouseCapacityReportinfo> findStorehouseCapacityPage(StorehouseCapacityReportinfoParam pageParam) {
        QueryWrapper<StorehouseCapacityReportinfo> queryWrapper = new QueryWrapper<>();
        //分页相关
            if (StringUtils.isNotBlank(pageParam.getOrgName())) {
                queryWrapper.like("b.org_name ", pageParam.getOrgName());
            }
            if (StringUtils.isNotBlank(pageParam.getStorehouseName())) {
                queryWrapper.like("a.storehouse_name ", pageParam.getStorehouseName());
            }
            if (StringUtils.isNotBlank(pageParam.getUnitName())) {
                queryWrapper.eq("c.dwmc", pageParam.getUnitName());
            }
            Page<StorehouseCapacityReportinfo> p = new Page<>(pageParam.getCurrent(), pageParam.getSize());
            IPage<StorehouseCapacityReportinfo> page = conReportinfoMapper.findStorehouseCapacityPage(p, queryWrapper);
            return page;
    }

    /**
     * 获取应急保障报表详情
     *
     * @return
     */
    @Override
    public EsNetworkEntryReportinfo getEsNetworkEntryDetails(EsNetworkEntryReportinfoParam esNetworkEntryReportinfoParam) {
        EsNetworkEntryReportinfo reportinfo = new EsNetworkEntryReportinfo();
        if (StringUtils.isBlank(esNetworkEntryReportinfoParam.getId())){
            throw new CommonException("参数Id为null");
        }
        if (esNetworkEntryReportinfoParam.getType() == null){
            throw new CommonException("参数type为");
        }
        String id = esNetworkEntryReportinfoParam.getId();
        Integer type = esNetworkEntryReportinfoParam.getType();
        if (type == 3) {
            //应急保障-应急配送企业管理
            EsDeliveryEnterprise esDeliveryEnterprise = esDeliveryEnterpriseService.getById(id);
            if (esDeliveryEnterprise != null) {
                QueryWrapper<EsDeliveryEnterpriseGrain> grainQueryWrapper = new QueryWrapper<>();
                grainQueryWrapper.lambda().eq(EsDeliveryEnterpriseGrain::getEsDeliveryEnterpriseId, esDeliveryEnterprise.getId());
                List<EsDeliveryEnterpriseGrain> list = esDeliveryEnterpriseGrainService.list(grainQueryWrapper);
                esDeliveryEnterprise.setEsDeliveryEnterpriseGrainList(list);

                reportinfo.setEsDeliveryEnterprise(esDeliveryEnterprise);
            }
        }
        if (type == 1) {
//        应急保障-应急供应网点管理
            EsNetworkEntry esNetworkEntry = esNetworkEntryService.getById(id);
            if (esNetworkEntry != null) {
                QueryWrapper<EsNetworkEntryGrain> grainQueryWrapper = new QueryWrapper<>();
                grainQueryWrapper.lambda().eq(EsNetworkEntryGrain::getEsNetworkEntryId, esNetworkEntry.getId());
                List<EsNetworkEntryGrain> list = esNetworkEntryGrainService.list(grainQueryWrapper);
                esNetworkEntry.setEsNetworkEntryGrainList(list);

                reportinfo.setEsNetworkEntry(esNetworkEntry);
            }
        }

        if (type == 2) {
//        应急保障-应急加工企业管理
            EsProcessEnterprise esProcessEnterprise = esProcessEnterpriseService.getById(id);
            if (esProcessEnterprise != null) {
                QueryWrapper<EsProcessEnterpriseGrain> grainQueryWrapper = new QueryWrapper<>();
                grainQueryWrapper.lambda().eq(EsProcessEnterpriseGrain::getEsProcessEnterpriseId, esProcessEnterprise.getId());
                List<EsProcessEnterpriseGrain> list = esProcessEnterpriseGrainService.list(grainQueryWrapper);
                esProcessEnterprise.setEsProcessEnterpriseGrainList(list);

                reportinfo.setEsProcessEnterprise(esProcessEnterprise);
            }
        }
        if (type == 4) {
//        应急保障-应急仓储企业管理
            BizEsStorageEnterprise esStorageEnterprise = bizEsStorageEnterpriseService.getById(id);
            if (esStorageEnterprise != null) {
                QueryWrapper<BizEsStorageEnterpriseGrain> grainQueryWrapper = new QueryWrapper<>();
                grainQueryWrapper.lambda().eq(BizEsStorageEnterpriseGrain::getEsStorageEnterpriseId, esStorageEnterprise.getId());
                List<BizEsStorageEnterpriseGrain> list = bizEsStorageEnterpriseGrainService.list(grainQueryWrapper);
                esStorageEnterprise.setBizEsStorageEnterpriseGrain(list);

                reportinfo.setEsStorageEnterprise(esStorageEnterprise);
            }
        }
        return reportinfo;
    }

    /**
     * 获取应急保障报表分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<EsNetworkEntryPageReportinfo> findEsNetworkEntryPage(EsNetworkEntryReportinfoParam pageParam) {
            QueryWrapper<EsNetworkEntryPageReportinfo> queryWrapper = new QueryWrapper<>();
            //分页相关
                if(StringUtils.isNotBlank(pageParam.getEnterpriseName())){//名称
                    queryWrapper.like("enterprise_name",pageParam.getEnterpriseName());
                }
            Page<EsNetworkEntryPageReportinfo> p = new Page<>(pageParam.getCurrent(), pageParam.getSize());
            IPage<EsNetworkEntryPageReportinfo> page = conReportinfoMapper.findEsNetworkEntryPage(p, queryWrapper);
            return page;
    }

    /**
     * 获取收购报表分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<PlanReportinfo> findBuyPlanPage(PlanReportinfoParam pageParam) {
            QueryWrapper<PlanReportinfo> queryWrapper = new QueryWrapper<>();
            //分页相关
            if(StringUtils.isNotBlank(pageParam.getOrgName())){//名称
                queryWrapper.like("orgName",pageParam.getOrgName());
            }
            Page<PlanReportinfo> p = new Page<>(pageParam.getCurrent(), pageParam.getSize());
            IPage<PlanReportinfo> page = conReportinfoMapper.findBuyPlanPage(p, queryWrapper);
            return page;
    }

    /**
     * 获取收购报表详情
     *
     * @param id
     * @return
     */
    @Override
    public PlanReportinfo getBuyPlanDetails(String id) {
            QueryWrapper<PlanReportinfo> queryWrapper = new QueryWrapper<>();
            //分页相关
                queryWrapper.eq("id",id);
            Page<PlanReportinfo> p = new Page<>(1, 1);
            IPage<PlanReportinfo> page = conReportinfoMapper.findBuyPlanPage(p, queryWrapper);
            List<PlanReportinfo> list = page.getRecords();
            return list.isEmpty()?null:list.get(0);
    }

    /**
     * 获取当日购销报表分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<PlanReportinfo> findCurrentPlanPage(PlanReportinfoParam pageParam) {
            QueryWrapper<PlanReportinfo> queryWrapper = new QueryWrapper<>();
            //分页相关
            if(StringUtils.isNotBlank(pageParam.getOrgName())){//名称
                queryWrapper.like("orgName",pageParam.getOrgName());
            }
            if(pageParam.getPlanType() != null){//计划类型
                queryWrapper.like("planType",pageParam.getPlanType());
            }
            Page<PlanReportinfo> p = new Page<>(pageParam.getCurrent(), pageParam.getSize());
            IPage<PlanReportinfo> page = conReportinfoMapper.findCurrentPlanPage(p, queryWrapper);
            return page;
    }

    /**
     * 获取当日购销报表详情
     *
     * @param id
     * @param warehouseId
     * @return
     */
    @Override
    public PlanReportinfo getCurrentPlanDetails(String id, String warehouseId) {
            QueryWrapper<PlanReportinfo> queryWrapper = new QueryWrapper<>();
            //分页相关
            queryWrapper.eq("id",id);
            queryWrapper.eq("warehouseId",warehouseId);
            Page<PlanReportinfo> p = new Page<>(1, 1);
            IPage<PlanReportinfo> page = conReportinfoMapper.findCurrentPlanPage(p, queryWrapper);
            List<PlanReportinfo> list = page.getRecords();
            return list.isEmpty()?null:list.get(0);
    }

}
