/***文档注释***********************************************
 * 作者            ： 朱泽鹏
 * 创建日期     ：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.ComMscStdDetectionCMapper;
import market.Qms.metallurgy.service.ComMscStdDetectionCService;
import market.Qms.metallurgy.vo.ComMscStdDetectionC;

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 = "ComMscStdDetectionCService")
public class ComMscStdDetectionCServiceImpl extends BaseServiceImpl<ComMscStdDetectionC, Object> implements ComMscStdDetectionCService {
	/**
	 * 探伤标准信息 查询
	 * 
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdDetectionC(CommonPage<ComMscStdDetectionC> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdDetectionC> pm = new PageModel<ComMscStdDetectionC>(pageIndex, pageSize);
		try 
		{
			ComMscStdDetectionC cbs = cb.getObject();
			String condition = null;
			if ("".equals(cb.getMemo1())) 
			{
				condition = " DIC = '" + cbs.getDic() + "'";

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

	/**
	 * 探伤标准信息 保存
	 * @param com
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdDetectionC(List<ComMscStdDetectionC> coms) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
//		ComMscStdDetectionC ComMscStdDetectionC = new ComMscStdDetectionC();
		try {
			Date date = new Date();
			for (ComMscStdDetectionC com : coms) 
			{
//				ComMscStdDetectionC.setDic(com.getDic());
//				ComMscStdDetectionC.setIndexSeq(com.getIndexSeq());
				//联合主键
//				List<ComMscStdDetectionC> comm = this.getMapper(ComMscStdDetectionCMapper.class).find(ComMscStdDetectionC, 0, 0);
				if (com.getIndexSeq() != null) 
				{
					com.setUpdateName(userName);
					com.setUpdateTime(date);
					this.getMapper(ComMscStdDetectionCMapper.class).update(com);
					rm.setState("200");
					rm.setMsgInfo("更新成功!");
				} else {
					Integer index = this.getMapper(ComMscStdDetectionCMapper.class).getIndexNo(com.getDic());
						com.setCreateName(userName);
						com.setCreateTime(new Date());
						com.setIndexSeq(index);
						this.getMapper(ComMscStdDetectionCMapper.class).insert(com);
						rm.setState("200");
						rm.setMsgInfo("新增成功!");
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;

	}

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