/***文档注释***********************************************
 * 作者            ：      易鑫
 * 创建日期     ：2018-02-6
 * 描述            ：生产产线适用规格表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.metallurgy.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.metallurgy.mapper.ComMscPlineRProductMapper;
import market.Qms.metallurgy.mapper.ComMscPlineRSpecMapper;
import market.Qms.metallurgy.mapper.ComMscPlineRSteelMapper;
import market.Qms.metallurgy.service.ComMscPlineRSpecService;
import market.Qms.metallurgy.vo.ComMscPlineRProduct;
import market.Qms.metallurgy.vo.ComMscPlineRSpec;
import market.Qms.metallurgy.vo.ComMscPlineRSteel;

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="ComMscPlineRSpecService")
public class ComMscPlineRSpecServiceImpl extends BaseServiceImpl<ComMscPlineRSpec, Object> implements ComMscPlineRSpecService {

	/**
	 * 查询
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryWebBySQl(CommonPage<ComMscPlineRSpec> cp)
	{
		ResultModel rm = new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<ComMscPlineRSpec> pm = new PageModel<ComMscPlineRSpec>(pageIndex,pageSize);
		String State = "";
		try {
			if(cp.getMemo1().equals("0"))
			{
				State =""; 
			}
			else
			{
				State ="1"; 
			}
			pm.setRows(this.getMapper(ComMscPlineRSpecMapper.class).doQueryWebBySQL
					((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, cp.getObject().getPline_Name(),cp.getObject().getWellName(),State));
			pm.setTotal(this.getMapper(ComMscPlineRSpecMapper.class).doQueryWebBySQLCount
					( cp.getObject().getPline_Name(),cp.getObject().getWellName(),State));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 新增
	 * @param cp
	 * @return
	 */
	public ResultModel doAddSpec(CommonPage<ComMscPlineRSpec> cp) {
		ResultModel rm =new ResultModel();
		try {
			ComMscPlineRSpec spec =cp.getObject();
			int is =this.getMapper(ComMscPlineRSpecMapper.class).doQueryById(spec.getPline_Code());
			if (is==0) {
				//获取序号
				Integer seq =this.getMapper(ComMscPlineRSpecMapper.class).GetMaxIndexNo();
				if(seq==null){
					seq=1;
				}
				String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				spec.setCreateName(username);
				spec.setCreateTime(new Date());
				spec.setIndexSeq(seq);
				spec.setValidflag("1");
				//插入生产产线适用规格表
				this.getMapper(ComMscPlineRSpecMapper.class).insert(spec);
				//插入产线适用品名关联关系表  COM_MSC_PLINE_R_PRODUCT
				String ProdCode =spec.getProdCode();
				String ProdName =spec.getProdName();
				if (ProdCode==null||"".equals(ProdCode)) {

				}else {
					List<String> procode =Arrays.asList(ProdCode.split(";"));
					List<String> proname =Arrays.asList(ProdName.split(";"));
					for (int i = 0; i < procode.size(); i++) {
						ComMscPlineRProduct product =new ComMscPlineRProduct();
						product.setIndexSeq(i+1);
						product.setValidflag("1");
						if (spec.getPline_Code()!=null) {
							product.setPlineCode(spec.getPline_Code());
						}
						product.setProdCode(procode.get(i));
						product.setProdName(proname.get(i));
						this.getMapper(ComMscPlineRProductMapper.class).insert(product);
					}
				}
				//插入产线适用规格与适用牌号对应关系表 COM_MSC_PLINE_R_STEEL
				String steelCode =spec.getSteelCode();
				String steelName =spec.getSteelName();
				if (steelCode==null||"".equals(steelCode)) {

				}else {
					List<String> stcode =Arrays.asList(steelCode.split(";"));
					List<String> stname =Arrays.asList(steelName.split(";"));
					for (int i = 0; i < stcode.size(); i++) {
						ComMscPlineRSteel steel =new ComMscPlineRSteel();
						steel.setIndexSeq(i+1);
						steel.setValidflag("1");
						if (spec.getPline_Code()!=null) {
							steel.setPlineCode(spec.getPline_Code());
						}
						steel.setSteelCode(stcode.get(i));
						steel.setSteelName(stname.get(i));
						this.getMapper(ComMscPlineRSteelMapper.class).insert(steel);
					}
				}
				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("不能添加已有产线");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			rm.setMsgInfo("新增失败");
			rm.setState("500");
		}
		return rm;
	}

	/**
	 * 修改
	 */
	public ResultModel doExitSpec(CommonPage<ComMscPlineRSpec> cp) {
		ResultModel rm =new ResultModel();
		ComMscPlineRSpec spec =cp.getObject();
		try {
			int is =this.getMapper(ComMscPlineRSpecMapper.class).doQueryById(spec.getPline_Code());
			int iss=this.getMapper(ComMscPlineRSpecMapper.class).doQueryByIds(spec.getPline_Code(),spec.getIndexSeq());
			if ((is==1&&iss==1)||is==0) {
				spec.setValidflag("1");
				spec.setUpdateTime(new Date());
				//修改生产产线适用规格表
				String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				spec.setUpdateName(username);
				spec.setUpdateTime(new Date());
				this.getMapper(ComMscPlineRSpecMapper.class).update(spec);
				//先删除产线适用品名关联关系表  COM_MSC_PLINE_R_PRODUCT
				ComMscPlineRProduct duct =new ComMscPlineRProduct();
				duct.setPlineCode(spec.getPline_Code());
				if (duct.getPlineCode()!=null) {
					String id =duct.getPlineCode();
					this.getMapper(ComMscPlineRSpecMapper.class).doExitDuct(id);
				}
				//在新增产线适用品名关联关系表  COM_MSC_PLINE_R_PRODUCT
				String ProdCode =spec.getProdCode();
				String ProdName =spec.getProdName();
				if (ProdCode==null||"".equals(ProdCode)) {
				}else {
					List<String> procode =Arrays.asList(ProdCode.split(";"));
					List<String> proname =Arrays.asList(ProdName.split(";"));
					for (int i = 0; i < procode.size(); i++) {
						ComMscPlineRProduct product =new ComMscPlineRProduct();
						product.setIndexSeq(i+1);
						product.setValidflag("1");
						product.setUpdateTime(new Date());
						if (spec.getPline_Code()!=null) {
							product.setPlineCode(spec.getPline_Code());
						}
						product.setProdCode(procode.get(i));
						product.setProdName(proname.get(i));
						this.getMapper(ComMscPlineRProductMapper.class).insert(product);
					}
				}
				//先删除产线适用规格与适用牌号对应关系表 COM_MSC_PLINE_R_STEEL
				ComMscPlineRSteel steels =new ComMscPlineRSteel();
				steels.setPlineCode(spec.getPline_Code());
				if (steels.getPlineCode()!=null) {
					String id =steels.getPlineCode();
					this.getMapper(ComMscPlineRSpecMapper.class).doExitsteels(id);
				}
				//在新增产线适用规格与适用牌号对应关系表 COM_MSC_PLINE_R_STEEL
				String steelCode =spec.getSteelCode();
				String steelName =spec.getSteelName();
				if (steelCode==null||"".equals(steelCode)) {
				}else {
					List<String> stcode =Arrays.asList(steelCode.split(";"));
					List<String> stname =Arrays.asList(steelName.split(";"));
					for (int i = 0; i < stcode.size(); i++) {
						ComMscPlineRSteel steel =new ComMscPlineRSteel();
						steel.setUpdateTime(new Date());
						steel.setIndexSeq(i+1);
						steel.setValidflag("1");
						if (spec.getPline_Code()!=null) {
							steel.setPlineCode(spec.getPline_Code());
						}
						steel.setSteelCode(stcode.get(i));
						steel.setSteelName(stname.get(i));
						this.getMapper(ComMscPlineRSteelMapper.class).insert(steel);
					}
				}
				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("产线已存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			rm.setMsgInfo("修改失败");
			rm.setState("500");
		}
		return rm;
	}

	/**
	 * 作废
	 * @param cp
	 * @return
	 */
	public ResultModel doSpecZf(CommonPage<ComMscPlineRSpec> cp) {
		ResultModel rm =new ResultModel();
		ComMscPlineRSpec spec =cp.getObject();
		try {
			spec.setValidflag("0");
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			spec.setDeleteName(username);
			spec.setDeleteTime(new Date());
			this.getMapper(ComMscPlineRSpecMapper.class).update(spec);
			ComMscPlineRProduct product =new ComMscPlineRProduct();
			product.setPlineCode(spec.getPline_Code());
			product.setValidflag("0");
			this.getMapper(ComMscPlineRProductMapper.class).update(product);
			ComMscPlineRSteel steel =new ComMscPlineRSteel();
			steel.setPlineCode(spec.getPline_Code());
			steel.setValidflag("0");
			this.getMapper(ComMscPlineRSteelMapper.class).update(steel);
			rm.setState("200");
		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("作废失败");
		}
		return rm;
	}

	/**
	 * 恢复
	 * @param cp
	 * @return
	 */
	public ResultModel doSpechf(CommonPage<ComMscPlineRSpec> cp) {
		ResultModel rm =new ResultModel();
		try {
			ComMscPlineRSpec spec =cp.getObject();
			spec.setValidflag("1");
			this.getMapper(ComMscPlineRSpecMapper.class).doUpdateHf(spec.getPline_Code());
			ComMscPlineRProduct product =new ComMscPlineRProduct();
			product.setPlineCode(spec.getPline_Code());
			product.setValidflag("1");
			this.getMapper(ComMscPlineRProductMapper.class).update(product);
			ComMscPlineRSteel steel =new ComMscPlineRSteel();
			steel.setPlineCode(spec.getPline_Code());
			steel.setValidflag("1");
			this.getMapper(ComMscPlineRSteelMapper.class).update(steel);
			rm.setState("200");
		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			rm.setState("恢复失败");
			// TODO: handle exception
		}
		return rm;
	}

}
