package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.winsea.svc.base.base.util.DateUtils;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.mapper.WarehouseInventoryCountInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IWarehouseInOutInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IWarehouseInventoryCountInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.IWarehousePositionStorageInfoService;
import org.apache.commons.httpclient.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 记录库存盘点信息 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-05-25
 */
@Service
public class WarehouseInventoryCountInfoServiceImpl extends ServiceImpl<WarehouseInventoryCountInfoMapper, WarehouseInventoryCountInfo> implements IWarehouseInventoryCountInfoService {

    @Autowired
    private IWarehouseInOutInfoService warehouseInOutInfoService;
    @Autowired
    private IWarehousePositionStorageInfoService warehousePositionStorageInfoService;

    /**
     * 查询盘损记录
     *
     * @param warehouseInventoryCountInfo
     * @return
     */
    @Override
    public Page<WarehouseInventoryCountInfo> selectInfo(WarehouseInventoryCountInfo warehouseInventoryCountInfo) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (warehouseInventoryCountInfo.getCurrentPage() - 1)
                * warehouseInventoryCountInfo.getPageSize());
        //  仓库ID
        pageView.put("baseId", warehouseInventoryCountInfo.getBaseId());
        //  仓位ID
        pageView.put("positionId", warehouseInventoryCountInfo.getPositionId());
        pageView.put("pageSize", warehouseInventoryCountInfo.getPageSize());
        pageView.put("currentPage", warehouseInventoryCountInfo.getCurrentPage());
        // 查询记录总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<WarehouseInventoryCountInfo> dataList = baseMapper.getListByCondition(pageView);
        Page<WarehouseInventoryCountInfo> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(warehouseInventoryCountInfo.getCurrentPage());
        page.setSize(warehouseInventoryCountInfo.getPageSize());
        return page;
    }

    /**
     * 根据货名查累计量
     *
     * @param warehouseInventoryCountInfo
     * @return
     */
    @Override
    public WarehouseInventoryCountInfo getInfo(WarehouseInventoryCountInfo warehouseInventoryCountInfo) {
        WarehouseInventoryCountInfo warehouseInventoryCountInfoCopy = new WarehouseInventoryCountInfo();
        Wrapper<WarehouseInventoryCountInfo> warehouseInventoryCountInfoWrapper = new EntityWrapper<>();
        warehouseInventoryCountInfoWrapper.eq("base_id", warehouseInventoryCountInfo.getBaseId())
                .eq("position_id", warehouseInventoryCountInfo.getPositionId())
                .eq("goods_name_key",warehouseInventoryCountInfo.getGoodsNameKey())
                .eq("delete_flag", "0")
                .orderBy("create_date",false);
        List<WarehouseInventoryCountInfo> warehouseInventoryCountInfos = this.selectList(warehouseInventoryCountInfoWrapper);
        // 有盘损记录
        if (!CollectionUtils.isEmpty(warehouseInventoryCountInfos)) {
            WarehouseInventoryCountInfo warehouseInventoryCountInfo1=warehouseInventoryCountInfos.get(0);
            //最后一次盘损剩余量
            Float physicalInventory=warehouseInventoryCountInfo1.getPhysicalInventory();
            //定义入库
            Float inNetWeight =0f;
            String a= DateUtil.formatDate(warehouseInventoryCountInfo1.getCreateDate(), DateUtils.DATE_FMT_YYYY_MM_DD);
            // 查询入库数量
            List<WarehouseInOutInfo> warehouseInOutInfoList = warehouseInOutInfoService.selectList(new EntityWrapper<WarehouseInOutInfo>()
                    .eq("base_id", warehouseInventoryCountInfo1.getBaseId())
                    .eq("position_id", warehouseInventoryCountInfo1.getPositionId())
                    .eq("in_out_flag", "2")
                    .eq("status_flag","3")
                    .ge("create_date",DateUtil.formatDate(warehouseInventoryCountInfo1.getCreateDate(), DateUtils.DATE_FMT_YYYY_MM_DD))
                    .eq("goods_name_key",warehouseInventoryCountInfo1.getGoodsNameKey()));
            if (!CollectionUtils.isEmpty(warehouseInOutInfoList)) {
                for (WarehouseInOutInfo warehouseInOutInfo : warehouseInOutInfoList) {
                    inNetWeight = inNetWeight + Float.valueOf(String.valueOf(warehouseInOutInfo.getNetWeight()));
                }
            }
            warehouseInventoryCountInfoCopy.setCumulativeStockIn(inNetWeight + physicalInventory);
            //定义出库
            Float outNetWeight = 0f;
            // 查询出库数量
            List<WarehouseInOutInfo> warehouseInOutInfos = warehouseInOutInfoService.selectList(new EntityWrapper<WarehouseInOutInfo>()
                    .eq("base_id", warehouseInventoryCountInfo1.getBaseId())
                    .eq("position_id", warehouseInventoryCountInfo1.getPositionId())
                    .ge("create_date", DateUtil.formatDate(warehouseInventoryCountInfo1.getCreateDate(), DateUtils.DATE_FMT_YYYY_MM_DD))
                    .eq("in_out_flag", "1")
                    .eq("status_flag","3")
                    .eq("goods_name_key",warehouseInventoryCountInfo1.getGoodsNameKey()));
            if (!CollectionUtils.isEmpty(warehouseInOutInfos)) {
                for (WarehouseInOutInfo warehouseInOutInfo : warehouseInOutInfos) {
                        outNetWeight = outNetWeight + Float.valueOf(String.valueOf(warehouseInOutInfo.getNetWeight()));
                }
            }
            warehouseInventoryCountInfoCopy.setCumulativeStockOut(outNetWeight);
            warehouseInventoryCountInfoCopy.setGoodsName(warehouseInventoryCountInfo.getGoodsName());
        }
        // 未盘损过
        else{
            //定义入库
            Float inNetWeight =0f;
            // 查询入库数量
            List<WarehouseInOutInfo> warehouseInOutInfoList = warehouseInOutInfoService.selectList(new EntityWrapper<WarehouseInOutInfo>()
                    .eq("base_id", warehouseInventoryCountInfo.getBaseId())
                    .eq("position_id", warehouseInventoryCountInfo.getPositionId())
                    .eq("in_out_flag", "2")
                    .eq("status_flag","3")
                    .eq("goods_name_key",warehouseInventoryCountInfo.getGoodsNameKey()));
            if (!CollectionUtils.isEmpty(warehouseInOutInfoList)) {
                for (WarehouseInOutInfo warehouseInOutInfo : warehouseInOutInfoList) {
                    inNetWeight = inNetWeight + Float.valueOf(String.valueOf(warehouseInOutInfo.getNetWeight()));
                }
            }
            warehouseInventoryCountInfoCopy.setCumulativeStockIn(inNetWeight);
            //定义出库
            Float outNetWeight = 0f;
            // 查询出库数量
            List<WarehouseInOutInfo> warehouseInOutInfos = warehouseInOutInfoService.selectList(new EntityWrapper<WarehouseInOutInfo>()
                    .eq("base_id", warehouseInventoryCountInfo.getBaseId())
                    .eq("position_id", warehouseInventoryCountInfo.getPositionId())
                    .eq("in_out_flag", "1")
                    .eq("status_flag","3")
                    .eq("goods_name_key",warehouseInventoryCountInfo.getGoodsNameKey()));
            if (!CollectionUtils.isEmpty(warehouseInOutInfos)) {
                for (WarehouseInOutInfo warehouseInOutInfo : warehouseInOutInfos) {
                    outNetWeight = outNetWeight + Float.valueOf(String.valueOf(warehouseInOutInfo.getNetWeight()));
                }
            }
            warehouseInventoryCountInfoCopy.setCumulativeStockOut(outNetWeight);
            warehouseInventoryCountInfoCopy.setGoodsName(warehouseInventoryCountInfo.getGoodsName());
            warehouseInventoryCountInfoCopy.setPhysicalInventory(inNetWeight-outNetWeight);

        }
        return warehouseInventoryCountInfoCopy;
    }


    /**
     * 盘损
     *
     * @param warehouseInventoryCountInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addInventory(WarehouseInventoryCountInfo warehouseInventoryCountInfo) {
        //新增主键id
        warehouseInventoryCountInfo.setId(IdGenerator.generateUUID());
        WarehousePositionStorageInfo warehousePositionStorageInfo = warehousePositionStorageInfoService.selectOne(new EntityWrapper<WarehousePositionStorageInfo>()
                .eq("position_id",warehouseInventoryCountInfo.getPositionId())
                .eq("goods_name_key",warehouseInventoryCountInfo.getGoodsNameKey()));
        if (warehousePositionStorageInfo !=null){
            warehousePositionStorageInfo.setStorage(Double.valueOf(String.valueOf(warehouseInventoryCountInfo.getPhysicalInventory())));
            warehousePositionStorageInfoService.updateById(warehousePositionStorageInfo);
        }
        boolean one = this.insert(warehouseInventoryCountInfo);
        // 假如成功返回ok
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }
}
