package market.Com.impl;

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

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.mapper.ComBaseTolMapper;
import market.Com.service.ComBaseTolService;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ComBaseTol;
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="ComBaseTolService")
public class ComBaseTolServiceImpl extends BaseServiceImpl<ComBaseTol, Object> implements ComBaseTolService {

	/**
	 * WEB 公差项目管理：查询 
	 * @param cp 
	 * memo1  
	 * @return
	 */
	public ResultModel doQueryTolWeb(CommonPage<ComBaseTol> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseTol> pm = new PageModel<ComBaseTol>(pageIndex,pageSize);
		try {
			ComBaseTol cbp = cp.getObject();
			String condition = " 1=1";
			if("0".equals(cp.getMemo1())) {
				condition += " and validflag = '1'";
			}
			if(cbp != null && cbp.getTol_name() != null && !"".equals(cbp.getTol_name())) {
				condition += " and tol_name like '%" +cbp.getTol_name()+ "%'";
			}
			pm.setRows(this.getMapper(ComBaseTolMapper.class).findWithCondition(null, condition, "TOL_CODE",
							(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseTolMapper.class).countWithCondition(null, condition));
			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<ComBaseTol> sum) {
		ResultModel rm = new ResultModel();

		ComBaseTol oci = sum.getObject();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String sWhere = " 1=1";
		try{
			
			if(oci.getTol_name() != null)
			{
				sWhere += " and tol_name = '"+oci.getTol_name()+"'";
				int count = this.getMapper(ComBaseTolMapper.class).countWithCondition(null, sWhere);
				if(count == 0){
					oci.setTol_code(this.getMapper(ComBaseTolMapper.class).GetMaxCode());
					oci.setCreate_time(new Date());
					oci.setCreate_name(userName);
					this.getMapper(ComBaseTolMapper.class).insert(oci);
				}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 doUpdateWeb(CommonPage<ComBaseTol> sum) {
		ResultModel rm = new ResultModel();
		ComBaseTol oci = sum.getObject();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String sWhere = " 1 = 1";
		try{
		
			if(oci.getValidflag().equals("1"))
			{     		
				sWhere +=" and tol_name = '"+oci.getTol_name()+"'"+" and tol_code != '"+oci.getTol_code()+"'";
				int count = this.getMapper(ComBaseTolMapper.class).countWithCondition(null, sWhere);
				if(count == 0){
					oci.setUpdate_name(userName);
					oci.setUpdate_time(new Date());
					this.getMapper(ComBaseTolMapper.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<ComBaseTol> oci) {
		ResultModel rm=new ResultModel();
		ComBaseTol bd = oci.getObject();
		try {
			//查询当前名称
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			ComBaseTol cbi=this.getMapper(ComBaseTolMapper.class).findById(bd.getTol_code());
			if(cbi  != null&& cbi.getValidflag().equals("1")){
				ComBaseTol sba=new ComBaseTol();
				sba = cbi;
				sba.setValidflag("0");
				sba.setDelete_name(userName);
				sba.setDelete_time(new Date());
				this.getMapper(ComBaseTolMapper.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<ComBaseTol> oci) {
		ResultModel rm=new ResultModel();
		ComBaseTol bd = oci.getObject();
		try {
			//查询当前名称
			ComBaseTol cbi=this.getMapper(ComBaseTolMapper.class).findById(bd.getTol_code());
			if(cbi  != null&& cbi.getValidflag().equals("0")){
				ComBaseTol sba=new ComBaseTol();
				sba = cbi;
				sba.setValidflag("1");
				sba.setDelete_name(null);
				sba.setDelete_time(null);
				this.getMapper(ComBaseTolMapper.class).doRecovery(sba.getTol_code());
		        rm.setState("200");	
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败!");
			e.printStackTrace();
		}
		return rm;
	}
	/**
	 * 获取精度等级下拉框数据
	 * @return
	 */
    public ResultModel getTolName(){
    	ResultModel  rm=new ResultModel();
    	try {
			List<ComBaseInfo> listT = this.getMapper(ComBaseInfoMapper.class).findWithCondition(null," SORT_CODE='4041' AND VALIDFLAG='1'", null, 0, 0);
		    rm.setData(listT);
		    rm.setState("200");
    	} catch (Exception e) {
			// TODO: handle exception
    		rm.setState("500");
    		rm.setMsgInfo("查询精度等级信息失败");
    		rm.setErrorInfo(e.getMessage());
		}
    	return rm;
    }
}
