/*
 * 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.finishedGrainSupervision.modular.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.unis.finishedGrainSupervision.modular.entity.DataKcglKcswDefault;
import com.unis.finishedGrainSupervision.modular.entity.vo.DevInputDetailVO;
import com.unis.finishedGrainSupervision.modular.entity.vo.DevInputPageVO;
import com.unis.finishedGrainSupervision.modular.entity.vo.OrgInfoPageVO;
import com.unis.finishedGrainSupervision.modular.entity.vo.StatisticsOurOrInVO;
import com.unis.finishedGrainSupervision.modular.entity.vo.StorageDailycheckDetailVO;
import com.unis.finishedGrainSupervision.modular.entity.vo.StorageDailycheckPageVO;
import com.unis.finishedGrainSupervision.modular.entity.vo.StorehousePageVO;
import com.unis.finishedGrainSupervision.modular.entity.vo.UnitPageVO;
import com.unis.finishedGrainSupervision.modular.mapper.DataKcglKcswDefaultCountMapper;
import com.unis.finishedGrainSupervision.modular.param.DateCrkywCplcrkCountPageParam;
import com.unis.finishedGrainSupervision.modular.param.PgBasicReportInfoPageParam;
import com.unis.finishedGrainSupervision.modular.service.DataKcglKcswDefaultCountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 应急保障-应急配送企业管理表Service接口实现类
 *
 * @author ZeroLiYi
 * @date  2024/06/01 11:37
 **/
@Service
public class DataKcglKcswDefaultCounterviceImpl  implements DataKcglKcswDefaultCountService{


    @Resource
    private DataKcglKcswDefaultCountMapper dataKcglKcswDefaultCountMapper;
    @Override
    public IPage<Map<String,Object>> page(DateCrkywCplcrkCountPageParam dateCrkywCplcrkPageParam ) throws ParseException {
        //条形查询器
        QueryWrapper<DataKcglKcswDefault> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(dateCrkywCplcrkPageParam.getArea())){
            queryWrapper.like("dq.DICT_LABEL",dateCrkywCplcrkPageParam.getArea() );
        }
        if(StringUtils.isNotBlank(dateCrkywCplcrkPageParam.getOrgName())){
            queryWrapper.like("oi.org_name",dateCrkywCplcrkPageParam.getOrgName() );
        }
        if(StringUtils.isNotBlank(dateCrkywCplcrkPageParam.getGrainVarietyName())){
            queryWrapper.eq("pz.enumname",dateCrkywCplcrkPageParam.getGrainVarietyName() );
        }
        if(StringUtils.isNotBlank(dateCrkywCplcrkPageParam.getGrainGradeName())){
            queryWrapper.eq("dj.enumname",dateCrkywCplcrkPageParam.getGrainGradeName() );
        }

        if (dateCrkywCplcrkPageParam.getStartDate() != null && dateCrkywCplcrkPageParam.getEndDate() != null &&!dateCrkywCplcrkPageParam.getEndDate().isEmpty() && !dateCrkywCplcrkPageParam.getStartDate().isEmpty()) {
            String startDate =dateCrkywCplcrkPageParam.getStartDate();
            String endDate =dateCrkywCplcrkPageParam.getEndDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date stadate = sdf.parse(startDate);
            Date enddate = sdf.parse(endDate);

            queryWrapper.between("kc.modify_date", stadate, enddate);
        }

