/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2018.2.5
 * 描述               :        			基础数据信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseMachineMapper;
import market.Com.mapper.ComBaseMachineRStationMapper;
import market.Com.service.ComBaseMachineService;
import market.Com.vo.ComBaseM;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.ComBaseMachineRStation;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="ComBaseMachineService")
public class ComBaseMachineServiceImpl extends BaseServiceImpl<ComBaseMachine,Object> implements ComBaseMachineService {

	/**
	 * 查询数据 memo1有值时不查父当前节点
	 * 
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<ComBaseM> oci) {
		ResultModel rm = new ResultModel();
		int pageIndex = oci.getPageIndex();
		int pageSize = oci.getPageSize();
		try {
			PageModel<ComBaseM> pm = new PageModel<ComBaseM>(
					pageIndex, pageSize);
			ComBaseM sb = oci.getObject() == null ? (new ComBaseM())
					: oci.getObject();
			List<ComBaseMachine> cbm = new ArrayList<ComBaseMachine>();
			List<ComBaseMachineRStation> li = new ArrayList<ComBaseMachineRStation>();
			List<ComBaseM> lis=new ArrayList<ComBaseM>();
			String param = null;
			if (oci.getMemo1().equals("0")) {
				param = "1";
			}
			cbm = this.getMapper(ComBaseMachineMapper.class).doQuery(sb.getMachine_name(), param);
			for(ComBaseMachine c:cbm)
			{
				ComBaseMachineRStation cs = new ComBaseMachineRStation();
				cs.setMachine_name(c.getMachine_name());
				cs.setValidflag(param);

				List<ComBaseMachineRStation> list = this.getMapper(ComBaseMachineRStationMapper.class).find(cs, 0, 0);
				ComBaseM cc = Assemble(c, list);
				cc.setPline_code(c.getPline_code());
				cc.setIs_entrust(c.getIs_entrust());
				lis.add(cc);
			}
			pm.setRows(lis);
			pm.setTotal(lis.size());
			rm.setState("200");
			rm.setData(pm);
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 添加数据 machine_code
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<ComBaseM> oci) {
		ResultModel rm = new ResultModel();
		try {
			ComBaseM bd = oci.getObject();
			String[] code = bd.getProcess_point_code().split("\\,");
			String[] desc = bd.getProcess_point_desc().split("\\,");
			String sWhere = "machine_name = '" + bd.getMachine_name() + "'";
			int num = this.getMapper(ComBaseMachineMapper.class)
					.countWithCondition(null, sWhere);
			if (num > 0) {
				rm.setState("500");
				rm.setMsgInfo("数据库存在相同机台名称");
				return rm;
			}
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			Date date = new Date();
			ComBaseMachine c = new ComBaseMachine();
			c.setCreate_name(userName);
			c.setCreate_time(date);
			c.setMachine_code(this.getMapper(ComBaseMachineMapper.class).GetCode());
			c.setMachine_mark(bd.getMachine_mark());
			c.setMemo(bd.getMemo());
			c.setMachine_name(bd.getMachine_name());
			c.setValidflag(bd.getValidflag());
			c.setPline_code(bd.getPline_code());
			c.setIs_entrust(bd.getIs_entrust());
			this.getMapper(ComBaseMachineMapper.class).insert(c);
			for (int i = 0; i < code.length; i++) {
				ComBaseMachineRStation com = new ComBaseMachineRStation();
				com.setCreate_name(userName);
				com.setCreate_time(date);
				com.setMachine_code(c.getMachine_code());
				com.setMachine_name(bd.getMachine_name());
				com.setMemo(bd.getMemo());
				com.setProcess_point_code(code[i]);
				com.setProcess_point_desc(desc[i]);
				com.setValidflag(bd.getValidflag());
				this.getMapper(ComBaseMachineRStationMapper.class).insert(com);
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("新增失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateValidfla(CommonPage<ComBaseM> oci) {
		ResultModel rm = new ResultModel();
		ComBaseM bd = oci.getObject();
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		String[] code = bd.getProcess_point_code().split("\\,");
		String[] desc = bd.getProcess_point_desc().split("\\,");
		try {
			String sWhere = "machine_name = '" + bd.getMachine_name() + "' and machine_code != '"+bd.getMachine_code()+"'";
			int num = this.getMapper(ComBaseMachineMapper.class)
					.countWithCondition(null, sWhere);
			if (num > 0) {
				rm.setState("500");
				rm.setMsgInfo("数据库存在相同机台名称");
				return rm;
			}
			Date date = new Date();
			ComBaseMachine c = new ComBaseMachine();
			c.setCreate_name(bd.getCreate_name());
			c.setCreate_time(bd.getCreate_time());
			c.setDelete_name(bd.getDelete_name());
			c.setDelete_time(bd.getDelete_time());
			c.setMachine_code(bd.getMachine_code());
			c.setMachine_mark(bd.getMachine_mark());
			c.setMachine_name(bd.getMachine_name());
			c.setMemo(bd.getMemo());
			c.setValidflag(bd.getValidflag());
			c.setPline_code(bd.getPline_code());
			c.setIs_entrust(bd.getIs_entrust());
			ComBaseMachineRStation crs = new ComBaseMachineRStation();
			crs.setCreate_name(bd.getCreate_name());
			crs.setCreate_time(bd.getCreate_time());
			crs.setDelete_name(bd.getDelete_name());
			crs.setDelete_time(bd.getDelete_time());
			crs.setMachine_code(bd.getMachine_code());
			crs.setMachine_name(bd.getMachine_name());
			crs.setMemo(bd.getMemo());
			crs.setValidflag(bd.getValidflag());
			String sql = "machine_code = '"+bd.getMachine_code()+"'";
			int count = this.getMapper(ComBaseMachineMapper.class).countWithCondition(null, sql);
		
			if(count>0){
				c.setUpdate_name(userName);
				c.setUpdate_time(date);
				this.getMapper(ComBaseMachineMapper.class).update(c);
				this.getMapper(ComBaseMachineRStationMapper.class).doDelete(bd.getMachine_code());
				crs.setUpdate_name(userName);
				crs.setUpdate_time(date);
				for(int i=0;i<code.length;i++)
				{
					crs.setProcess_point_code(code[i]);
					crs.setProcess_point_desc(desc[i]);
					this.getMapper(ComBaseMachineRStationMapper.class).insert(crs);
					crs.setProcess_point_code(null);
					crs.setProcess_point_desc(null);
				}
			}else {
				c.setCreate_name(userName);
				c.setCreate_time(date);
				c.setMachine_code(this.getMapper(ComBaseMachineMapper.class).GetCode());
				this.getMapper(ComBaseMachineMapper.class).insert(c);
				crs.setCreate_name(userName);
				crs.setCreate_time(date);
				crs.setMachine_code(c.getMachine_code());
				for(int i=0;i<code.length;i++)
				{
					crs.setProcess_point_code(code[i]);
					crs.setProcess_point_desc(desc[i]);
					this.getMapper(ComBaseMachineRStationMapper.class).insert(crs);
					crs.setProcess_point_code(null);
					crs.setProcess_point_desc(null);
				}
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改作废数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<ComBaseM> oci) {
		ResultModel rm = new ResultModel();
		ComBaseM bd = oci.getObject();
		try {
			// 查询当前名称
			String userName = (String) this.getSession().getAttribute(
					HomeService.SESSION_USER_NAME);
			Date date = new Date();
			ComBaseMachine c = new ComBaseMachine();
			c.setMachine_code(bd.getMachine_code());
			ComBaseMachine cb = this.getMapper(ComBaseMachineMapper.class).find(c, 0, 0).get(0);
			ComBaseMachine cbm = new ComBaseMachine();
			ComBaseMachineRStation crs = new ComBaseMachineRStation();
			if(cb.getValidflag().equals("0")){
				rm.setState("500");
				rm.setMsgInfo("该记录已经作废");
				return rm;
			}
			cbm.setMachine_code(bd.getMachine_code());
			cbm.setValidflag("0");
			cbm.setDelete_name(userName);
			cbm.setDelete_time(date);
			this.getMapper(ComBaseMachineMapper.class).update(cbm);
			crs.setMachine_code(bd.getMachine_code());
			crs.setValidflag("0");
			crs.setDelete_time(date);
			crs.setDelete_name(userName);
			this.getMapper(ComBaseMachineRStationMapper.class).doupdate(crs);
			rm.setState("200");
			rm.setMsgInfo("作废成功!");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("作废失败："+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 动作处理恢复
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateFlagWeb(CommonPage<ComBaseM> oci) {
		ResultModel rm = new ResultModel();
		ComBaseM bd = oci.getObject();
		try {
			ComBaseMachine c = new ComBaseMachine();
			c.setMachine_code(bd.getMachine_code());
			ComBaseMachine co = this.getMapper(ComBaseMachineMapper.class).find(c, 0, 0).get(0);
			if(co.getValidflag().equals("1"))
			{
				rm.setState("500");
				rm.setMsgInfo("该记录已经恢复!");
				return rm;
			}
			this.getMapper(ComBaseMachineMapper.class).DoRecover(bd.getMachine_code());
			this.getMapper(ComBaseMachineRStationMapper.class).DoRecover(bd.getMachine_code());
			rm.setState("200");
			rm.setMsgInfo("恢复成功!");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败!");
			e.printStackTrace();
		}
		return rm;

	}

	/**
	 * 获取机台代号
	 * */
	public ComBaseMachine doGetMark(String machineCode){
		try{
			ComBaseMachine mbmq=new ComBaseMachine();
			mbmq.setMachine_code(machineCode);
			
			ComBaseMachine mbm = this.getMapper(ComBaseMachineMapper.class).doQueryMark(mbmq);
			if(mbm!=null&&mbm.getMachine_mark()!=null){
				String m=mbm.getMachine_mark().trim();
				if(m.length()>2){
					return null;
				}else{
					try{
						if(Integer.parseInt(m)>0&&Integer.parseInt(m)<10){
							mbm.setMachine_mark("0"+m);
//							String mm=String.format("%03d",Integer.parseInt(m));
//							mbm.setMachine_mark(mm);
							
						}
					}catch(Exception e){
						return null;
					}
				}
			}
			return mbm;
		}catch(Exception ex){
			return null;
		}
	}
	/**
	 * 根据机台代码获取机台名称
	 * */
	public ResultModel doGetMachineName(CommonPage<ComBaseMachine> cp){
		ResultModel rm=new ResultModel();
		if(cp==null || cp.getObject().getMachine_code().trim().equals("")){
			rm.setState("500");
			rm.setMsgInfo("请输入正确的机台代码");
			return rm;
		}
		try{
			ComBaseMachine mbm = this.getMapper(ComBaseMachineMapper.class).findById(cp.getObject().getMachine_code());
			if(mbm!=null&&mbm.getMachine_name()!=null){
				rm.setData(mbm);
				rm.setState("200");
			}else{
				rm.setState("500");
				rm.setMsgInfo("没有机台代码【"+cp.getObject().getMachine_code()+"】对应的机台名称");
				return rm;
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setErrorInfo(ex.toString());
			return rm;
		}
		return rm;
	}

	
	
	
	public ComBaseM Assemble(ComBaseMachine a,List<ComBaseMachineRStation>  b){
		ComBaseM cc = new ComBaseM(a);
		String code=null;
		String desc=null;
		for(int i=0;i<b.size();i++)
		{
			if(i == 0)
			{
				code = b.get(0).getProcess_point_code();
				desc = b.get(0).getProcess_point_desc();
continue;
			}
			code+=","+b.get(i).getProcess_point_code();
			desc+=","+b.get(i).getProcess_point_desc();
		}
		cc.setProcess_point_code(code);
		cc.setProcess_point_desc(desc);
		return cc;
	}
	
}
