package com.op.mes.service.impl;

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

import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.security.utils.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.op.mes.mapper.ProRouteProductMapper;
import com.op.mes.domain.ProRouteProduct;
import com.op.mes.service.IProRouteProductService;

/**
 * 工艺线体关联Service业务层处理
 *
 * @author Open Platform
 * @date 2023-07-13
 */
@Service
public class ProRouteProductServiceImpl implements IProRouteProductService {
	@Autowired
	private ProRouteProductMapper proRouteProductMapper;

	/**
	 * 查询工艺线体关联
	 *
	 * @param recordId 工艺线体关联主键
	 * @return 工艺线体关联
	 */
	@Override
	@DS("#header.poolName")
	public ProRouteProduct selectProRouteProductByRecordId(String recordId) {
		return proRouteProductMapper.selectProRouteProductByRecordId(recordId);
	}

	/**
	 * 查询工艺线体关联列表
	 *
	 * @param proRouteProduct 工艺线体关联
	 * @return 工艺线体关联
	 */
	@Override
	@DS("#header.poolName")
	public List<ProRouteProduct> selectProRouteProductList(ProRouteProduct proRouteProduct) {
		return proRouteProductMapper.selectProRouteProductList(proRouteProduct);
	}

	/**
	 * 新增工艺线体关联
	 *
	 * @param proRouteProduct 工艺线体关联
	 * @return 结果
	 */
	@Override
	@DS("#header.poolName")
	public int insertProRouteProduct(ProRouteProduct proRouteProduct) {
		Date now = DateUtils.getNowDate();
		if(StringUtils.isNotBlank(proRouteProduct.getRouteId())){
           //删除之前的关联关系
			proRouteProductMapper.deleteByRouteId(proRouteProduct);
		}
		if(proRouteProduct.getSelectedValues()!=null){
			ProRouteProduct proRouteProductDto =null;
			for(String ProductCode:proRouteProduct.getSelectedValues()){
				//查询物料编码对应的各种信息
				proRouteProductDto = new ProRouteProduct();
				proRouteProductDto.setRecordId(IdUtils.fastSimpleUUID());
				proRouteProductDto.setCreateTime(now);
				proRouteProductDto.setCreateBy(SecurityUtils.getUsername());
				proRouteProductDto.setRouteId(proRouteProduct.getRouteId());
				proRouteProductDto.setItemId(proRouteProduct.getItemId());
				proRouteProductDto.setItemCode(ProductCode);
				proRouteProductDto.setItemName(proRouteProduct.getItemName());
				proRouteProductMapper.insertProRouteProduct(proRouteProductDto);
			}
		}
		//新增关联关系
		return 1;
	}

	/**
	 * 修改工艺线体关联
	 *
	 * @param proRouteProduct 工艺线体关联
	 * @return 结果
	 */
	@Override
	@DS("#header.poolName")
	public int updateProRouteProduct(ProRouteProduct proRouteProduct) {
		proRouteProduct.setUpdateTime(DateUtils.getNowDate());
		return proRouteProductMapper.updateProRouteProduct(proRouteProduct);
	}

	/**
	 * 批量删除工艺线体关联
	 *
	 * @param recordIds 需要删除的工艺线体关联主键
	 * @return 结果
	 */
	@Override
	@DS("#header.poolName")
	public int deleteProRouteProductByRecordIds(String[] recordIds) {
		return proRouteProductMapper.deleteProRouteProductByRecordIds(recordIds);
	}

	/**
	 * 删除工艺线体关联信息
	 *
	 * @param recordId 工艺线体关联主键
	 * @return 结果
	 */
	@Override
	@DS("#header.poolName")
	public int deleteProRouteProductByRecordId(String recordId) {
		return proRouteProductMapper.deleteProRouteProductByRecordId(recordId);
	}

	@Override
	@DS("#header.poolName")
	public ProRouteProduct getList(ProRouteProduct proRouteProduct) {
		ProRouteProduct dto = new ProRouteProduct();
		List<ProRouteProduct> unSelected = proRouteProductMapper.getRouteProdProductListUndo(proRouteProduct);
		dto.setUnSelect(unSelected);
		List<ProRouteProduct> selected= proRouteProductMapper.getRouteProdProductListDo(proRouteProduct);
		dto.setSelected(selected);
		return dto;
	}
}
