package com.ruoyi.warehouseBusiness.service.impl;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.basicData.mapper.TWarehouseMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.TWarehouse;
import com.ruoyi.common.core.domain.enums.WarehouseEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.domain.dto.WarehouseDTO;
import com.ruoyi.warehouseBusiness.domain.enums.WarehouseTypeEnum;
import com.ruoyi.warehouseBusiness.domain.vo.WarehouseTimeIntervalVO;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsitemsMapper;
import com.ruoyi.warehouseBusiness.service.ITWarehousebillsitemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 仓库明细从表Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@Service
public class TWarehousebillsitemsServiceImpl implements ITWarehousebillsitemsService {
    @Autowired
    private TWarehouseMapper warehouseMapper;

    @Autowired
    private TWarehousebillsitemsMapper tWarehousebillsitemsMapper;

    /**
     * 查询仓库明细从表
     *
     * @param fId 仓库明细从表ID
     * @return 仓库明细从表
     */
    @Override
    public TWarehousebillsitems selectTWarehousebillsitemsById(Long fId) {
        return tWarehousebillsitemsMapper.selectTWarehousebillsitemsById(fId);
    }

    /**
     * 查询仓库明细从表列表
     *
     * @param tWarehousebillsitems 仓库明细从表
     * @return 仓库明细从表
     */
    @Override
    public List<TWarehousebillsitems> selectTWarehousebillsitemsList(TWarehousebillsitems tWarehousebillsitems) {
        return tWarehousebillsitemsMapper.selectTWarehousebillsitemsList(tWarehousebillsitems);
    }

    /**
     * 新增仓库明细从表
     *
     * @param tWarehousebillsitems 仓库明细从表
     * @return 结果
     */
    @Override
    public int insertTWarehousebillsitems(TWarehousebillsitems tWarehousebillsitems) {
        tWarehousebillsitems.setCreateTime(DateUtils.getNowDate());
        return tWarehousebillsitemsMapper.insertTWarehousebillsitems(tWarehousebillsitems);
    }

    /**
     * 修改仓库明细从表
     *
     * @param tWarehousebillsitems 仓库明细从表
     * @return 结果
     */
    @Override
    public int updateTWarehousebillsitems(TWarehousebillsitems tWarehousebillsitems) {
        tWarehousebillsitems.setUpdateTime(DateUtils.getNowDate());
        return tWarehousebillsitemsMapper.updateTWarehousebillsitems(tWarehousebillsitems);
    }

    /**
     * 批量删除仓库明细从表
     *
     * @param fIds 需要删除的仓库明细从表ID
     * @return 结果
     */
    @Override
    public int deleteTWarehousebillsitemsByIds(Long[] fIds) {
        return tWarehousebillsitemsMapper.deleteTWarehousebillsitemsByIds(fIds);
    }

    /**
     * 删除仓库明细从表信息
     *
     * @param fId 仓库明细从表ID
     * @return 结果
     */
    @Override
    public int deleteTWarehousebillsitemsById(Long fId) {
        return tWarehousebillsitemsMapper.deleteTWarehousebillsitemsById(fId);
    }

    @Override
    public int warehouseItemfItemstatus(Long fPid, Long fItemstatus) {
        return tWarehousebillsitemsMapper.warehouseItemfItemstatus(fPid, fItemstatus);
    }

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

