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.ProRouteLineMapper;
import com.op.mes.domain.ProRouteLine;
import com.op.mes.service.IProRouteLineService;

/**
 * 工艺线体关联Service业务层处理
 *
 * @author Open Platform
 * @date 2023-07-13
 */
@Service
public class ProRouteLineServiceImpl implements IProRouteLineService {
	@Autowired
	private ProRouteLineMapper proRouteLineMapper;

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

	/**
	 * 查询工艺线体关联列表
	 *
	 * @param proRouteLine 工艺线体关联
	 * @return 工艺线体关联
	 */
	@Override
	@DS("#header.poolName")
	public List<ProRouteLine> selectProRouteLineList(ProRouteLine proRouteLine) {
		return proRouteLineMapper.selectProRouteLineList(proRouteLine);
	}

	/**
	 * 新增工艺线体关联
	 *
	 * @param proRouteLine 工艺线体关联
	 * @return 结果
	 */
	@Override
	@DS("#header.poolName")
	public int insertProRouteLine(ProRouteLine proRouteLine) {
		Date now = DateUtils.getNowDate();
		if(StringUtils.isNotBlank(proRouteLine.getRouteId())){
           //删除之前的关联关系
			proRouteLineMapper.deleteByRouteId(proRouteLine);
		}
		if(proRouteLine.getSelectedValues()!=null){
			ProRouteLine proRouteLineDto =null;
			for(String lineCode:proRouteLine.getSelectedValues()){
				proRouteLineDto = new ProRouteLine();
				proRouteLineDto.setRecordId(IdUtils.fastSimpleUUID());
				proRouteLineDto.setCreateTime(now);
				proRouteLineDto.setCreateBy(SecurityUtils.getUsername());
				proRouteLineDto.setRouteId(proRouteLine.getRouteId());
				proRouteLineDto.setItemCode(lineCode);
				proRouteLineMapper.insertProRouteLine(proRouteLineDto);
			}
		}
		//新增关联关系
		return 1;
	}

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

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

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

	@Override
	@DS("#header.poolName")
	public ProRouteLine getList(ProRouteLine proRouteLine) {
		ProRouteLine dto = new ProRouteLine();
		List<ProRouteLine> unSelected= proRouteLineMapper.getRouteProdLineListUndo(proRouteLine);
		dto.setUnSelect(unSelected);
		List<ProRouteLine> selected= proRouteLineMapper.getRouteProdLineListDo(proRouteLine);
		dto.setSelected(selected);
		return dto;
	}
}
