/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-02-6
 * 描述            ：化学元素标准表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.metallurgy.impl;

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

import market.Com.impl.ComBaseChemServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.metallurgy.mapper.ComMscStdChemMapper;
import market.Qms.metallurgy.service.ComMscStdChemService;
import market.Qms.metallurgy.vo.ComMscStdChem;

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 = "ComMscStdChemService")
public class ComMscStdChemServiceImpl extends BaseServiceImpl<ComMscStdChem, Object> implements ComMscStdChemService {
	/**
	 * 化学成分标准信息 查询(ComBaseCic cic)
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdChemNext(CommonPage<ComMscStdChem> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdChem> pm = new PageModel<ComMscStdChem>(pageIndex,pageSize);
		try 
		{
			ComMscStdChem cbs = cb.getObject();
			String condition = null;
			if ("".equals(cb.getMemo1())) 
			{
				condition = " CIC = '" + cbs.getCic() + "'";

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

		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	/**
	 * 化学成分标准信息 新增或修改
	 * @param cb (ComMscStdCic cic)
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveChem(List<ComMscStdChem> cbs) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComBaseChemServiceImpl ComBaseChemServiceImpl = this.getBean(market.Com.impl.ComBaseChemServiceImpl.class);
		Date date = new Date();
		try {
			if (cbs != null) 
			{
				for (ComMscStdChem cb : cbs) 
				{
					ComMscStdChem cms = new ComMscStdChem();
					cms.setCic(cb.getCic());
					cms.setIndexSeq(cb.getIndexSeq());
					List<ComMscStdChem> list = this.getMapper(ComMscStdChemMapper.class).find(cms, 0, 0);
					if(!list.isEmpty() && cb.getIndexSeq() != null)
					{
//						Integer findvalid = this.getMapper(ComMscStdChemMapper.class).findvalid(cb.getCic(), cb.getIndexSeq(),cb.getChemName());
//						if(findvalid > 0){
//							rm.setState("500");
//							rm.setMsgInfo("已经存在相同的化学元素!");
//							return rm;
//						}
						//修改
						if(cb.getChemType().equals("B")){
							if(!this.getBean(ComBaseChemServiceImpl.class).complexIsValid(cb.getChemFormula()))
							{
								rm.setState("500");
								rm.setMsgInfo("该化学元素无效!");
								return rm;
							}
						}
						if(cb.getComplexType().equals("1")){
							if(!this.getBean(ComBaseChemServiceImpl.class).complexIsValid(cb.getStdmin()))
							{
								rm.setState("500");
								rm.setMsgInfo("该化学元素下限值无效!");
								return rm;
							}
						}
						if(cb.getComplexType().equals("1")){
							if(!this.getBean(ComBaseChemServiceImpl.class).complexIsValid(cb.getStdmax()))
							{
								rm.setState("500");
								rm.setMsgInfo("该化学元素上限值无效!");
								return rm;
							}
						}
						cb.setUpdateName(userName);
						cb.setUpdateTime(date);
						this.getMapper(ComMscStdChemMapper.class).update(cb);
						rm.setState("200");
						rm.setMsgInfo("修改成功!");
					}else {
//						ComMscStdChem c = new ComMscStdChem();
//						c.setChemName(cb.getChemName());
//						int count = this.getMapper(ComMscStdChemMapper.class).count(c);
//						if(count > 0){
//							rm.setState("500");
//							rm.setMsgInfo("已经存在相同的化学元素!");
//							return rm;
//						}
						if(cb.getChemType().equals("B")){
							if(!this.getBean(ComBaseChemServiceImpl.class).complexIsValid(cb.getChemFormula()))
							{
								rm.setState("500");
								rm.setMsgInfo("该化学元素无效!");
								return rm;
							}}
							Integer index = this.getMapper(ComMscStdChemMapper.class).getIndexNo(cb.getCic());
							cb.setIndexSeq(index);
							cb.setCreateName(userName);
							cb.setCreateTime(date);
							this.getMapper(ComMscStdChemMapper.class).insert(cb);
							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<ComMscStdChem> 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 (ComMscStdChem com : cb) 
				{
					if (com.getValidflag().equals("1")) 
					{
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						com.setValidflag("0");
						this.getMapper(ComMscStdChemMapper.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<ComMscStdChem> cb) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try 
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdChem com : cb) 
				{
					if (com.getValidflag().equals("0")) 
					{
						this.getMapper(ComMscStdChemMapper.class).updatec(com.getCic(), 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;
	}

	enum Status {
		INVALID("0"), RECOVER("1");
		private String value;
		private Status(String val){
			this.value = val;
		}
		public String getValue(){
			return this.value;
		}
	}
}