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.mapper.ComMscStdSpecBiasMapper;
import market.Qms.metallurgy.service.ComMscStdSpecBiasService;
import market.Qms.metallurgy.vo.ComMscStdSpecBias;
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 = "ComMscStdSpecBiasService")
public class ComMscStdSpecBiasServiceImpl extends BaseServiceImpl<ComMscStdSpecBias, String>
		implements ComMscStdSpecBiasService {

	/**
	 * 公差引用标准查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdSpecBias(CommonPage<ComMscStdSpecBias> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdSpecBias> pm = new PageModel<ComMscStdSpecBias>(pageIndex,pageSize);
		try 
		{   
			ComMscStdSpecBias cbs = cb.getObject();
			
			String condition = " 1=1";
			if("0".equals(cb.getMemo1())) {
				condition += " and validflag = '1'";
			}
			if(cbs != null && cbs.getStdName() != null && !"".equals(cbs.getStdName())) {
				condition += " and std_name like '%" +cbs.getStdName()+ "%'";
			}
			if(cbs != null && cbs.getProdTypeCodeM() != null && !"".equals(cbs.getProdTypeCodeM())) {
				condition += " and prod_type_code_m = '" + cbs.getProdTypeCodeM() + "'";
			}
			
			List<ComMscStdSpecBias> cmss = this.getMapper(ComMscStdSpecBiasMapper.class).findWithCondition(null, condition, " CREATE_TIME desc ",(pageIndex - 1) * pageSize, pageSize);
			// 设置元素个数
			for (ComMscStdSpecBias cics : cmss) 
			{
				// 有效记录个数
				ComMscStdSpecBiasC cc = new ComMscStdSpecBiasC();
				cc.setSic(cics.getSic());
				Integer count = this.getMapper(ComMscStdSpecBiasCMapper.class).count(cc);

				cics.setCount(count);

			}
			pm.setRows(cmss);
			pm.setTotal(this.getMapper(ComMscStdSpecBiasMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 公差引用标准批量保存
	 * @param cms
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdSpecBias(List<ComMscStdSpecBias> cmss)
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		String sWhere = " 1=1";
		try
		{
			for (ComMscStdSpecBias cms : cmss) 
			{   
				if(cms.getSic() != null)
				{
					sWhere +=" and std_name = '"+cms.getStdName()+"'"+" and sic != '"+cms.getSic()+"' and PRECISICON_TYPE_CODE='"+cms.getPrecisiconTypeCode()+"'";
					int count = this.getMapper(ComMscStdSpecBiasMapper.class).countWithCondition(null, sWhere);
					if(count == 0) {
						ComMscStdSpecBias com = this.getMapper(ComMscStdSpecBiasMapper.class).findById(cms.getSic());
						if (com != null) 
						{
							cms.setUpdateName(userName);
							cms.setUpdateTime(date);
							this.getMapper(ComMscStdSpecBiasMapper.class).update(cms);
							rm.setState("200");
							rm.setMsgInfo("更新成功");
						}
					} else {
						rm.setState("500");
						rm.setMsgInfo("同一精度类型下标准名称重复!");
						return rm;	
					}
				} else {
					sWhere += " and std_name = '"+cms.getStdName()+"' and PRECISICON_TYPE_CODE='"+cms.getPrecisiconTypeCode()+"'";
					int count = this.getMapper(ComMscStdSpecBiasMapper.class).countWithCondition(null, sWhere);
					if(count == 0) {
						// 获取主键
						String sic = this.getMapper(ComMscStdSpecBiasMapper.class).getMax();
						cms.setSic(sic);
						String stdCode = this.getMapper(ComMscStdSpecBiasMapper.class).getStdCode();
						cms.setStdCode(stdCode);
						cms.setCreateName(userName);
						cms.setCreateTime(new Date());
						cms.setValidflag(Status.RECOVER.getValue());
						if (sic != null) 
						{
							this.getMapper(ComMscStdSpecBiasMapper.class).insert(cms);
							rm.setState("200");
							rm.setMsgInfo("新增成功");
						}
					} else {
						rm.setState("500");
						rm.setMsgInfo("同一精度类型下标准名称重复!");
						return rm;	
					}
				}
			}
		}catch (Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			throw(ex);
		}
		return rm;

	}

//	/**
//	 * 公差引用标准保存
//	 * @param cms
//	 * @return
//	 */
//	@Transactional(value = "tm", rollbackFor = Exception.class)
//	public ResultModel doSaveComMscStdSpecBias(CommonPage<ComMscStdSpecBias> sum)
//	{
//		ResultModel rm = new ResultModel();
//		ComMscStdSpecBias oci = sum.getObject();
//		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
//		Date date = new Date();
//		String sWhere = " 1=1";
//		try
//		{
//			if(oci.getSic() != null)
//			{
//				sWhere +=" and std_name = '"+oci.getStdName()+"'"+" and sic != '"+oci.getSic()+"'";
//				int count = this.getMapper(ComMscStdSpecBiasMapper.class).countWithCondition(null, sWhere);
//				if(count == 0) {
//					ComMscStdSpecBias com = this.getMapper(ComMscStdSpecBiasMapper.class).findById(oci.getSic());
//					if (com != null) 
//					{
//						oci.setUpdateName(userName);
//						oci.setUpdateTime(date);
//						this.getMapper(ComMscStdSpecBiasMapper.class).update(oci);
//						rm.setState("200");
//						rm.setMsgInfo("更新成功");
//					}
//				} else {
//					rm.setState("500");
//					rm.setMsgInfo("标准名称不能重复!");
//					return rm;	
//				}
//			} else {
//				sWhere += " and std_name = '"+oci.getStdName()+"'";
//				int count = this.getMapper(ComMscStdSpecBiasMapper.class).countWithCondition(null, sWhere);
//				if(count == 0) {
//					// 获取主键
//					String sic = this.getMapper(ComMscStdSpecBiasMapper.class).getMax();
//					oci.setSic(sic);
//					oci.setCreateName(userName);
//					oci.setCreateTime(new Date());
//					oci.setValidflag(Status.RECOVER.getValue());
//					if (sic != null) 
//					{
//						this.getMapper(ComMscStdSpecBiasMapper.class).insert(oci);
//						rm.setState("200");
//						rm.setMsgInfo("新增成功");
//					}
//				} else {
//					rm.setState("500");
//					rm.setMsgInfo("标准名称重复!");
//					return rm;	
//				}
//			}
//		}catch (Exception ex){
//			rm.setState("500");
//			rm.setMsgInfo("操作失败!");
//			throw(ex);
//		}
//		return rm;
//
//	}
	
	/**
	 * 信息废除A
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInvalidA(List<ComMscStdSpecBias> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdSpecBias css : cb) 
				{
					ComMscStdSpecBias st = new ComMscStdSpecBias();
					st.setSic(css.getSic());
					ComMscStdSpecBias com = this.getMapper(ComMscStdSpecBiasMapper.class).find(st, 0, 0).get(0);
					if (com.getValidflag().equals("1")) 
					{
						com.setValidflag("0");
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						ComMscStdSpecBiasC ComMscStdSpecBiasC = new ComMscStdSpecBiasC();
						ComMscStdSpecBiasC.setSic(com.getSic());
						List<ComMscStdSpecBiasC> list = this.getMapper(ComMscStdSpecBiasCMapper.class).find(ComMscStdSpecBiasC, 0,0);
						this.getMapper(ComMscStdSpecBiasMapper.class).update(com);
						if (!list.isEmpty()) 
						{
							for (ComMscStdSpecBiasC c : list) 
							{
								if (c.getValidflag().equals("1")) 
								{
									c.setDeleteName(userName);
									c.setDeleteTime(date);
									c.setValidflag("0");
									this.getMapper(ComMscStdSpecBiasCMapper.class).update(c);
								}
							}
						} 
					} else {
						rm.setState("500");
						rm.setMsgInfo("已是废除状态!");
					}
				}
			}
			rm.setState("200");
			rm.setMsgInfo("索引信息 废除成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	/**
	 * 信息恢复A
	 * @param cb
	 * @return
	 */
	public ResultModel doRecoverA(List<ComMscStdSpecBias> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		ComMscStdSpecBiasC ComMscStdSpecBiasC = new ComMscStdSpecBiasC();
		try
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdSpecBias css : cb) 
				{
					ComMscStdSpecBias st = new ComMscStdSpecBias();
					st.setSic(css.getSic());
					ComMscStdSpecBias com = this.getMapper(ComMscStdSpecBiasMapper.class).find(st, 0, 0).get(0);
					if (com.getValidflag().equals("0")) 
					{
						this.getMapper(ComMscStdSpecBiasMapper.class).updates(com.getSic());;
						ComMscStdSpecBiasC.setSic(com.getSic());
						List<ComMscStdSpecBiasC> list = this.getMapper(ComMscStdSpecBiasCMapper.class).find(ComMscStdSpecBiasC, 0,0);
						if (!list.isEmpty()) 
						{
							for (ComMscStdSpecBiasC c : list) 
							{
								if (c.getValidflag().equals("0")) 
								{
									this.getMapper(ComMscStdSpecBiasMapper.class).updatec(c.getSic(), c.getIndexSeq());
								}
							}
						}

					}else {
						rm.setState("500");
						rm.setMsgInfo("记录已经恢复！");
						return rm;
					}
				}
			}

			rm.setState("200");
			rm.setMsgInfo("索引信息 恢复成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	enum Status {
		INVALID("0"), RECOVER("1");
		private String value;
		private Status(String val) {
			this.value = val;
		}
		public String getValue() {
			return this.value;
		}
	}
	
	/**
	 * 作废引用项目
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel abolishItems(List<ComMscStdSpecBiasC> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try
		{
			if (cb==null || cb.size()<1) {
				rm.setState("500");
				rm.setMsgInfo("请选择需要作废的引用公差标准!");
				return rm;
			}
			
			
			if (cb.size() > 0) 
			{
				for (ComMscStdSpecBiasC css : cb) 
				{	
					css.setDeleteName(userName);
					css.setDeleteTime(date);
					css.setValidflag("0");
					this.getMapper(ComMscStdSpecBiasCMapper.class).update(css);
					
				}
			}
			rm.setState("200");
			rm.setMsgInfo("公差引用标准 废除成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 *恢复引用项目
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel recoveryItems(List<ComMscStdSpecBiasC> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		try
		{
			if (cb==null || cb.size()<1) {
				rm.setState("500");
				rm.setMsgInfo("请选择需要恢复的引用公差标准!");
				return rm;
			}
			
			
			if (cb.size() > 0) 
			{
				for (ComMscStdSpecBiasC css : cb) 
				{		
					this.getMapper(ComMscStdSpecBiasMapper.class).updatec(css.getSic(), css.getIndexSeq());
					
				}
			}
			rm.setState("200");
			rm.setMsgInfo("公差引用标准恢复成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	
	
}
