package market.Qms.com.service.impl;

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.com.mapper.BaseDeliverystateMapper;
import market.Qms.com.mapper.BaseInfoMapper;
import market.Qms.com.mapper.MscItemMapper;
import market.Qms.com.mapper.MscMapper;
import market.Qms.com.mapper.PscMapper;
import market.Qms.com.mapper.PscRDeliveryMapper;
import market.Qms.com.mapper.PscRMscMapper;
import market.Qms.com.service.PscRDeliveryService;
import market.Qms.com.service.PscROrgService;
import market.Qms.com.service.PscService;
import market.Qms.com.vo.BaseDeliverystate;
import market.Qms.com.vo.BaseInfo;
import market.Qms.com.vo.Msc;
import market.Qms.com.vo.MscItem;
import market.Qms.com.vo.Psc;
import market.Qms.com.vo.PscRDelivery;
import market.Qms.com.vo.PscRMsc;
import market.Qms.com.vo.PscROrg;
import market.Qms.common.DataHelper;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
@RemoteService(path ="PscService")
@Transactional(value="tm",rollbackFor=Exception.class)
public class PscServiceImpl extends BaseServiceImpl<Psc, String> implements PscService {
	

	@Override
	public ReturnMsg<List<Psc>> GetPsc(RequestModel<Psc> psc) {
		// TODO Auto-generated method stu
		ReturnMsg<List<Psc>> msg=new ReturnMsg<>();
		try{
			PscMapper mapper=this.getMapper(PscMapper.class);
			String condition="";
			if(!DataHelper.IsNullOrSpace(psc.getModel().getPscDesc())){
				condition+=" PSC_DESC like '%"+psc.getModel().getPscDesc()+"%'";
				psc.getModel().setStdName(null);
			}
			int count=mapper.countWithCondition(psc.getModel(),condition);
			List<Psc> list=mapper.findWithCondition(psc.getModel(), condition, "psc DESC", psc.getSkipResults(), psc.getPageSize());
			for (Psc _psc : list) {
				String deNames="";
				for (PscRDelivery delivery : _psc.getListState()) {
					if(delivery.getValidflag().equals("0")){
						continue;
					}
					deNames+=delivery.getDeliveryStateDesc()+";";
				}
				_psc.setDeliveryNames(deNames);
				String orgNames="";
				for (PscROrg org : _psc.getListOrg()) {
					if(org.getValidflag().equals("0")){
						continue;
					}
					orgNames+=org.getOrgName()+";";
				}
				_psc.setOrgNames(orgNames);			
			}
			msg.setTotal(count);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);
			msg.setResult(list);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	/**
	 * 创建冶金规范项目明细
	 * @param msc
	 */
	private void CreateMscItem(Msc msc){
		BaseInfo info=new BaseInfo();
		info.setValidflag("1");
		info.setSortCode("4030");
		List<BaseInfo> list=this.getMapper(BaseInfoMapper.class).find(info, 0, 0);
		int indexSeq=1;
		for (BaseInfo baseInfo : list) {
			MscItem item=new MscItem();
			item.setCreateName(msc.getCreateName());
			item.setCreateTime(msc.getCreateTime());
			item.setIndexSeq(indexSeq);
			item.setItem(baseInfo.getBaseCode());
			item.setMsc(msc.getMsc());
			if(msc.getMsc().endsWith("00")){
				item.setItemC(baseInfo.getBaseCode()+"01");
			}else{
				item.setItemC(baseInfo.getBaseCode()+"02");
			}
			item.setValidflag("1");
			this.getMapper(MscItemMapper.class).insert(item);
			indexSeq++;
		}
	}
	
	/**
	 * 创建冶金规范与产品码关联表
	 * @param msc
	 * @param psc
	 */
	private void CreateMscRPsc(Msc msc,Psc psc){
		PscRMsc rMsc=new PscRMsc();
		rMsc.setCreateTime(new Date());
		rMsc.setCreateName(psc.getCreateName());
		rMsc.setMsc(msc.getMsc());
		rMsc.setMscDesc(msc.getMscDesc());
		rMsc.setPsc(psc.getPsc());
		rMsc.setPscDesc(psc.getPscDesc());
		rMsc.setAlphaCode(msc.getAlphaCode());
		rMsc.setAlphaName(msc.getAlphaName());
		rMsc.setValidflag("1");
		
		this.getMapper(PscRMscMapper.class).insert(rMsc);
	}
	
	/**
	 * 创建冶金规范码
	 * @param pscObj
	 */
	private Msc CreateMsc(Psc pscObj){
		Msc mscEntity=new Msc();
		String useCode = pscObj.getStdCode() + "000";
		mscEntity.setAlphaCode(useCode);
		mscEntity.setAlphaName("/");
	//	mscEntity.setAllowNk("0");
		mscEntity.setCreateName(pscObj.getCreateName());
		mscEntity.setCreateTime(pscObj.getCreateTime());
		mscEntity.setCreateName(pscObj.getCreateName());
		mscEntity.setDeptCode(pscObj.getDeptCode());
		mscEntity.setDeptDesc(pscObj.getDeptName());
		mscEntity.setMsc(pscObj.getPsc().replaceAll("PSC", "MSC") + "000");
		mscEntity.setMscDesc(pscObj.getPscDesc());
		mscEntity.setMscStyle(pscObj.getProdType());
		mscEntity.setNewProcCode(pscObj.getNewProdCode());
		mscEntity.setNewProcDesc(pscObj.getNewProdDesc());
		mscEntity.setProdCode(pscObj.getProdCode());
		mscEntity.setProdLine(pscObj.getProdLine());
		mscEntity.setProdName(pscObj.getProdName());
		mscEntity.setStdCode(pscObj.getStdCode());
		mscEntity.setStdName(pscObj.getStdName());
		mscEntity.setStdStyleCode(pscObj.getStdStyleCode());
		mscEntity.setStdStyleDesc(pscObj.getStdStyleDesc());
		mscEntity.setSteelCode(pscObj.getSteelCode());
		mscEntity.setSteelName(pscObj.getSteelName());
		//mscEntity.setPsc(pscObj.getPsc());
		//mscEntity.setPscDesc(pscObj.getPscDesc());
		mscEntity.setUnitCodeProcess(pscObj.getSectionCode());
		mscEntity.setUnitDescProcess(pscObj.getSectionName());
		mscEntity.setSectionCode(pscObj.getSectionCode());
		mscEntity.setSectionName(pscObj.getSectionName());
		mscEntity.setUserCount(1);
		this.getMapper(MscMapper.class).insert(mscEntity);
		return mscEntity;
	}
	
	private void CreatePscOrg(Psc psc){
		PscROrg org=new PscROrg();
		org.setCreateName(psc.getCreateName());
		BaseInfo info=this.getMapper(BaseInfoMapper.class).findById("409601");
		org.setOrgCode(info.getBaseCode());
		org.setOrgName( info.getBaseName());
		org.setPsc(psc.getPsc());
		org.setValidflag("1");
		this.getService(PscROrgService.class).SavePscROrg(org);
	}
	
	@Override
	public ReturnMsg<Psc> SaveStdPsc(Psc psc) {
		ReturnMsg<Psc> msg=new ReturnMsg<>();
		msg.setResult(psc);
		try{
			
			if (psc!=null) {
				if (psc.getMemo()!=null && !"".equals(psc.getMemo())) {
					if (psc.getMemo().length()>500) {
						msg.setSuccess(false);
						msg.setMessage("备注信息超长，请确认");
						msg.setResult(psc);
						return msg;
					}
				}
				
			}
			
			
			
			PscMapper mapper=this.getMapper(PscMapper.class);
			Psc _psc=new Psc();
			_psc.setPscDesc(psc.getPscDesc());
			_psc.setValidflag("1");
			List<Psc> _list=mapper.find(_psc, 0, 0);
			if(_list.size()>0&&!_list.get(0).getPsc().equals(psc.getPsc())){
				msg.setSuccess(false);
				msg.setMessage("已存在相同的产品规范码");
				msg.setResult(psc);
				return msg;
			}
			if(DataHelper.IsNullOrSpace(psc.getPsc())){
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode,"PSC", 10);
				psc.setPsc(code);
				psc.setCreateTime(new Date());				
				mapper.insert(psc);
				Msc msc=CreateMsc(psc);
				CreateMscRPsc(msc,psc);
				CreateMscItem(msc);
				CreatePscOrg(psc);
			}else{
				psc.setUpdateTime(new Date());
				mapper.update(psc);
			}
			this.getMapper(PscRDeliveryMapper.class).DelOrRenewByPsc(psc.getPsc());
			if(!DataHelper.IsNullOrSpace(psc.getDeliveryStateCode())){
				String[] stateCodes=psc.getDeliveryStateCode().trim().split(",");
				BaseDeliverystateMapper dMapper=this.getMapper(BaseDeliverystateMapper.class);
				List<PscRDelivery> list=new ArrayList<>();
				for (String str : stateCodes) {
					if("".equals(str)){
						continue;
					}
					BaseDeliverystate state=dMapper.findById(str);
					PscRDelivery delivery=new PscRDelivery();
					delivery.setDeliveryStateCode(state.getDeliveryStateCode());
					delivery.setDeliveryStateDesc(state.getDeliveryStateDesc());
					delivery.setPsc(psc.getPsc());
					delivery.setPscDesc(psc.getPscDesc());
					delivery.setValidflag("1");
					list.add(delivery);
				}
				this.getService(PscRDeliveryService.class).InsertSteel(list);
			}
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(psc);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
			
		}
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenew(String deleteName,String psc, Boolean flag) {
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			Psc _psc=new Psc();
			_psc.setPsc(psc);
			_psc.setValidflag(flag?"0":"1");
			if(flag){
				PscRMsc msc=new PscRMsc();
				msc.setPsc(psc);
				msc.setValidflag("1");
				List<PscRMsc> list=this.getMapper(PscRMscMapper.class).find(msc, 0, 0);
				if(list.size()>0){
					msg.setSuccess(false);
					msg.setMessage("已有冶金规范码与其相关联，不能进行作废操作");;
					return msg;
				}
				_psc.setDeleteTime(new Date());
				_psc.setDeleteName(deleteName);
			}else{
				_psc.setUpdateTime(new Date());
				_psc.setUpdateName(deleteName);
			}
			this.getMapper(PscMapper.class).update(_psc);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}
	
	
	/**
	 * 根据标准号查询psc
	 */
	
	public ResultModel getPscByStdCode(CommonPage<Psc> oci) {
		
		ResultModel rm=new ResultModel();
		
		try {
			
			List<Psc>  listPsc=new ArrayList<Psc>();
			if (oci!=null) {
				
				if (oci.getObject()!=null) {
					
					if (oci.getObject().getStdCode()!=null) {
						listPsc=this.getMapper(PscMapper.class).findWithCondition(null, "STD_CODE='"+oci.getObject().getStdCode()+"'", null, 0, 0);
					}
					
				}
				
			}
			rm.setData(listPsc);
			rm.setState("200");
			
		} catch (Exception e) {
			rm.setState("500");
		}
		
		
		
		
		return rm;
	}
	
	/**
	 * 修改交货状态
	 */
	public ReturnMsg<Psc> updateDelivery(Psc psc) {
		ReturnMsg<Psc> msg = new ReturnMsg<>();
		msg.setResult(psc);

		try {

			this.getMapper(PscRDeliveryMapper.class).DelOrRenewByPsc(psc.getPsc());
			if (!DataHelper.IsNullOrSpace(psc.getDeliveryStateCode())) {
				String[] stateCodes = psc.getDeliveryStateCode().trim().split(",");
				BaseDeliverystateMapper dMapper = this.getMapper(BaseDeliverystateMapper.class);
				List<PscRDelivery> list = new ArrayList<>();
				for (String str : stateCodes) {
					if ("".equals(str)) {
						continue;
					}
					BaseDeliverystate state = dMapper.findById(str);
					PscRDelivery delivery = new PscRDelivery();
					delivery.setDeliveryStateCode(state.getDeliveryStateCode());
					delivery.setDeliveryStateDesc(state.getDeliveryStateDesc());
					delivery.setPsc(psc.getPsc());
					delivery.setPscDesc(psc.getPscDesc());
					delivery.setValidflag("1");
					list.add(delivery);
				}
				this.getService(PscRDeliveryService.class).InsertSteel(list);
			}
			msg.setMessage("修改交货状态成功");
			msg.setSuccess(true);
			msg.setResult(psc);

		} catch (Exception e) {
			msg.setSuccess(false);
			msg.setMessage(e.getMessage());
		}

		return msg;
	}
	
}
