package market.Qms.slmord.impl;
/**
 *有附加要求编号则直接验证附加要求，没有则取标准号下的附加要求项
 * @author ZQ
 *
 */
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComBaseAdditionalMapper;
import market.Qms.metallurgy.vo.ComBaseAdditional;
import market.Qms.slmord.mapper.SlmOrdDesignAdditionalMapper;
import market.Qms.slmord.service.QualityDesignAddRequireService;
import market.Qms.slmord.vo.SlmOrdDesignAdditional;
import market.Slm.ContractManagement.mapper.SlmCustomerAddAskMapper;
import market.Slm.ContractManagement.vo.SlmCustomerAddAsk;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;
@RemoteService(path="QualityDesignAddRequireService")
public class QualityDesignAddRequireServiceImpl extends BaseServiceImpl<Object,Object> implements QualityDesignAddRequireService{

	@Override
	public ResultModel GetCustomerAddAsk(CommonPage<SlmCustomerAddAsk> sum){
		ResultModel rm=new ResultModel();
		try{
			List<SlmCustomerAddAsk> listAsk=this.getMapper(SlmCustomerAddAskMapper.class).find(sum.getObject(),0,0);
			rm.setData(listAsk);
			rm.setMsgInfo("操作成功");
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("获取客户附加要求出错："+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 客户附加要求评审
	 * @param sum
	 * @return
	 */
	@Override
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel AddAskReview(CommonPage<SlmCustomerAddAsk> sum){
		ResultModel rm=new ResultModel();
			SlmCustomerAddAsk _addAsk=new SlmCustomerAddAsk();
			_addAsk.setAdd_ask_no(sum.getObject().getAdd_ask_no());
			_addAsk.setUpdate_time(new Date());
			_addAsk.setUpdate_man_no((String)this.getSession().getAttribute(HomeService.SESSION_USER_ID));
			_addAsk.setUpdate_man_name((String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
			_addAsk.setAdd_ask_status(sum.getObject().getAdd_ask_status());
			_addAsk.setReview_desc(sum.getObject().getReview_desc());
			this.getMapper(SlmCustomerAddAskMapper.class).doAddAskReview(_addAsk);

			rm.setMsgInfo("操作成功");
			rm.setState("200");
		return rm;
	}
	
	@Override
	public boolean ValidationOrderAsk(ReturnMsg msg, SlmContractProcessPlan orderLine,String stdCode,String desginKey) {
		List<SlmOrdDesignAdditional> _listAddition=new ArrayList<SlmOrdDesignAdditional>();
		//无客户附加要求
		if (DataHelper.IsNullOrSpace(orderLine.getAdd_ask_no())) {
			
			ComBaseAdditional _queryAdd=new ComBaseAdditional();
			_queryAdd.setStd_code(stdCode);
			_queryAdd.setValidflag("1");
			ComBaseAdditionalMapper mapper=this.getMapper(ComBaseAdditionalMapper.class); 
			List<ComBaseAdditional> listBase=mapper.find(_queryAdd, 0, 0);
			
			//过滤psc
			for (int i = 0; i < listBase.size(); i++) {
				if (listBase.get(i).getPsc()!=null && !listBase.get(i).getPsc().contains(orderLine.getPsc()) ) {
					listBase.remove(i);
					i--;
				}
				
			}
			
			
			for (ComBaseAdditional comBaseAdditional : listBase) {
				SlmOrdDesignAdditional _add=new SlmOrdDesignAdditional();
				try{
					DataHelper.CopyEntity(comBaseAdditional, _add);
					_add.setDesign_key(desginKey);
					_add.setAsk_item(comBaseAdditional.getAsk_itme());
					_add.setStdflag("1");
					_listAddition.add(_add);
				}catch(Exception ex){
					msg.setMessage(ex.getMessage());
					return false;
				}
			}
			if(_listAddition.size()>0){
				this.getMapper(SlmOrdDesignAdditionalMapper.class).insertBatch(_listAddition);
			}
			return true;
		} else {
			SlmCustomerAddAsk _ask=new SlmCustomerAddAsk();
			_ask.setAdd_ask_no(orderLine.getAdd_ask_no());
			SlmCustomerAddAskMapper cusAskMapper=this.getMapper(SlmCustomerAddAskMapper.class);
			List<SlmCustomerAddAsk> listAddAsk=cusAskMapper.find(_ask, 0, 0);
			List<String> listAskItem=new ArrayList<>();
			for (SlmCustomerAddAsk _addAsk : listAddAsk) {
				if(!"2".equals(_addAsk.getAdd_ask_status())){//未评审或者评审不通过
					listAskItem.add(_addAsk.getAsk_sub_itme_val());
				}
			}
			if (listAskItem.size()> 0) {
				msg.setMessage("合同有客户附加要求未评审通过");
				return false;
			}
			
			List<SlmOrdDesignAdditional> addStd=new ArrayList<SlmOrdDesignAdditional>();
			ComBaseAdditional _queryAdd=new ComBaseAdditional();
			_queryAdd.setStd_code(stdCode);
			_queryAdd.setValidflag("1");
			ComBaseAdditionalMapper mapper=this.getMapper(ComBaseAdditionalMapper.class); 
			List<ComBaseAdditional> listBase=mapper.find(_queryAdd, 0, 0);
			//过滤psc
			for (int i = 0; i < listBase.size(); i++) {
				if (listBase.get(i).getPsc()!=null && !listBase.get(i).getPsc().contains(orderLine.getPsc()) ) {
					listBase.remove(i);
					i--;
				}
				
			}
			
			
			
			
			for (int i = 0; i < listBase.size(); i++) {
				
				for (int j = 0; j < listAddAsk.size(); j++) {
					if (listBase.get(i).getAsk_sub_itme().equals(listAddAsk.get(j).getAsk_sub_itme())) {
						listBase.remove(i);
						i--;
						break;
					}
				}
				
			}
			//其他基础项
			for (ComBaseAdditional comBaseAdditional : listBase) {
				SlmOrdDesignAdditional _add=new SlmOrdDesignAdditional();
				try{
					DataHelper.CopyEntity(comBaseAdditional, _add);
					_add.setDesign_key(desginKey);
					_add.setAsk_item(comBaseAdditional.getAsk_itme());
					_add.setStdflag("1");
					addStd.add(_add);
				}catch(Exception ex){
					msg.setMessage(ex.getMessage());
					return false;
				}
			}
			if(addStd.size()>0){
				for (SlmOrdDesignAdditional sd : addStd) {
					this.getMapper(SlmOrdDesignAdditionalMapper.class).insert(sd);
				}
			
			}
			
			for (SlmCustomerAddAsk customerAsk : listAddAsk) {
				SlmOrdDesignAdditional _add=new SlmOrdDesignAdditional();
				try{
					DataHelper.CopyEntity(customerAsk, _add);
					_add.setDesign_key(desginKey);
					_add.setAsk_item(customerAsk.getAsk_itme());
					_add.setStdflag("1");
					_listAddition.add(_add);
				}catch(Exception ex){
					msg.setMessage(ex.getMessage());
					return false;
				}
			}
			this.getMapper(SlmOrdDesignAdditionalMapper.class).insertBatch(_listAddition);
			msg.setMessage("附加要求验证通过");
			return true;
		}
	}
	
}
