/***文档注释***********************************************
 * 作者            ： 朱泽鹏
 * 创建日期     ：2018-02-6
 * 描述            ：材质标准表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.metallurgy.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.metallurgy.mapper.ComMscStdPhyMapper;
import market.Qms.metallurgy.service.ComMscStdPhyService;
import market.Qms.metallurgy.vo.ComMscPhySample;
import market.Qms.metallurgy.vo.ComMscStdPhy;

import org.springframework.transaction.annotation.Transactional;

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 = "ComMscStdPhyService")
public class ComMscStdPhyServiceImpl extends BaseServiceImpl<ComMscStdPhy, Object> implements ComMscStdPhyService {
	/**
	 * 材质标准表 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdPhy(CommonPage<ComMscStdPhy> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdPhy> pm = new PageModel<ComMscStdPhy>(pageIndex,pageSize);
		try 
		{
			ComMscStdPhy cbs = cb.getObject();
			String condition = null;
			if ("".equals(cb.getMemo1())) 
			{
				condition = " pic = '" + cbs.getPic() + "'";

			} else {
				condition = " pic = '" + cbs.getPic() + "' and VALIDFLAG <> 0 ";
			}
	
			pm.setRows(this.getMapper(ComMscStdPhyMapper.class).findWithCondition(null, condition," INDEX_SEQ",
								(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComMscStdPhyMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");

		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	public ResultModel doQueryComMscStdPhy2(CommonPage<ComMscStdPhy> cb) {
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdPhy> pm = new PageModel<ComMscStdPhy>(pageIndex, pageSize);
		try {
			ComMscStdPhy cbs = cb.getObject();
			String condition = null;
			if ("".equals(cb.getMemo1())) {
				condition = " pic = '" + cbs.getPic() + "'";

			} else {
				condition = " pic = '" + cbs.getPic() + "' and VALIDFLAG <> 0 ";
			}

			List<ComMscStdPhy> phys = this.getMapper(ComMscStdPhyMapper.class).findWithCondition(null, condition,
					" INDEX_SEQ", (pageIndex - 1) * pageSize, pageSize);

			// 去重
			String oldP = null;
			String newP = null;
			List<ComMscStdPhy> phyOnlyOne = new ArrayList<ComMscStdPhy>();
			for (int i = 0; i < phys.size(); i++) {
				if (i == 0) {
					phyOnlyOne.add(phys.get(i));
					continue;
				}
				oldP = phys.get(i).getPhyCodeM() + phys.get(i).getItemCodeD() + phys.get(i).getItemCodeS()
						+ phys.get(i).getItemCodeT();

				for (int j = 0; j <phyOnlyOne.size(); j++) {//C0003I0001I0005I0003

					newP = phyOnlyOne.get(j).getPhyCodeM() + phyOnlyOne.get(j).getItemCodeD() + phyOnlyOne.get(j).getItemCodeS()
							+ phyOnlyOne.get(j).getItemCodeT();
					if (oldP.equals(newP)) {
						break;
					}

					if (j == phyOnlyOne.size() - 1) {
						phyOnlyOne.add(phys.get(i));
					}

				}

			}
			
			//遍历集合
			
			for (ComMscStdPhy phy : phyOnlyOne) {
				CommonPage<ComMscPhySample> cp=new CommonPage<ComMscPhySample>();
				ComMscPhySample smple=new ComMscPhySample();
				smple.setPic(phy.getPic());
				smple.setPhy_code_m(phy.getPhyCodeM());
				String CodeD=phy.getItemCodeD()==null?"null":phy.getItemCodeD();
				String CodeS=phy.getItemCodeS()==null?"null":phy.getItemCodeS();
				String CodeT=phy.getItemCodeT()==null?"null":phy.getItemCodeT();
				String sample_key=phy.getPhyCodeM()+"_"+CodeD+"_"+CodeT+"_"+CodeS;
				smple.setSample_key(sample_key);
				cp.setMemo1("1");
				cp.setObject(smple);
				PageModel<ComMscPhySample> ps=(PageModel<ComMscPhySample>) this.getBean(ComMscPhySampleServiceImpl.class).doQueryComMscStdDetection(cp).getData();

				phy.setPhySamples(ps.getRows());
				
				
				
			}
			
			
			

			pm.setRows(phyOnlyOne);
			pm.setTotal(phyOnlyOne.size());
			rm.setData(pm);
			rm.setState("200");

		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	

	/**
	 * 材质标准表 保存
	 * @param cms
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdPhy(List<ComMscStdPhy> cmss) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComMscStdPhy cbs = new ComMscStdPhy();
		Date date = new Date();
		try 
		{
			if(!cmss.isEmpty()){
			
			for (ComMscStdPhy cms : cmss) 
			{
				cbs.setIndexSeq(cms.getIndexSeq());
				cbs.setPic(cms.getPic());
				List<ComMscStdPhy> com = this.getMapper(ComMscStdPhyMapper.class).find(cbs, 0, 0);
				if (cms.getIndexSeq() != null) 
				{
					cms.setUpdateName(userName);
					cms.setUpdateTime(date);
					this.getMapper(ComMscStdPhyMapper.class).update(cms);
					rm.setState("200");
					rm.setMsgInfo("修改成功!");
				} else {
					// 连和主键
					
//					if (index != null && cms.getPic() != null) {
						cms.setCreateName(userName);
						cms.setCreateTime(date);
						Integer index = this.getMapper(ComMscStdPhyMapper.class).getIndexNo(cmss.get(0).getPic());
						cms.setIndexSeq(index);
						this.getMapper(ComMscStdPhyMapper.class).insert(cms);
						rm.setState("200");
						rm.setMsgInfo("新增成功!");
//					}
				}
			}
			}else {
				rm.setMsgInfo("不存在勾选的数据!");
				rm.setState("500");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
		}
		return rm;

	}

	/**
	 * 信息废除B
	 * 
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInvalidB(List<ComMscStdPhy> cb) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try 
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdPhy css : cb) 
				{
					ComMscStdPhy ct = new ComMscStdPhy();
					ct.setPic(css.getPic());
					ct.setIndexSeq(css.getIndexSeq());
					ComMscStdPhy com = this.getMapper(ComMscStdPhyMapper.class).find(ct, 0, 0).get(0);
					if (com.getValidflag().equals("1")) 
					{
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						com.setValidflag("0");
						this.getMapper(ComMscStdPhyMapper.class).update(com);
						rm.setState("200");
						rm.setMsgInfo("作废成功!");
					}
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("作废失败!");
			ex.getStackTrace();
		}
		return rm;

	}

	/**
	 * 信息恢复B
	 * 
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doRecoverB(List<ComMscStdPhy> cb) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try 
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdPhy css : cb) 
				{
					ComMscStdPhy ct = new ComMscStdPhy();
					ct.setPic(css.getPic());
					ct.setIndexSeq(css.getIndexSeq());
					ComMscStdPhy com = this.getMapper(ComMscStdPhyMapper.class).find(ct, 0, 0).get(0);
					if (com.getValidflag().equals("0")) 
					{
												
						this.getMapper(ComMscStdPhyMapper.class).updateno(com.getPic(), com.getIndexSeq());;
						
					}
				}
				rm.setState("200");
				rm.setMsgInfo("恢复成功!");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败!");
		}
		return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doGroup(List<ComMscStdPhy> list) 
	{
		ResultModel rm = new ResultModel();
//		int count = 0;
		String findmax="";
		try 
		{
			if(!list.isEmpty())
			{
			if (list.get(0).getPsc()==null ||"".equals(list.get(0).getPsc())) {
					rm.setState("500");
					rm.setMsgInfo("请传入PSC");
					return rm;
			}
//			Integer max = -1000000;
//		for(ComMscStdPhy cm:list)
//		{
//			ComMscStdPhy cst = new ComMscStdPhy();
//			cst.setPic(cm.getPic());
//			cst.setIndexSeq(cm.getIndexSeq());
//			ComMscStdPhy com = this.getMapper(ComMscStdPhyMapper.class).find(cst, 0, 0).get(0);
//			if(com.getGroupSeq() != null)
//			{
//				if(Integer.parseInt(com.getGroupSeq())>max)
//				{
//					max = Integer.parseInt(com.getGroupSeq());
//				}
//			}else {
//				count ++;
//			}
//		}
//		max = max+1;
		if(this.getMapper(ComMscStdPhyMapper.class).findnumpsc(list.get(0).getPsc())==0){
			findmax = "1";
		}else {
			findmax = this.getMapper(ComMscStdPhyMapper.class).findmaxpsc(list.get(0).getPsc());
				int n = Integer.parseInt(findmax);
				n = n + 1;
				findmax = new Integer(n).toString();	
		}
		
		
		for(ComMscStdPhy c:list)
		{
			ComMscStdPhy cst = new ComMscStdPhy();
			cst.setPic(c.getPic());
			cst.setIndexSeq(c.getIndexSeq());
			ComMscStdPhy com = this.getMapper(ComMscStdPhyMapper.class).find(cst, 0, 0).get(0);
//			if(count == list.size()){
				com.setGroupSeq(findmax);
//			}else {
//				com.setGroupSeq(max.toString());	
//			}
		this.getMapper(ComMscStdPhyMapper.class).update(com);
		}
			}
			rm.setState("200");
			rm.setMsgInfo("分组成功！");
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("分组失败!");
	}
	return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doCancelGroup(List<ComMscStdPhy> list) 
	{
		ResultModel rm = new ResultModel();
		try 
		{
			if(!list.isEmpty())
			{
		for(ComMscStdPhy cm:list)
		{ComMscStdPhy c = new ComMscStdPhy();
		c.setPic(cm.getPic());
		c.setIndexSeq(cm.getIndexSeq());
		ComMscStdPhy specC = this.getMapper(ComMscStdPhyMapper.class).find(c, 0, 0).get(0);
		if(specC.getGroupSeq() == null)
		{
			rm.setState("500");
			rm.setMsgInfo("该记录中包含着无效的记录分组！");
			return rm;
		}
			this.getMapper(ComMscStdPhyMapper.class).updatec(cm.getPic(), cm.getIndexSeq());;
		}
					}
			rm.setState("200");
			rm.setMsgInfo("取消分组成功！");
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("取消分组失败!");
	}
	return rm;
	}
	enum Status {
		INVALID("0"), RECOVER("1");
		private String value;

		private Status(String val) {
			this.value = val;
		}

		public String getValue() {
			return this.value;
		}
	}
}