/*
 * 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.grainReserves.modular.businessGslrLossCollect.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.basic.info.modular.enumInfo.entity.BasicEnum;
import com.unis.basic.info.modular.enumInfo.mapper.BasicEnumMapper;
import com.unis.basic.info.modular.orgInfo.entity.OrgInfo;
import com.unis.basic.info.modular.orgInfo.mapper.OrgInfoMapper;
import com.unis.basic.info.modular.storehouseInfo.entity.StorehouseInfo;
import com.unis.basic.info.modular.storehouseInfo.mapper.StorehouseInfoMapper;
import com.unis.basic.info.modular.warehouseInfo.entity.WarehouseInfo;
import com.unis.basic.info.modular.warehouseInfo.mapper.WarehouseInfoMapper;
import com.unis.grainReserves.modular.basicWarehouse.entity.BasicWarehouse;
import com.unis.grainReserves.modular.basicWarehouse.mapper.BasicWarehouseMapper;
import com.unis.grainReserves.modular.basicWarehouse.service.BasicWarehouseService;
import com.unis.grainReserves.modular.businessGslrLossCollect.param.*;
import com.unis.grainReserves.modular.businessGslrLossCollect.vo.BusinessGslrLossCollectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unis.common.enums.CommonSortOrderEnum;
import com.unis.common.exception.CommonException;
import com.unis.common.page.CommonPageRequest;
import com.unis.grainReserves.modular.businessGslrLossCollect.entity.BusinessGslrLossCollect;
import com.unis.grainReserves.modular.businessGslrLossCollect.mapper.BusinessGslrLossCollectMapper;
import com.unis.grainReserves.modular.businessGslrLossCollect.service.BusinessGslrLossCollectService;

import java.util.ArrayList;
import java.util.List;

/**
 * 粮食储备-节粮损益-损溢损失信息采集Service接口实现类
 *
 * @author hjc
 * @date 2024/05/17 15:22
 **/
@Service
public class BusinessGslrLossCollectServiceImpl extends ServiceImpl<BusinessGslrLossCollectMapper, BusinessGslrLossCollect> implements BusinessGslrLossCollectService {


    @Autowired
    private BusinessGslrLossCollectMapper businessGslrLossCollectMapper;

    @Autowired
    private OrgInfoMapper orgInfoMapper;

    @Autowired
    private BasicEnumMapper basicEnumMapper;

    @Autowired
    private StorehouseInfoMapper storehouseInfoMapper;

