/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-08-04
 * 描述            ：合同行工艺信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.ContractManagement.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Api.Qms.impl.QmsApiServiceImpl;
import market.Com.mapper.ComBaseStationMapper;
import market.Com.pb.PbMethod;
import market.Com.vo.ComBaseStation;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ContractManagement.mapper.SlmContractBaseStationMapper;
import market.Slm.ContractManagement.service.SlmContractBaseStationService;
import market.Slm.ContractManagement.vo.SlmContractBaseStation;
import market.Slm.OrderManagement.mapper.SlmOrderInfoMapper;
import market.Slm.OrderManagement.vo.SlmOrderInfo;

import org.springframework.beans.BeanUtils;
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="SlmContractBaseStationService")
public class SlmContractBaseStationServiceImpl extends BaseServiceImpl<SlmContractBaseStation,Object> implements SlmContractBaseStationService {

	/**
	 * 订单维护-工艺要求-保存
	 * @throws Exception 
	 * memo1 订单号
	 * */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel doSaveWeb(CommonPage<ComBaseStation> sum) throws Exception{
		ResultModel rm=new ResultModel();
		List<ComBaseStation> stationList=sum.getObjectList();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			if(stationList==null){
				throw new Exception("请传入参数！");
			}
			SlmOrderInfo order=this.getMapper(SlmOrderInfoMapper.class).findById(sum.getMemo1());				
			if(order==null){
				throw new Exception("未找到对应的订单");
			}
			for(ComBaseStation station:stationList){
				SlmContractBaseStation bStaion=new SlmContractBaseStation();
				bStaion.setOrder_no(sum.getMemo1());
				bStaion.setProcess_point_code(station.getProcess_point_code());
				// 如果是同样的【工序点名称】，则 删除 再增加，否则增加
				//20201210 存在相同的工序点信息，如  YT-402020110226 酸洗-冷轧-退火-冷轧
				/*List<SlmContractBaseStation> stList=this.getMapper(SlmContractBaseStationMapper.class).find(bStaion, 0, 0);
				for(SlmContractBaseStation st:stList){
					this.getMapper(SlmContractBaseStationMapper.class).delete(st.getSeq());
				}*/

				BeanUtils.copyProperties(station, bStaion);
				bStaion.setSeq(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
				bStaion.setCreate_name(userName);
				bStaion.setCreate_time(new Date());
				bStaion.setDelete_name(null);
				bStaion.setDelete_time(null);
				bStaion.setUpdate_name(null);
				bStaion.setUpdate_time(null);
				bStaion.setContract_no(order.getContract_no());
				bStaion.setContract_line_seq(order.getContract_line_seq());
				bStaion.setOrder_seq(order.getOrder_seq());
				this.getMapper(SlmContractBaseStationMapper.class).insert(bStaion);
			}
			updateOrder(order.getOrder_no());
			rm.setState("200");
			rm.setMsgInfo("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("工艺要求保存失败！"+e.getMessage());
		}
		return rm;
	}

	/**
	 * 查询工艺要求
	 * memo1 订单号
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmContractBaseStation> sum){
		ResultModel rm=new ResultModel();
		List<SlmContractBaseStation> list=new ArrayList<SlmContractBaseStation>();
		try {
			SlmOrderInfo order=this.getMapper(SlmOrderInfoMapper.class).findById(sum.getMemo1());		
			if(order!=null){
				SlmContractBaseStation st=new SlmContractBaseStation();
				st.setOrder_no(sum.getMemo1());
				list=this.getMapper(SlmContractBaseStationMapper.class).findWithCondition(st, null, " rnum,seq ",0,0);
				if(list.size()<1){
					st=new SlmContractBaseStation();
					st.setContract_no(order.getContract_no());
					st.setContract_line_seq(order.getContract_line_seq());
					st.setOrder_seq(order.getOrder_seq());
					list=this.getMapper(SlmContractBaseStationMapper.class).findWithCondition(st, null, " rnum,seq ",0,0);
				}
			}
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}

	/**
	 * 合同维护-工艺要求-删除
	 * @throws Exception 
	 */
	public ResultModel doDeleteWeb(CommonPage<SlmContractBaseStation> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try {
			SlmContractBaseStation st=sum.getObject();
			st=this.getMapper(SlmContractBaseStationMapper.class).findById(st.getSeq());
			if(st==null){
				throw new Exception("请传入参数！");
			}
			this.getMapper(SlmContractBaseStationMapper.class).delete(st.getSeq());
			updateOrder(st.getOrder_no());
			rm.setState("200");
			rm.setMsgInfo("删除成功！");
		} catch (Exception e) {
			throw new Exception("删除失败！"+e.getMessage());
		}
		return rm;
	}

	public  void updateOrder(String order_no) throws Exception{
		try {
		SlmOrderInfo order=this.getMapper(SlmOrderInfoMapper.class).findById(order_no);		
		if(order!=null){
			SlmContractBaseStation st=new SlmContractBaseStation();
			st.setOrder_no(order_no);
			List<SlmContractBaseStation> list=this.getMapper(SlmContractBaseStationMapper.class).findWithCondition(st, null, " seq",0,0);
			if(list.size()<1){
				st=new SlmContractBaseStation();
				st.setContract_no(order.getContract_no());
				st.setContract_line_seq(order.getContract_line_seq());
				st.setOrder_seq(order.getOrder_seq());
				 list=this.getMapper(SlmContractBaseStationMapper.class).findWithCondition(st, null, " rnum ",0,0);
			}
			String process="";
			int num=1;
			for(SlmContractBaseStation station:list){
				process+=num+""+station.getProcess_point_desc()+"-";
				num++;
			}
			if(process.length()>1)
				process=process.substring(0,process.length()-1);
			order=new SlmOrderInfo();
			order.setProcess(process);
			order.setOrder_no(order_no);
			this.getMapper(SlmOrderInfoMapper.class).update(order);
		}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	//工艺要求自动新增
	public ResultModel setProcessParam(String process, String order_no) throws Exception {
		ResultModel rm=new ResultModel();
		try{
		if(process==null||"".equals(process)) return rm;
		if(process.length()>0){
			String[] list;
			if(process.contains(";")){ list=process.split(";");
			}else if(process.contains("；")){ list=process.split("；");
			}else if(process.contains("，")){ list=process.split("，");
			}else if(process.contains(",")){ list=process.split(",");
			}else if(process.contains("-")){ list=process.split("-");
			}else if(process.contains("/")){ list=process.split("/");
			}else if(process.contains("\\")){ list=process.split("\\");
			}else{ list=new String[]{process};}
			//删除工艺要求
			SlmContractBaseStation station=new SlmContractBaseStation();
			station.setOrder_no(order_no);
			List<SlmContractBaseStation> delete_list=this.getMapper(SlmContractBaseStationMapper.class).find(station, 0, 0);
			for(SlmContractBaseStation stations:delete_list){
				this.getMapper(SlmContractBaseStationMapper.class).delete(stations.getSeq());
			}
			
			List<ComBaseStation> all_list=new ArrayList<ComBaseStation>();
			for(String point:list){			//找对应的工序点
				String type=this.getBean(QmsApiServiceImpl.class).getPROCESS_POINT_TYPE();
				List<ComBaseStation> station_list=this.getMapper(ComBaseStationMapper.class).findWithCondition(null, "PROCESS_POINT_TYPE='"+type+"' and process_desc='"+point.trim()+"' and nvl(VALIDFLAG, '0') = '1' ", "PROCESS_DESC", 0,0);
				if(station_list.size()>0){
					all_list.addAll(station_list);
				}
			}
			CommonPage<ComBaseStation> sum=new CommonPage<ComBaseStation>();
			sum.setObjectList(all_list);
			sum.setMemo1(order_no);
			rm=doSaveWeb(sum);
			rm.setState("200");
		}
		}catch(Exception e){
			rm.setMsgInfo(e.getMessage());
			rm.setState("500");
		}
		return rm;
	}
}
