package com.op.wms.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.op.common.core.context.SecurityContextHolder;
import com.op.common.core.web.domain.AjaxResult;
import com.op.wms.domain.OdsRawStorageNews;
import com.op.wms.domain.Purcode;
import com.op.wms.mapper.PurcodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.op.wms.mapper.BasePowderOrderMapper;
import com.op.wms.domain.BasePowderOrder;
import com.op.wms.service.IBasePowderOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 粉料采购计划Service业务层处理
 *
 * @author Open Platform
 * @date 2023-07-05
 */
@Service
public class BasePowderOrderServiceImpl implements IBasePowderOrderService {
	@Autowired
	private BasePowderOrderMapper basePowderOrderMapper;

	@Autowired
	private PurcodeMapper purcodeMapper;

	/**
	 * 查询粉料采购计划
	 *
	 * @param ID 粉料采购计划主键
	 * @return 粉料采购计划
	 */
	@Override
	public BasePowderOrder selectBasePowderOrderByID(String ID) {
		return basePowderOrderMapper.selectBasePowderOrderByID(ID);
	}

	/**
	 * 查询粉料采购计划列表
	 *
	 * @param basePowderOrder 粉料采购计划
	 * @return 粉料采购计划
	 */
	@Override
	public List<BasePowderOrder> selectBasePowderOrderList(BasePowderOrder basePowderOrder) {
		return basePowderOrderMapper.selectBasePowderOrderList(basePowderOrder);
	}

	/**
	 * 新增粉料采购计划
	 *
	 * @param basePowderOrder 粉料采购计划
	 * @return 结果
	 */
	@Override
	public int insertBasePowderOrder(BasePowderOrder basePowderOrder) {
		String userName = SecurityContextHolder.getUserName();
		basePowderOrder.setID(UUID.randomUUID().toString());
		String 	orderCode =getPurcode(null,"FL");
		basePowderOrder.setUserDefined1("FL");
		basePowderOrder.setOrderCode(orderCode);
		basePowderOrder.setOrderStatus("1");
		basePowderOrder.setCreateBy(userName);
		basePowderOrder.setActive("1");
		basePowderOrder.setCreateDate(new Date());
		return basePowderOrderMapper.insertBasePowderOrder(basePowderOrder);
	}



	private String getPurcode(String factorycode, String orderType) {

		String purcode = "";
		String curDate = new SimpleDateFormat("yyyyMMdd").format(new Date());

		Integer vaule = purcodeMapper.queryCurValue(orderType, curDate);
		if (vaule == null) {
			Purcode purcodeO = new Purcode();
			purcodeO.setCurDate(curDate);
			purcodeO.setOrderType(orderType);
			purcodeO.setValue(1);
			int count = purcodeMapper.insert(purcodeO);
			if (count != 1) {
				return "";
			}
			if (factorycode != null && !factorycode.equals("")) {
				purcode = orderType + factorycode + curDate.substring(2) + String.format("%04d", 1);
			} else {
				purcode = orderType + curDate.substring(2) + String.format("%04d", 1);
			}

		} else {
			int count = purcodeMapper.updateCurValue(orderType, curDate, vaule);
			if (count != 1) {
				return "";
			}

			if (factorycode != null && !factorycode.equals("")) {
				purcode = orderType + factorycode + curDate.substring(2) + String.format("%04d", vaule + 1);
			} else {
				purcode = orderType + curDate.substring(2) + String.format("%04d", vaule + 1);
			}
		}
		return purcode;
	}
	/**
	 * 修改粉料采购计划
	 *
	 * @param basePowderOrder 粉料采购计划
	 * @return 结果
	 */
	@Override
	public int updateBasePowderOrder(BasePowderOrder basePowderOrder) {
		return basePowderOrderMapper.updateBasePowderOrder(basePowderOrder);
	}

	/**
	 * 批量删除粉料采购计划
	 *
	 * @param IDs 需要删除的粉料采购计划主键
	 * @return 结果
	 */
	@Override
	public int deleteBasePowderOrderByIDs(String[] IDs) {
		return basePowderOrderMapper.deleteBasePowderOrderByIDs(IDs);
	}

	/**
	 * 删除粉料采购计划信息
	 *
	 * @param ID 粉料采购计划主键
	 * @return 结果
	 */
	@Override
	public int deleteBasePowderOrderByID(String ID) {
		return basePowderOrderMapper.deleteBasePowderOrderByID(ID);
	}