    @Autowired
    private BasicWarehouseMapper basicWarehouseMapper;
    @Override
    public Page<BusinessGslrLossCollectVO> page(BusinessGslrLossCollectPageParam pageParam) {

        //获取对应的数据
        List<BusinessGslrLossCollect> collectList = businessGslrLossCollectMapper.selectMyPage
                (pageParam.getCurrent(), pageParam.getSize(), pageParam.getGrainNatureName(),
                        pageParam.getGrainVarietyName(), pageParam.getOrgName());

        //封装返回对象
        List<BusinessGslrLossCollectVO> businessGslrLossCollectVOS = new ArrayList<>();
        for (BusinessGslrLossCollect businessGslrLossCollect : collectList) {
            BusinessGslrLossCollectVO businessGslrLossCollectVO = new BusinessGslrLossCollectVO();
            BeanUtils.copyProperties(businessGslrLossCollect, businessGslrLossCollectVO);
            //查询对应的库区名称
            OrgInfo orgInfo = orgInfoMapper.selectById(businessGslrLossCollect.getOrgId());
            businessGslrLossCollectVO.setOrgName(orgInfo.getOrgName());
            //查询对应的品种性质等级
            List<BasicEnum> basicEnums = basicEnumMapper.selectList(new LambdaQueryWrapper<BasicEnum>()
                    .eq(BasicEnum::getEnumid, businessGslrLossCollect.getGrainNatureId()));

            if (CollectionUtils.isNotEmpty(basicEnums)) {
                businessGslrLossCollectVO.setGrainNatureName(basicEnums.get(0).getEnumname());
            }

            List<BasicEnum> basicEnums1 = basicEnumMapper.selectList(new LambdaQueryWrapper<BasicEnum>()
                    .eq(BasicEnum::getEnumid, businessGslrLossCollect.getGrainVarietyId()));

            if (CollectionUtils.isNotEmpty(basicEnums1)) {
                businessGslrLossCollectVO.setGrainVarietyName(basicEnums1.get(0).getEnumname());
            }

            List<BasicEnum> basicEnums2 = basicEnumMapper.selectList(new LambdaQueryWrapper<BasicEnum>()
                    .eq(BasicEnum::getEnumid, businessGslrLossCollect.getGrainGradeId()));

            if (CollectionUtils.isNotEmpty(basicEnums2)) {
                businessGslrLossCollectVO.setGrainGradeName(basicEnums2.get(0).getEnumname());
            }


            List<BasicWarehouse> basicWarehouses = basicWarehouseMapper.selectList(new LambdaQueryWrapper<BasicWarehouse>()
                    .eq(BasicWarehouse::getWarehouseId, businessGslrLossCollect.getWarehouseId()));

            if (CollectionUtils.isNotEmpty(basicWarehouses)) {
                businessGslrLossCollectVO.setWarehouseName(basicWarehouses.get(0).getWarehouseName());
            }

            List<StorehouseInfo> basicStorehouses = storehouseInfoMapper.selectList(new LambdaQueryWrapper<StorehouseInfo>()
                    .eq(StorehouseInfo::getStorehouseId, businessGslrLossCollect.getBasicStorehouseId()));

            if (CollectionUtils.isNotEmpty(basicStorehouses)) {
                businessGslrLossCollectVO.setStorehouseName(basicStorehouses.get(0).getStorehouseName());
            }

            businessGslrLossCollectVOS.add(businessGslrLossCollectVO);

        }

        Page<BusinessGslrLossCollectVO> page = new Page<>();
        page.setRecords(businessGslrLossCollectVOS);
        page.setTotal(businessGslrLossCollectVOS.size());
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BusinessGslrLossCollectAddParam businessGslrLossCollectAddParam) {
        BusinessGslrLossCollect businessGslrLossCollect = BeanUtil.toBean(businessGslrLossCollectAddParam, BusinessGslrLossCollect.class);
        this.save(businessGslrLossCollect);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BusinessGslrLossCollectEditParam businessGslrLossCollectEditParam) {
        BusinessGslrLossCollect businessGslrLossCollect = this.queryEntity(businessGslrLossCollectEditParam.getId());
        BeanUtil.copyProperties(businessGslrLossCollectEditParam, businessGslrLossCollect);
        this.updateById(businessGslrLossCollect);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BusinessGslrLossCollectIdParam> businessGslrLossCollectIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(businessGslrLossCollectIdParamList, BusinessGslrLossCollectIdParam::getId));
    }

    @Override
    public BusinessGslrLossCollectVO detail(BusinessGslrLossCollectIdParam businessGslrLossCollectIdParam) {
       /* return this.queryEntity(businessGslrLossCollectIdParam.getId());*/


        BusinessGslrLossCollectVO businessGslrLossCollectVO = new BusinessGslrLossCollectVO();

        BusinessGslrLossCollect businessGslrLossCollect = businessGslrLossCollectMapper.selectById(businessGslrLossCollectIdParam.getId());
        if (ObjectUtils.isEmpty(businessGslrLossCollect)){
            throw new RuntimeException("暂无数据，id为"+businessGslrLossCollectIdParam.getId());
        }

        BeanUtils.copyProperties(businessGslrLossCollect,businessGslrLossCollectVO);


        //查询对应的库区名称
        OrgInfo orgInfo = orgInfoMapper.selectById(businessGslrLossCollect.getOrgId());
        businessGslrLossCollectVO.setOrgName(orgInfo.getOrgName());
        //查询对应的品种性质等级
        List<BasicEnum> basicEnums = basicEnumMapper.selectList(new LambdaQueryWrapper<BasicEnum>()
                .eq(BasicEnum::getEnumid, businessGslrLossCollect.getGrainNatureId()));

        if (CollectionUtils.isNotEmpty(basicEnums)) {
            businessGslrLossCollectVO.setGrainNatureName(basicEnums.get(0).getEnumname());
        }

        List<BasicEnum> basicEnums1 = basicEnumMapper.selectList(new LambdaQueryWrapper<BasicEnum>()
                .eq(BasicEnum::getEnumid, businessGslrLossCollect.getGrainVarietyId()));

        if (CollectionUtils.isNotEmpty(basicEnums1)) {
            businessGslrLossCollectVO.setGrainVarietyName(basicEnums1.get(0).getEnumname());
        }

        List<BasicEnum> basicEnums2 = basicEnumMapper.selectList(new LambdaQueryWrapper<BasicEnum>()
                .eq(BasicEnum::getEnumid, businessGslrLossCollect.getGrainGradeId()));

        if (CollectionUtils.isNotEmpty(basicEnums2)) {
            businessGslrLossCollectVO.setGrainGradeName(basicEnums2.get(0).getEnumname());
        }


        List<BasicWarehouse> basicWarehouses = basicWarehouseMapper.selectList(new LambdaQueryWrapper<BasicWarehouse>()
                .eq(BasicWarehouse::getWarehouseId, businessGslrLossCollect.getWarehouseId()));

        if (CollectionUtils.isNotEmpty(basicWarehouses)) {
            businessGslrLossCollectVO.setWarehouseName(basicWarehouses.get(0).getWarehouseName());
        }

        List<StorehouseInfo> basicStorehouses = storehouseInfoMapper.selectList(new LambdaQueryWrapper<StorehouseInfo>()
                .eq(StorehouseInfo::getStorehouseId, businessGslrLossCollect.getBasicStorehouseId()));

        if (CollectionUtils.isNotEmpty(basicStorehouses)) {
            businessGslrLossCollectVO.setStorehouseName(basicStorehouses.get(0).getStorehouseName());
        }

        return businessGslrLossCollectVO;




    }

    @Override
    public BusinessGslrLossCollect queryEntity(String id) {
        BusinessGslrLossCollect businessGslrLossCollect = this.getById(id);
        if (ObjectUtil.isEmpty(businessGslrLossCollect)) {
            throw new CommonException("粮食储备-节粮损益-损溢损失信息采集不存在，id值为：{}", id);
        }
        return businessGslrLossCollect;
    }

    /**
     * 获取粮食储备-节粮损益-损溢损失分析报警
     *
     * @author hjc
     * @date 2024/05/17 15:22
     */
    @Override
    public Page<BusinessGslrLossCollectVO> alarmPage(BusinessGslrLossCollectPageAlarmParam pageAlarmParam) {

        Page<BusinessGslrLossCollectVO> page = new Page<>(pageAlarmParam.getCurrent(), pageAlarmParam.getSize());

        return businessGslrLossCollectMapper.alarmPage(page,pageAlarmParam.getOrgName(),pageAlarmParam.getStorehouseName());

    }

    /**
     * 获取粮食储备-节粮损益-报警折线图
     *
     * @author hjc
     * @date  2024/05/17 15:22
     */
    @Override
    public List<BusinessGslrLossCollectVO> alarmLineChart(AlarmLineChartParam param) {


        return businessGslrLossCollectMapper.alarmLineChart(param.getStartTime(),param.getEndTime(),param.getOrgName(),
                param.getWarehouseName(),param.getStorehouseName());


    }
}
