/***文档注释***********************************************
 * 作者            ： 朱泽鹏
 * 创建日期     ：2018-02-6
 * 描述            ：公差标准表(从表)
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.metallurgy.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.metallurgy.mapper.ComMscStdSpecCMapper;
import market.Qms.metallurgy.service.ComMscStdSpecCService;
import market.Qms.metallurgy.vo.ComMscStdSpecC;

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 = "ComMscStdSpecCService")
public class ComMscStdSpecCServiceImpl extends BaseServiceImpl<ComMscStdSpecC, Object> implements ComMscStdSpecCService {
	/**
	 * 公差标准表(从表) 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdSpecC(CommonPage<ComMscStdSpecC> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdSpecC> pm = new PageModel<ComMscStdSpecC>(pageIndex,pageSize);
		try 
		{   
			String condition = null;
			ComMscStdSpecC cbs = cb.getObject();
			if ("".equals(cb.getMemo1())) 
			{
				condition = " sic = '" + cbs.getSic() + "'";

			} else {
				condition = " sic = '" + cbs.getSic() + "' and VALIDFLAG <> 0 ";
			}
			pm.setRows(this.getMapper(ComMscStdSpecCMapper.class).findWithCondition(null, condition,"INDEX_SEQ",(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComMscStdSpecCMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			rm.setErrorInfo(ex.getMessage());
		}
		return rm;
	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdSpecC(List<ComMscStdSpecC> coms) 
	{
		ResultModel rm = new ResultModel();
		ComMscStdSpecC ComMscStdSpecC = new ComMscStdSpecC();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try 
		{
			
			for (ComMscStdSpecC com : coms) 
			{
				ComMscStdSpecC.setSic(com.getSic());
				ComMscStdSpecC.setIndexSeq(com.getIndexSeq());
				List<ComMscStdSpecC> comm = this.getMapper(ComMscStdSpecCMapper.class).find(com, 0, 0);
				if (com.getIndexSeq() != null) 
				{
					com.setUpdateName(userName);
//					com.setCreateTime(date);
					com.setUpdateTime(date);
					this.getMapper(ComMscStdSpecCMapper.class).update(com);
					rm.setState("200");
					rm.setMsgInfo("修改成功!");
				} else {
					// 联合主键
				
//					if (index != null && com.getSic() != null) 
//					{
						com.setCreateName(userName);
						com.setCreateTime(date);
						com.setValidflag(Status.RECOVER.getValue());// 默认有效
						Integer index = this.getMapper(ComMscStdSpecCMapper.class).getIndexNo(coms.get(0).getSic());
						com.setIndexSeq(index);
						this.getMapper(ComMscStdSpecCMapper.class).insert(com);
						rm.setState("200");
						rm.setMsgInfo("新增成功!");
//					}
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
		}
		return rm;
	}

	/**
	 * 信息废除B
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInvalidB(List<ComMscStdSpecC> 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 (ComMscStdSpecC com : cb) 
				{
					if (com.getValidflag().equals("1")) 
					{
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						com.setValidflag("0");
						this.getMapper(ComMscStdSpecCMapper.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<ComMscStdSpecC> cb) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			if (cb.size() > 0) 
			{
				for (ComMscStdSpecC com : cb) 
				{
					if (com.getValidflag().equals("0")) 
					{
						
						this.getMapper(ComMscStdSpecCMapper.class).updatef(com.getSic(), 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<ComMscStdSpecC> 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(ComMscStdSpecC cm:list)
//		{
//			ComMscStdSpecC cst = new ComMscStdSpecC();
//			cst.setSic(cm.getSic());
//			cst.setIndexSeq(cm.getIndexSeq());
//			ComMscStdSpecC com = this.getMapper(ComMscStdSpecCMapper.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(ComMscStdSpecCMapper.class).findnumpsc(list.get(0).getPsc()) == 0){
					findmax = "1";
				}else {
					// findmax = this.getMapper(ComMscStdSpecCMapper.class).findmax(list.get(0).getSic());
					 findmax = this.getMapper(ComMscStdSpecCMapper.class).findmaxpsc(list.get(0).getPsc());	
					int n = Integer.parseInt(findmax);
						n = n + 1;
						findmax = new Integer(n).toString();
				}
	
		for(ComMscStdSpecC c:list)
		{
			ComMscStdSpecC cst = new ComMscStdSpecC();
			cst.setSic(c.getSic());
			cst.setIndexSeq(c.getIndexSeq());
			ComMscStdSpecC com = this.getMapper(ComMscStdSpecCMapper.class).find(cst, 0, 0).get(0);
//			if(count == list.size()){
				com.setGroupSeq(findmax);
//			}else {
//				com.setGroupSeq(max.toString());	
//			}
		this.getMapper(ComMscStdSpecCMapper.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<ComMscStdSpecC> list) 
	{
		ResultModel rm = new ResultModel();
		try 
		{
			if(!list.isEmpty())
			{
		for(ComMscStdSpecC cm:list)
		{
			ComMscStdSpecC c = new ComMscStdSpecC();
			c.setSic(cm.getSic());
			c.setIndexSeq(cm.getIndexSeq());
			ComMscStdSpecC specC = this.getMapper(ComMscStdSpecCMapper.class).find(c, 0, 0).get(0);
			if(specC.getGroupSeq()==null)
			{
				rm.setState("500");
				rm.setMsgInfo("该记录中包含着无效的记录分组！");
				return rm;
			}
			this.getMapper(ComMscStdSpecCMapper.class).updatec(cm.getSic(), 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;
		}
	}

}
