/**
 */
package com.sifang.modules.supply.warehouse.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.utils.PlantformUtils;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialDeliverOrder;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialDeliverOrderDetail;
import com.sifang.modules.supply.setting.entity.CrmSupplyWarehouse;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialReturnOrder;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialReturnOrderDetail;
import com.sifang.modules.supply.stock.entity.MaterialWarehouseStocks;
import com.sifang.modules.supply.stock.entity.SupplyMaterial;
import com.sifang.modules.supply.stock.service.SupplyMaterialService;
import com.sifang.modules.supply.warehouse.constant.MaterialConstant;
import com.sifang.modules.supply.warehouse.entity.*;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.LayuiPage;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sifang.modules.supply.warehouse.dao.SupplyMaterialBatchDao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 物料批次信息Service
 * @author lzp
 * @version 
 */
@Component
@Transactional(readOnly = true)
public class SupplyMaterialBatchService extends BaseService {

	@Autowired
	private SupplyMaterialBatchDao supplyMaterialBatchDao;

	@Autowired
	private SupplyMaterialService materialService;
	
	public SupplyMaterialBatch get(String id) {
		return supplyMaterialBatchDao.get(id);
	}


	public SupplyMaterialBatch  createBatchForInventory(CrmSupplyInventoryOrder order, CrmSupplyInventoryOrderDetail detail, String keepId) {
		BigDecimal diffAmount = detail.getAmount().subtract(detail.getStock());//盘点数量-实物库存
		SupplyMaterial fitting = materialService.getById(detail.getMaterialId());
		Date now = new Date();
		SupplyMaterialBatch batch = new SupplyMaterialBatch();
		batch.setCode(supplyMaterialBatchDao.getBatchCode());
		batch.setBizType(MaterialConstant.BIZ_TYPR_THREE);
		batch.setBizId(order.getId());
		batch.setBizDetailId(detail.getId());
		batch.setBizCode(order.getCode());
		batch.setMaterialKeepId(keepId);
		batch.setMaterialId(fitting.getId());
		batch.setBelongType(order.getInventoryType());
		batch.setBelongId(order.getInventoryObjectId());
		batch.setWarehouseId(order.getWarehouseId());
		batch.setAmount(diffAmount);
		batch.setCreateTime(now);
		batch.setInputTime(now);
		batch.setInstoreTime(now);
		batch.setNewestStorageTime(now);
		batch.setRemaining(diffAmount);
		batch.setSoldNum(BigDecimal.ZERO);
		batch.setCostPrice(detail.getCostPrice());
		batch.setBdSalePrice(detail.getBdSalePrice());
		batch.setVersion(1L);
		supplyMaterialBatchDao.save(batch);
		return batch;
	}

	// 用于初始代理商仓库库存
	public SupplyMaterialBatch  createBatchForInventorys(BigDecimal diffAmount, SupplyMaterial material, String bizId,String bizCode,
														 CrmSupplyWarehouse warehouse, String keepId,BigDecimal price,String belongId)  {
		Date now = new Date();
		SupplyMaterialBatch batch = new SupplyMaterialBatch();
		batch.setCode(supplyMaterialBatchDao.getBatchCode());
		batch.setBizType(MaterialConstant.BIZ_TYPR_THREE);
		batch.setBizId(bizId);
		batch.setBizDetailId(keepId);
		batch.setBizCode(bizCode);
		batch.setMaterialKeepId(keepId);
		batch.setMaterialId(material.getId());
		batch.setBelongType("2");//归属代理商
		batch.setBelongId(belongId);
		batch.setWarehouseId(warehouse.getId());
		batch.setAmount(diffAmount);
		batch.setCreateTime(now);
		batch.setInputTime(now);
		batch.setInstoreTime(now);
		batch.setNewestStorageTime(now);
		batch.setRemaining(diffAmount);
		batch.setSoldNum(BigDecimal.ZERO);
		batch.setCostPrice(BigDecimal.ZERO);
		batch.setBdSalePrice(price);
		batch.setVersion(1L);
		supplyMaterialBatchDao.save(batch);
		return batch;
	}
	public SupplyMaterialBatch  createBatchForDeliver(CrmSupplyMaterialDeliverOrder order,
													  CrmSupplyMaterialDeliverOrderDetail detail, String keepId) {
		BigDecimal diffAmount = detail.getInAmount();//采购入库单的入库库存
		Date now = new Date();
		SupplyMaterialBatch batch = new SupplyMaterialBatch();
		batch.setCode(supplyMaterialBatchDao.getBatchCode());
		batch.setBizType(MaterialConstant.BIZ_TYPE_QQ);
		batch.setBizId(order.getId());
		batch.setBizDetailId(detail.getId());
		batch.setBizCode(order.getNumber());
		batch.setMaterialKeepId(keepId);
		batch.setMaterialId(detail.getMaterialId());
		batch.setBelongType("1");
		batch.setBelongId("a");
		batch.setWarehouseId(order.getInWarehouseId());
		batch.setAmount(diffAmount);
		batch.setCreateTime(now);
		batch.setInputTime(now);
		batch.setInstoreTime(now);
		batch.setNewestStorageTime(now);
		batch.setRemaining(diffAmount);
		batch.setSoldNum(BigDecimal.ZERO);
		batch.setCostPrice(detail.getPurchasePrice());
		batch.setBdSalePrice(detail.getPurchasePrice());
		batch.setVersion(1L);
		supplyMaterialBatchDao.save(batch);
		return batch;
	}

