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

import market.Com.mapper.ComBaseChemRatioMapper;
import market.Com.service.ComBaseChemRatioService;
import market.Com.vo.ComBaseChemRatio;
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="ComBaseChemRatioService")
public class ComBaseChemRatioServiceImpl extends BaseServiceImpl<ComBaseChemRatio,Object> implements ComBaseChemRatioService {
	/**
	 * 查询数据
	 * memo1有值时不查父当前节点
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<ComBaseChemRatio> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<ComBaseChemRatio> pm = new PageModel<ComBaseChemRatio>(pageIndex,pageSize);
			ComBaseChemRatio sb=sum.getObject()==null?(new ComBaseChemRatio()):sum.getObject();
			String sWhere=" 1=1 ";//sort_code='1001' ";

			if(sum.getMemo1().equals("0"))
	        {sWhere += " and validflag = '1'";
	        }
			pm.setRows(this.getMapper(ComBaseChemRatioMapper.class).findWithCondition(null, sWhere, " create_name desc ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseChemRatioMapper.class).countWithCondition(null,sWhere));
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<ComBaseChemRatio> sum) {
		ResultModel rm=new ResultModel();
		List<ComBaseChemRatio> list = sum.getObjectList();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String sWhere = " 1 = 1 ";
		String sql = sWhere;
		try {
			
			for(ComBaseChemRatio bd:list){
				sWhere += " and chem_code = '"+bd.getChem_code()
				+"' and group_text = '"+bd.getGroup_text()
				+"' and ratio_code = '"+bd.getRatio_code()+"'";
				int count =this.getMapper(ComBaseChemRatioMapper.class).countWithCondition(null, sWhere);
				if(count == 0){
					bd.setCreate_name(userName);
					bd.setCreate_time(new Date());
					this.getMapper(ComBaseChemRatioMapper.class).insert(bd);
				}else {
					bd.setUpdate_name(userName);
					bd.setUpdate_time(new Date());
					this.getMapper(ComBaseChemRatioMapper.class).updateByIndex(bd);
				}
				sWhere = sql;
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<ComBaseChemRatio> sum) {
		ResultModel rm=new ResultModel();
		List<ComBaseChemRatio> list = sum.getObjectList();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String sWhere = " 1 = 1 ";
		String sql = sWhere;
		try {
			
			for(ComBaseChemRatio bd:list){
				sWhere += " and chem_code = '"+bd.getChem_code()
				+"' and group_text = '"+bd.getGroup_text()
				+"' and ratio_code = '"+bd.getRatio_code()+"'";
				int count =this.getMapper(ComBaseChemRatioMapper.class).countWithCondition(null, sWhere);
				if(count == 0){
					rm.setState("500");
					rm.setMsgInfo("数据库不存在对应记录!");	
					return rm;
				}else {
					bd.setUpdate_name(userName);
					bd.setUpdate_time(new Date());
					this.getMapper(ComBaseChemRatioMapper.class).updateByIndex(bd);
				}
				sWhere = sql;
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}
	/**
	 * 作废数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateNo(CommonPage<ComBaseChemRatio> oci) {
		ResultModel rm=new ResultModel();
		ComBaseChemRatio bd = oci.getObject();
		String sWhere = " 1=1";
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			ComBaseChemRatio cbi= new ComBaseChemRatio();
			sWhere += " and chem_code = '"+bd.getChem_code()
			+"' and group_text = '"+bd.getGroup_text()
			+"' and ratio_code = '"+bd.getRatio_code()+"'";
			int count = this.getMapper(ComBaseChemRatioMapper.class).countWithCondition(null, sWhere);
			// TODO: handle exception
			if(count >0)
			{ 
				cbi = this.getMapper(ComBaseChemRatioMapper.class).findWithCondition(null, sWhere, null, 0, 0).get(0);
				if(cbi.getValidflag().equals("1")){
					cbi.setValidflag("0");
					cbi.setUpdate_name(userName);
					this.getMapper(ComBaseChemRatioMapper.class).updateByIndex(cbi);
					
				}else {
					rm.setState("500");
					rm.setMsgInfo("该记录已经作废!");	
					return rm;
				}
				
			}else {
				rm.setState("500");
				rm.setMsgInfo("数据库里面没有对应记录!");	
				return rm;
			}
				rm.setState("200");	
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
	}	
	
	/**
	 * 动作恢复
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateYes(CommonPage<ComBaseChemRatio> oci) {
		ResultModel rm=new ResultModel();
		ComBaseChemRatio bd = oci.getObject();
		String sWhere = " 1=1";
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			ComBaseChemRatio cbi= new ComBaseChemRatio();
			sWhere += " and chem_code = '"+bd.getChem_code()
			+"' and group_text = '"+bd.getGroup_text()
			+"' and ratio_code = '"+bd.getRatio_code()+"'";
			int count = this.getMapper(ComBaseChemRatioMapper.class).countWithCondition(null, sWhere);
			// TODO: handle exception
			if(count >0)
			{ 
				cbi = this.getMapper(ComBaseChemRatioMapper.class).findWithCondition(null, sWhere, null, 0, 0).get(0);
				if(cbi.getValidflag().equals("0")){
					cbi.setValidflag("1");
					cbi.setUpdate_name(userName);
					this.getMapper(ComBaseChemRatioMapper.class).updateByIndex(cbi);
					
				}else {
					rm.setState("500");
					rm.setMsgInfo("该记录已经作废!");	
					return rm;
				}
				
			}else {
				rm.setState("500");
				rm.setMsgInfo("数据库里面没有对应记录!");	
				return rm;
			}
				rm.setState("200");	
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
}
}