    /**
     * 根据日期区间计算 仓库入库数量比较
     *
     * @param warehouseDto 查询条件
     * @return
     */
    @Override
    public AjaxResult warehouseComparison(WarehouseDTO warehouseDto) {
        warehouseDto.setWarehouseType(WarehouseTypeEnum.SJRK.getType());
        Date queryTime = warehouseDto.getQueryTime();
        List<String> newTimeList = new ArrayList<>();
        List<String> timeList = new ArrayList<>();
        // 结果
        List<WarehouseTimeIntervalVO> firstItemsList;
        List<WarehouseTimeIntervalVO> secondItemsList;
        // 按照年
        if (WarehouseEnum.BY_YEAR.getType().equals(warehouseDto.getQueryType())) {
            // 获取上一年
            String year = DateUtils.getYear(queryTime);
            timeList.add(year + "-01-01");
            timeList.add(year + "-12-31");
            warehouseDto.setTimeList(timeList);
            firstItemsList = tWarehousebillsitemsMapper.selectInboundQtyByTime(warehouseDto);
            // 获取当前年
            Date newDate = DateUtils.addYears(queryTime, 1);
            String newYear = DateUtils.getYear(newDate);
            newTimeList.add(newYear + "-01-01");
            newTimeList.add(newYear + "-12-31");
            warehouseDto.setTimeList(newTimeList);
            secondItemsList = tWarehousebillsitemsMapper.selectInboundQtyByTime(warehouseDto);
            return queryWarehouseQty(firstItemsList, secondItemsList);
        } else
            // 按照月
            if (WarehouseEnum.BY_MONTH.getType().equals(warehouseDto.getQueryType())) {
                // 获取年
                String year = DateUtils.getYear(queryTime);
                // 获取月
                String month = DateUtils.getMonth(queryTime);
                timeList.add(DateUtils.getFirstDay(Integer.parseInt(year), Integer.parseInt(month)));
                timeList.add(DateUtils.getLastDay(Integer.parseInt(year), Integer.parseInt(month)));
                warehouseDto.setTimeList(timeList);
                firstItemsList = tWarehousebillsitemsMapper.selectInboundQtyByTime(warehouseDto);
                Date newDate = DateUtils.addMonths(queryTime, 1);
                // 获取年
                String newYear = DateUtils.getYear(newDate);
                // 获取月
                String newMonth = DateUtils.getMonth(newDate);
                newTimeList.add(DateUtils.getFirstDay(Integer.parseInt(newYear), Integer.parseInt(newMonth)));
                newTimeList.add(DateUtils.getLastDay(Integer.parseInt(newYear), Integer.parseInt(newMonth)));
                warehouseDto.setTimeList(newTimeList);
                secondItemsList = tWarehousebillsitemsMapper.selectInboundQtyByTime(warehouseDto);
                return queryWarehouseQty(firstItemsList, secondItemsList);
            } else
                // 按照周
                if (WarehouseEnum.BY_WEEK.getType().equals(warehouseDto.getQueryType())) {
                    // 上周
                    Date date = DateUtils.addDays(queryTime, -7);
                    timeList.add(DateUtils.dateTime(date));
                    timeList.add(DateUtils.dateTime(queryTime));
                    warehouseDto.setTimeList(timeList);
                    firstItemsList = tWarehousebillsitemsMapper.selectInboundQtyByTime(warehouseDto);
                    // 本周
                    Date newDate = DateUtils.addDays(queryTime, 1);
                    newTimeList.add(DateUtils.dateTime(newDate));
                    newTimeList.add(DateUtils.dateTime(DateUtils.addDays(newDate, 6)));
                    warehouseDto.setTimeList(newTimeList);
                    secondItemsList = tWarehousebillsitemsMapper.selectInboundQtyByTime(warehouseDto);
                    return queryWarehouseQty(firstItemsList, secondItemsList);
                }
        return AjaxResult.success();
    }

    /**
     * 查询数量
     *
     * @param firstItemsList  上次
     * @param secondItemsList 本次
     * @return
     */
    private AjaxResult queryWarehouseQty(List<WarehouseTimeIntervalVO> firstItemsList,
                                         List<WarehouseTimeIntervalVO> secondItemsList) {
        // 如果都为空
        if (CollectionUtils.isEmpty(firstItemsList) && CollectionUtils.isEmpty(secondItemsList)) {
            return AjaxResult.error();
        }
        if (CollectionUtils.isNotEmpty(firstItemsList)) {
            List<Long> warehouseIds = firstItemsList.stream().filter(li -> Objects.nonNull(li.getWarehouseId())).map(WarehouseTimeIntervalVO::getWarehouseId)
                    .distinct().collect(Collectors.toList());

            List<TWarehouse> warehouseList = warehouseMapper.selectByIds(warehouseIds);
            firstItemsList.forEach(li -> {
                li.setLastQty(li.getSumQty());
                Optional<TWarehouse> warehouseFirst = warehouseList.stream().filter(ware -> Objects.equals(ware.getfId(), li.getWarehouseId())).findFirst();
                warehouseFirst.ifPresent(tWarehouse -> li.setWarehouseName(tWarehouse.getfName()));

                Optional<WarehouseTimeIntervalVO> first = secondItemsList.stream().filter(second -> Objects.equals(li.getWarehouseId(), second.getWarehouseId()))
                        .findFirst();
                if (first.isPresent()) {
                    li.setThisQty(first.get().getSumQty());
                    li.setDifference(li.getThisQty() - li.getLastQty());
                } else {
                    li.setDifference(li.getLastQty() * -1);
                }
            });
            return AjaxResult.success(firstItemsList);
        }
        List<Long> warehouseIds = firstItemsList.stream().filter(li -> Objects.nonNull(li.getWarehouseId())).map(WarehouseTimeIntervalVO::getWarehouseId)
                .distinct().collect(Collectors.toList());
        List<TWarehouse> warehouseList = warehouseMapper.selectByIds(warehouseIds);
        secondItemsList.forEach(li -> {
            li.setThisQty(li.getSumQty());
            li.setDifference(li.getLastQty());
            Optional<TWarehouse> warehouseFirst = warehouseList.stream().filter(ware -> Objects.equals(ware.getfId(), li.getWarehouseId())).findFirst();
            warehouseFirst.ifPresent(tWarehouse -> li.setWarehouseName(tWarehouse.getfName()));
        });
        return AjaxResult.success(secondItemsList);
    }

}
