/***文档注释***********************************************
 * 作者               :                   朱泽鹏
 * 创建日期      :                   2018.2.5
 * 描述               :        			复合元素系数接口
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;
import java.util.Date;

import market.Com.mapper.ComBaseChemMapper;
import market.Com.service.ComBaseChemService;
import market.Com.vo.ComBaseChem;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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="ComBaseChemService")
public class ComBaseChemServiceImpl extends BaseServiceImpl<ComBaseChem,Object> implements ComBaseChemService {
	/**
	 * WEB 化学元素管理：查询 
	 * @param cp 
	 * memo1  
	 * @return
	 */
	public ResultModel doQueryPhyBWeb(CommonPage<ComBaseChem> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseChem> pm = new PageModel<ComBaseChem>(pageIndex,pageSize);
		try {
			ComBaseChem cbp = cp.getObject();
			String condition = " 1=1";
			ComBaseChem Org = new ComBaseChem();
			Org.setChem_name(cbp.getChem_name());
			if("0".equals(cp.getMemo1())){
				condition += " and validflag = '1'";
			}
			if(cbp != null && cbp.getChem_name() != null && !"".equals(cbp.getChem_name()))
			{condition += " and chem_name like '%" +cbp.getChem_name()+ "%'";}
			pm.setRows(this.getMapper(ComBaseChemMapper.class).findWithCondition(null, condition, "CHEM_CODE",
							(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseChemMapper.class).countWithCondition(null, condition));
//			this.getMapper(ComBaseChemMapper.class).findById(arg0)
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
		/**
	 * 新增化学元素
	 * 
	 * @param bd
	 * @return
	 */	
	public ResultModel doAddWeb(CommonPage<ComBaseChem> sum) {
		ResultModel rm = new ResultModel();
//		List<ComBa>
		ComBaseChem oci = sum.getObject();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String sWhere = " 1=1";
		try{
			
			if(oci.getChem_name() != null)
			{
				if(oci.getRound_off_digits() != null)
				{
					sWhere += " and chem_name = '"+oci.getChem_name()+"'";
					int count = this.getMapper(ComBaseChemMapper.class).countWithCondition(null, sWhere);
					if(count == 0){
						int num =this.getMapper(ComBaseChemMapper.class).countWithCondition(null, " 1 = 1");
//						if(num == 0 ){
//							oci.setChem_code("A0001");
//						}else {
							oci.setChem_code(this.getMapper(ComBaseChemMapper.class).GetMaxCode());
//						}
						if(oci.getChem_type().equals("B")){
						boolean bol=complexIsValid(oci.getChem_formula());
						
						if(!bol)
						{
							rm.setState("500");
							rm.setMsgInfo("复合元素中的单一元素"+Sinent+"不存在!");
							return rm;	
						}
						}
						oci.setCreate_time(new Date());
						oci.setCreate_name(userName);
						this.getMapper(ComBaseChemMapper.class).insert(oci);
					}else {
						rm.setState("500");
						rm.setMsgInfo("化学元素描述重复!");
						return rm;	
					}
				}else {
					rm.setState("500");
					rm.setMsgInfo("俢约位数为空!");
					return rm;
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("化学元素描述为空!");
				return rm;
			}
			
			
			rm.setState("200");
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("新增失败!");
		ex.printStackTrace();
	}
	return rm;
		
	}
	
	
	/**
	 * 修改化学元素
	 * 
	 * @param bd
	 * @return
	 */	
	public ResultModel doUpdaetWeb(CommonPage<ComBaseChem> sum) {
		ResultModel rm = new ResultModel();
		ComBaseChem oci = sum.getObject();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String sWhere = " 1 = 1";
	try{
	
		if(oci.getChem_type().equals("B")){
			boolean bol=complexIsValid(oci.getChem_formula());
		if(!bol)
		{
			rm.setState("500");
			rm.setMsgInfo("复合元素中的单一元素不存在!");
			return rm;	
		}}
	if(oci.getValidflag().equals("1"))
	{     
//		ComBaseChem cbc = new ComBaseChem();
//		cbc.setChem_code(oci.getChem_code());
//		ComBaseChem c = this.getMapper(ComBaseChemMapper.class).find(cbc, 0, 0).get(0);
		
		sWhere +=" and chem_name = '"+oci.getChem_name()+"'"+" and chem_code != '"+oci.getChem_code()+"'";
		int count = this.getMapper(ComBaseChemMapper.class).countWithCondition(null, sWhere);
		if(count == 0){
			oci.setUpdate_name(userName);
			oci.setUpdate_time(new Date());
			this.getMapper(ComBaseChemMapper.class).update(oci);
		}else {
			rm.setState("500");
			rm.setMsgInfo("化学元素名称不能重复!");
			return rm;	
		}
		rm.setState("200");
		
	}else {
		rm.setState("500");
		rm.setMsgInfo("该数据无效!");
		return rm;
	}
	
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("修改失败!");
		ex.printStackTrace();
	}
	return rm;
	}
	
	/**
	 * 修改作废数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateNo(CommonPage<ComBaseChem> oci) {
		ResultModel rm=new ResultModel();
		ComBaseChem bd = oci.getObject();
		try {
			//查询当前名称
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			ComBaseChem cbi=this.getMapper(ComBaseChemMapper.class).findById(bd.getChem_code());
			if(cbi  != null&& cbi.getValidflag().equals("1")){
				ComBaseChem sba=new ComBaseChem();
				sba = cbi;
				sba.setValidflag("0");
				sba.setDelete_name(userName);
				sba.setDelete_time(new Date());
				this.getMapper(ComBaseChemMapper.class).update(sba);
		        rm.setState("200");	
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("作废失败!");
			e.printStackTrace();
		}
		return rm;
	}		
	/**
	 * 动作恢复
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateYes(CommonPage<ComBaseChem> oci) {
		ResultModel rm=new ResultModel();
		ComBaseChem bd = oci.getObject();
		try {
			//查询当前名称
			ComBaseChem cbi=this.getMapper(ComBaseChemMapper.class).findById(bd.getChem_code());
			if(cbi  != null&& cbi.getValidflag().equals("0")){
				ComBaseChem sba=new ComBaseChem();
				sba = cbi;
				sba.setValidflag("1");
				sba.setDelete_name(null);
				sba.setDelete_time(null);
				this.getMapper(ComBaseChemMapper.class).doRecovery(sba.getChem_code());
		        rm.setState("200");	
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败!");
			e.printStackTrace();
		}
		return rm;
	}
public boolean IsNumber(String str){
		String regex = "^[+-]?[0123456789]*[.]?[0123456789]*$";
		return str.matches(regex);
	}

//不存在的单一元素
private String Sinent;

public boolean complexIsValid(String txt)
{
	txt=txt.replaceAll("\\d", "");
	String[] s = txt.trim().replace(" ","").split("[\\^\\+\\-\\*\\/\\(\\)]");
	String elements = "''";
    int count = 0;
    
    for (int i = 0; i < s.length; i++)
    {
    	String sWhere = " 1=1";
        if (s[i].trim() == "") continue;

        if(!IsNumber(s[i])){
        	if (s[i].toUpperCase() == "AS")
                s[i] = "AS2";
        	
        sWhere +=" and chem_name = '"+s[i]+"'";
    	 count = this.getMapper(ComBaseChemMapper.class).countWithCondition(null, sWhere);

    	 if(count ==0)
    	 {
    		 Sinent=s[i];
    		 return false; 
    	 }
        }
        
    }
	return true;
}
}
