package com.deer.wms.produce.manage.service.impl;

import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.produce.manage.constant.ProduceManageConstant;
import com.deer.wms.produce.manage.dao.MaterialsOutgoingLogMapper;
import com.deer.wms.produce.manage.dao.MaterialsStockInfoMapper;
import com.deer.wms.produce.manage.model.*;
import com.deer.wms.produce.manage.service.MaterialsOutgoingLogService;
import com.deer.wms.produce.manage.service.MaterialsStockInfoService;
import com.deer.wms.project.seed.core.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Created by guo on 2019/07/21.
 */
@Service
@Transactional
public class MaterialsOutgoingLogServiceImpl extends AbstractService<MaterialsOutgoingLog, Integer> implements MaterialsOutgoingLogService {

    @Autowired
    private MaterialsOutgoingLogMapper materialsOutgoingLogMapper;

    @Autowired
    private MaterialsStockInfoService materialsStockInfoService;


    @Override
    public List<MaterialsOutgoingLogVo> findListByOneMaterial(MaterialsOutgoingLogParams params) {
        return materialsOutgoingLogMapper.findListByOneMaterial(params);
    }

    @Override
    public void save(MaterialsOutgoingLog materialsOutgoingLog, CurrentUser currentUser) {
        Date date = new Date();

        materialsOutgoingLog.setOperatorId(currentUser.getUserId());
        materialsOutgoingLog.setCreateTime(date);
        materialsOutgoingLog.setVersion("1.1");
        materialsOutgoingLog.setStatus(ProduceManageConstant.STATUS_AVAILABLE);
        materialsOutgoingLog.setCompanyId(currentUser.getCompanyId());
        materialsOutgoingLogMapper.insert(materialsOutgoingLog);

        MaterialsStockInfo stock = materialsStockInfoService.findBy("materialsId", materialsOutgoingLog.getMaterialsId());
        if(stock==null){//如果是新的物料，库存表中没有该物料信息，则新增一条库存记录
            stock = new MaterialsStockInfo();
            stock.setOperatorId(currentUser.getUserId());
            stock.setCreateTime(date);
            stock.setVersion("1.1");
            stock.setMaterialsId(materialsOutgoingLog.getMaterialsId());
            stock.setUnitId(materialsOutgoingLog.getUnitId());

            if(materialsOutgoingLog.getType() == ProduceManageConstant.TYPE_OUT) {
                stock.setQuantity(0-materialsOutgoingLog.getQuantity());//新建物料的出库仓储数量为负数
            }else if(materialsOutgoingLog.getType() == ProduceManageConstant.TYPE_IN) {
                stock.setQuantity(materialsOutgoingLog.getQuantity());//新建物料的入库仓储数量为正数
            }

            stock.setPositionName(materialsOutgoingLog.getPositionName());
            stock.setCompanyId(currentUser.getCompanyId());

            materialsStockInfoService.save(stock);
        }else{//如果库存表中有该物料信息，则更新对应的库存记录
            MaterialsInfoParams params = new MaterialsInfoParams();//查询条件赋值
            params.setMaterialsId(materialsOutgoingLog.getMaterialsId());
            params.setCompanyId(currentUser.getCompanyId());

            //设置库存数量：已有库存数量+入库数量（或已有库存数量-出库数量）
            Float totalQuantity = materialsStockInfoService.getStockQuantityByMaId(params);
            if(materialsOutgoingLog.getType() == ProduceManageConstant.TYPE_OUT) {
                totalQuantity -= materialsOutgoingLog.getQuantity();
            }else if(materialsOutgoingLog.getType() == ProduceManageConstant.TYPE_IN) {
                totalQuantity += materialsOutgoingLog.getQuantity();
            }
            stock.setQuantity(totalQuantity);

            //设置库存位置：已有库存仓库,出入库记录对应仓库
            String inAndOutLogPosition = materialsOutgoingLog.getPositionName();
            String stockPosition = materialsStockInfoService.getStockPositionByMaId(params);
            if((null!=stockPosition) && (!stockPosition.trim().equals(""))) {
                stock.setPositionName(stockPosition + "," +inAndOutLogPosition);
            }else{
                stock.setPositionName(inAndOutLogPosition);
            }

            materialsStockInfoService.update(stock);
        }
    }

    @Override
    public void deleteByOutgoingIds(HashMap outgoingParam) {
        materialsOutgoingLogMapper.deleteByOutgoingIds(outgoingParam);
    }
}