        Page<DataKcglKcswDefault> p =new Page<>(dateCrkywCplcrkPageParam.getCurrent(),dateCrkywCplcrkPageParam.getSize());
        IPage<Map<String,Object>> page = dataKcglKcswDefaultCountMapper.selectMaPage(p,queryWrapper);
        return page;
    }

    /**
     * 粮食等级分类统计
     * @return
     */
    @Override
    public List<Map<String,Object>> statisticsGrainGrade() {
        return dataKcglKcswDefaultCountMapper.statisticsGrainGrade();
    }

    /**
     * 分地区库存统计
     * @return
     */
    @Override
    public List<Map<String,Object>> statisticsArea() {
        return dataKcglKcswDefaultCountMapper.statisticsArea();
    }

    @Override
    public List<Map<String,Object>> statisticsGrainVarietyName() {
        return dataKcglKcswDefaultCountMapper.statisticsGrainVarietyName();
    }

    /**
     * 出入库统计
     * @return
     */
    @Override
    public List<StatisticsOurOrInVO> statisticsOurOrIn(Integer tjnf) {
        List<StatisticsOurOrInVO> statisticsOurOrInVOList =new ArrayList<>();
        StatisticsOurOrInVO statisticsOurVO= new StatisticsOurOrInVO();
        StatisticsOurOrInVO statisticsInVO= new StatisticsOurOrInVO();
        List<Map<String, Object>> inMap =dataKcglKcswDefaultCountMapper.statisticsIn(tjnf);//入库
        List<Map<String, Object>> outMap =dataKcglKcswDefaultCountMapper.statisticsOut(tjnf);//出库
        if(!inMap.isEmpty()){
            statisticsInVO.setMap(inMap);
            statisticsInVO.setName("入库数量");
            statisticsOurOrInVOList.add(statisticsInVO);
        }

        if(!outMap.isEmpty()){
            statisticsOurVO.setMap(outMap);
            statisticsOurVO.setName("出库数量");
            statisticsOurOrInVOList.add(statisticsOurVO);
        }

        return statisticsOurOrInVOList;
    }

    /**
     * 数据统计分析 - 计划执行情况统计
     * @return
     */
    @Override
    public List<Map<String, Object>> statisticsPlan() {
        return dataKcglKcswDefaultCountMapper.statisticsPlan();
    }



    @Override
    public List<Map<String, Object>> statisticsGrainNature() {
        return dataKcglKcswDefaultCountMapper.statisticsGrainNature();
    }

    @Override
    public List<Map<String, Object>> statisticsQualitye() {
        return dataKcglKcswDefaultCountMapper.statisticsQualitye();
    }



    /**
     * 获取成品粮企业信息分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<UnitPageVO> findUnitPage(PgBasicReportInfoPageParam pageParam) {
        //条形查询器
        QueryWrapper<UnitPageVO> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(pageParam.getUnitName())){
            queryWrapper.like("a.dwmc",pageParam.getUnitName() );
        }
        Page<UnitPageVO> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());
        IPage<UnitPageVO> page = dataKcglKcswDefaultCountMapper.findUnitPage(p,queryWrapper);
        return page;
    }

    /**
     * 获取成品粮粮库信息分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<OrgInfoPageVO> findOrgInfoPage(PgBasicReportInfoPageParam pageParam) {
            //条形查询器
            QueryWrapper<OrgInfoPageVO> queryWrapper = new QueryWrapper<>();

            if(StringUtils.isNotBlank(pageParam.getOrgName())){
                queryWrapper.like("b.org_name",pageParam.getOrgName() );
            }
            Page<OrgInfoPageVO> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());
            IPage<OrgInfoPageVO> page = dataKcglKcswDefaultCountMapper.findOrgInfoPage(p,queryWrapper);
            return page;
    }

    /**
     * 获取成品粮仓房信息分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<StorehousePageVO> findStorehousePage(PgBasicReportInfoPageParam pageParam) {
            //条形查询器
            QueryWrapper<StorehousePageVO> queryWrapper = new QueryWrapper<>();

            if(StringUtils.isNotBlank(pageParam.getOrgName())){
                queryWrapper.like("b.org_name",pageParam.getOrgName() );
            }
            Page<StorehousePageVO> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());
            IPage<StorehousePageVO> page = dataKcglKcswDefaultCountMapper.findStorehousePage(p,queryWrapper);
            return page;
    }

    /**
     * 获取设备信息分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<DevInputPageVO> findDevInputPage(PgBasicReportInfoPageParam pageParam) {
            QueryWrapper<DevInputPageVO> queryWrapper = new QueryWrapper<>();

            if(StringUtils.isNotBlank(pageParam.getOrgName())){
                queryWrapper.like("b.org_name",pageParam.getOrgName() );
            }
            Page<DevInputPageVO> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());
            IPage<DevInputPageVO> page = dataKcglKcswDefaultCountMapper.findDevInputPage(p,queryWrapper);
            return page;
    }

    /**
     * 获取设备信息详情
     *
     * @param id
     * @return
     */
    @Override
    public DevInputDetailVO getDevInputDetail(String id) {
        return dataKcglKcswDefaultCountMapper.getDevInputDetail(id);
    }

    @Override
    public List<Map<String, Object>> statisticsTransaction() {
        return  dataKcglKcswDefaultCountMapper.statisticsTransaction();
    }

    @Override
    public List<Map<String, Object>> statisticsIn() {
        return dataKcglKcswDefaultCountMapper.statisticsYearIn();
    }

    @Override
    public List<Map<String, Object>> statisticsStorageFacilities() {
        return dataKcglKcswDefaultCountMapper.statisticsStorageFacilities();
    }




    /**
     * 获取日常检查信息分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<StorageDailycheckPageVO> finDailycheckPage(PgBasicReportInfoPageParam pageParam) {
            QueryWrapper<StorageDailycheckPageVO> queryWrapper = new QueryWrapper<>();

            if(StringUtils.isNotBlank(pageParam.getOrgName())){
                queryWrapper.like("b.org_name",pageParam.getOrgName() );
            }
            if(StringUtils.isNotBlank(pageParam.getSubTypeName())){
                queryWrapper.like("c.enumname",pageParam.getOrgName() );
            }
            Page<StorageDailycheckPageVO> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());
            IPage<StorageDailycheckPageVO> page = dataKcglKcswDefaultCountMapper.findDailycheckPage(p,queryWrapper);
            return page;
    }

    /**
     * 获取日常检查信息详情d
     *
     * @param id
     * @return
     */
    @Override
    public StorageDailycheckDetailVO getDailycheckDetail(String id) {
        return dataKcglKcswDefaultCountMapper.getDailycheckDetail(id);
    }

    @Override
    public List<Map<String, Object>> statisticsIntelligentEquipment() {
        return  dataKcglKcswDefaultCountMapper.statisticsIntelligentEquipment();
    }

}