	@Transactional
	@Override
	public int confirmReceipt(BasePowderOrder basePowderOrder) {
		BasePowderOrder basePowderOrder1 = new BasePowderOrder();
		String siteCode = basePowderOrder.getSiteCode();
		String userName = SecurityContextHolder.getUserName();
		//修改状态 改为收货完成 ，送货数量---即是整车重量减空车重量，把送货数量放入库存  ---  放入库存 ----
		basePowderOrder1.setID(basePowderOrder.getID());
		basePowderOrder1.setEmptyNumber(basePowderOrder.getEmptyNumber());//空车重量
		basePowderOrder1.setComNumber(basePowderOrder.getComNumber());//整车重量
		basePowderOrder1.setOrderStatus("3");
		basePowderOrder1.setLastUpdateBy(userName);
		basePowderOrder1.setSiteCode(siteCode);
		basePowderOrder1.setLastUpdateDate(new Date());
		BigDecimal number = basePowderOrder.getComNumber().subtract(basePowderOrder.getEmptyNumber());
		basePowderOrder1.setRealityNumber(number);//整车重量减空车重量   ------真实数量
		int tem = basePowderOrderMapper.updateBasePowderOrder(basePowderOrder1);
		//把库存放入库存表
		basePowderOrder1.setLocCode(basePowderOrder.getLocCode());
		basePowderOrder1.setMaterialCode(basePowderOrder.getMaterialCode());
		//先查询有没有这个库存，如果有就放入，如果没有就新增
		OdsRawStorageNews odsRawStorageNews  = basePowderOrderMapper.selectOdsRawStorageNews(basePowderOrder1);
		if (odsRawStorageNews!=null){
			OdsRawStorageNews odsRawStorageNews1 = new OdsRawStorageNews();
			odsRawStorageNews1.setStorageId(odsRawStorageNews.getStorageId());
			odsRawStorageNews1.setAmount(number);
			basePowderOrderMapper.updateOdsRawStorageNewsAdd(odsRawStorageNews);
		}else {
			OdsRawStorageNews odsRawStorageNews1 = new OdsRawStorageNews();
			odsRawStorageNews1.setStorageId(UUID.randomUUID().toString());
			odsRawStorageNews1.setSiteCode(siteCode);
			odsRawStorageNews1.setCreateBy(userName);
			odsRawStorageNews1.setCreateTime(new Date());
			odsRawStorageNews1.setWlCode(basePowderOrder.getLocCode());//库位
			odsRawStorageNews1.setMaterialCode(basePowderOrder.getMaterialCode());//物料
			odsRawStorageNews1.setMaterialDesc(basePowderOrder.getMaterialDesc());
			odsRawStorageNews1.setAmount(number);//数量
			odsRawStorageNews1.setStorageType("1");//
			odsRawStorageNews1.setStorageAmount(number);//冻结数量---先冻结  不能算真正入库---质检合格再后解冻入库
			basePowderOrderMapper.insertOdsRawStorageNews(odsRawStorageNews1);
		}
		return tem;
	}
	@Transactional
	@Override
	public AjaxResult powderReturn(String[] iDs) {
		for (String id:
		iDs) {//只退回不合格的
			BasePowderOrder basePowderOrder=	basePowderOrderMapper.selectBasePowderOrderByID(id);
			if ((basePowderOrder.getUserDefined2()==null||"1".equals(basePowderOrder.getUserDefined2()))){
				return  AjaxResult.error("只退回不合格的");
			}else if ("2".equals(basePowderOrder.getOrderStatus())){
				return  AjaxResult.error("已经退回了");
			}
		}
		for (String id:
				iDs) {
			BasePowderOrder basePowderOrder =	basePowderOrderMapper.selectBasePowderOrderByID(id);
			OdsRawStorageNews odsRawStorageNews1 = new OdsRawStorageNews();
			odsRawStorageNews1.setSiteCode(basePowderOrder.getSiteCode());
			odsRawStorageNews1.setMaterialCode(basePowderOrder.getMaterialCode());
			odsRawStorageNews1.setWlCode(basePowderOrder.getLocCode());
			odsRawStorageNews1.setAmount(basePowderOrder.getRealityNumber());
			basePowderOrderMapper.updateOdsRawStorageNewsReturn(odsRawStorageNews1);
			BasePowderOrder basePowderOrder1 = new BasePowderOrder();
			basePowderOrder1.setID(id);
			basePowderOrder1.setOrderStatus("2");//退回
			basePowderOrderMapper.updateBasePowderOrder(basePowderOrder);
		}
		return null;
	}
}