    public SupplyMaterialBatch  createBatchForReturn(CrmSupplyMaterialReturnOrder order, CrmSupplyMaterialReturnOrderDetail detail, String keepId, String belongType,String belongId) {

        SupplyMaterial fitting = materialService.getById(detail.getMaterialId());
        Date now = new Date();
        SupplyMaterialBatch batch = new SupplyMaterialBatch();
        batch.setCode(supplyMaterialBatchDao.getBatchCode());
        batch.setBizType(MaterialConstant.BIZ_TYPE_RETURN);
        batch.setBizId(order.getId());
        batch.setBizDetailId(detail.getId());
        batch.setBizCode(order.getNumber());
        batch.setMaterialKeepId(keepId);
        batch.setMaterialId(fitting.getId());
        batch.setBelongType(belongType);
		batch.setBelongId(belongId);
		batch.setWarehouseId(order.getInWarehouseId());
        batch.setAmount(detail.getReturnNum());
        batch.setCreateTime(now);
        batch.setInputTime(now);
        batch.setInstoreTime(now);
        batch.setNewestStorageTime(now);
        batch.setRemaining(detail.getReturnNum());
        batch.setSoldNum(BigDecimal.ZERO);
        batch.setCostPrice(detail.getCostPrice());
        batch.setBdSalePrice(detail.getSalesPrice());
        batch.setVersion(1L);
        supplyMaterialBatchDao.save(batch);
        return batch;
    }


	public SupplyMaterialBatch  createBatchForAllocationOrder(CrmSupplyAllocationOrder order, CrmSupplyAllocationOrderDetail detail, CrmSupplyWarehouse warehouse, String keepId) {
		BigDecimal diffAmount = detail.getAmount().subtract(detail.getStock());//盘点数量-实物库存
		SupplyMaterial fitting = materialService.getById(detail.getMaterialId());
		Date now = new Date();
		SupplyMaterialBatch batch = new SupplyMaterialBatch();
		batch.setCode(supplyMaterialBatchDao.getBatchCode());
		batch.setBizType(MaterialConstant.BIZ_TYPR_SIX);
		batch.setBizId(order.getId());
		batch.setBizDetailId(detail.getId());
		batch.setBizCode(order.getCode());
		batch.setMaterialKeepId(keepId);
		batch.setMaterialId(fitting.getId());
		batch.setBelongType(warehouse.getType());
		batch.setWarehouseId(warehouse.getId());
		batch.setAmount(diffAmount);
		batch.setCreateTime(now);
		batch.setInputTime(now);
		batch.setInstoreTime(now);
		batch.setNewestStorageTime(now);
		batch.setRemaining(diffAmount);
		batch.setSoldNum(BigDecimal.ZERO);
		batch.setCostPrice(detail.getCostPrice());
		batch.setBdSalePrice(detail.getBdSalePrice());
		batch.setVersion(1L);
		batch.setBelongId(warehouse.getAscriptionId());
		supplyMaterialBatchDao.save(batch);
		return batch;
	}


	public List<SupplyMaterialBatch> findBatchesByFIFO(String materialId,String warehouseId,BigDecimal outAmount){

		return supplyMaterialBatchDao.find("FROM SupplyMaterialBatch WHERE materialId =:p1 AND warehouseId =:p2 and  remaining > 0 ORDER BY createTime ASC",new Parameter(materialId,warehouseId));
	}
	public void outStockForInventory(SupplyMaterialBatch batch, BigDecimal amount) {
		batch.setSoldNum(batch.getSoldNum().add(amount));
		batch.setRemaining(batch.getRemaining().subtract(amount));
		if (batch.getRemaining().compareTo(BigDecimal.ZERO) < 0) {
			throw new ServiceException("备件库存批次不足");
		}
		if (batch.getRemaining().compareTo(BigDecimal.ZERO) == 0) {
			batch.setInstoreTime(null);
		}
		batch.setUpdateTime(new Date());
		batch.setNewestKeepTime(new Date());
		supplyMaterialBatchDao.save(batch);
	}

	public LayuiPage<Record> materialBathListData(LayuiPage page, Map<String, String> map){
		page.setData(supplyMaterialBatchDao.materialBathListData(page,map));
		page.setCount(supplyMaterialBatchDao.countBathList(map));
		return page;
	}

}
