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.ComMscStdSpecBiasCMapper;
import market.Qms.metallurgy.service.ComMscStdSpecBiasCService;
import market.Qms.metallurgy.vo.ComMscStdSpecBiasC;

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 = "ComMscStdSpecBiasCService")
public class ComMscStdSpecBiasCServiceImpl extends BaseServiceImpl<ComMscStdSpecBiasC, Object>
		implements ComMscStdSpecBiasCService {

	/**
	 * 公差引用标准表(从表) 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdSpecBiasC(CommonPage<ComMscStdSpecBiasC> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdSpecBiasC> pm = new PageModel<ComMscStdSpecBiasC>(pageIndex,pageSize);
		try 
		{   
			String condition = null;
			ComMscStdSpecBiasC cbs = cb.getObject();
			if ("1".equals(cb.getMemo1())) 
			{
				condition = " sic = '" + cbs.getSic() + "'";
			} else {
				condition = " sic = '" + cbs.getSic() + "' and VALIDFLAG <> 0 ";
			}
			pm.setRows(this.getMapper(ComMscStdSpecBiasCMapper.class).findWithCondition(null, condition,"INDEX_SEQ",(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComMscStdSpecBiasCMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
		}
		return rm;
	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdSpecBiasC(List<ComMscStdSpecBiasC> coms) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try 
		{		
			for (ComMscStdSpecBiasC com : coms) 
			{
				if (com.getIndexSeq() != null) 
				{
					if(com.getValidflag().equals("1")) {
						com.setUpdateName(userName);
	//					com.setCreateTime(date);
						com.setUpdateTime(date);
						this.getMapper(ComMscStdSpecBiasCMapper.class).update(com);
						rm.setState("200");
						rm.setMsgInfo("修改成功!");
					} else {
						rm.setState("500");
						rm.setMsgInfo("该数据无效!");
						return rm;
					}
				} else {
					// 联合主键
				
//					if (index != null && com.getSic() != null) 
//					{
						com.setCreateName(userName);
						com.setCreateTime(date);
						com.setValidflag(Status.RECOVER.getValue());// 默认有效
						Integer index = this.getMapper(ComMscStdSpecBiasCMapper.class).getIndexNo(coms.get(0).getSic());
						com.setIndexSeq(index);
						this.getMapper(ComMscStdSpecBiasCMapper.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<ComMscStdSpecBiasC> 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 (ComMscStdSpecBiasC com : cb) 
				{
					if (com.getValidflag().equals("1")) 
					{
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						com.setValidflag("0");
						this.getMapper(ComMscStdSpecBiasCMapper.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<ComMscStdSpecBiasC> cb) 
	{
		ResultModel rm = new ResultModel();
		try {
			if (cb.size() > 0) 
			{
				for (ComMscStdSpecBiasC com : cb) 
				{
					if (com.getValidflag().equals("0")) 
					{
						this.getMapper(ComMscStdSpecBiasCMapper.class).updatef(com.getSic(), com.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;
		}
	}

}
