/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			基础数据信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/

package market.Com.impl;

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

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.pb.PbMethod;
import market.Com.service.ComBaseInfoService;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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="ComBaseInfoService")
public class ComBaseInfoServiceImpl extends BaseServiceImpl<ComBaseInfo,Object> implements ComBaseInfoService {
	/**
	 * 查询数据
	 * memo1有值时不查父当前节点
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<ComBaseInfo> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<ComBaseInfo> pm = new PageModel<ComBaseInfo>(pageIndex,pageSize);
			ComBaseInfo sb=sum.getObject()==null?(new ComBaseInfo()):sum.getObject();
			String sWhere=" 1=1 ";//sort_code='1001' ";
			
			if(sb!=null){
				if(!"".equals(sb.getSort_code())&&sb.getSort_code()!=null){
					
					if(sum.getMemo1()!=null&&!"".equals(sum.getMemo1())){//memo1有值时不查父当前节点
						sWhere+=" and sort_code='"+sb.getSort_code()+"' ";;
						sb.setSort_code(null);
					}else{
						sWhere+=" and (base_code='"+sb.getSort_code()+"' or sort_code='"+sb.getSort_code()+"') ";
						sb.setSort_code(null);
					}
					
				}
				if(!"".equals(sb.getBase_name())&&sb.getBase_name()!=null){
					sWhere+=" and base_name LIKE '%"+sb.getBase_name()+"%' ";
					sb.setBase_name(null);
				}
			}
	
			pm.setRows(this.getMapper(ComBaseInfoMapper.class).findWithCondition(sb, sWhere, " to_number(base_code) ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseInfoMapper.class).countWithCondition(sb,sWhere));
	
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 查看详情
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<ComBaseInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(ComBaseInfoMapper.class).findById(sum.getObject().getBase_code()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<ComBaseInfo> sum) {
		ResultModel rm=new ResultModel();
		try {
			ComBaseInfo bd=sum.getObject();
			ComBaseInfo cbi=new ComBaseInfo();
//			boolean flag=false;
			int layer=1;		//层级 
			//验证是否允许新增子类
			if(bd.getSort_code()!=null&&!"".equals(bd.getSort_code())){
				cbi.setBase_code(bd.getSort_code());
				cbi.setFlag("0");//0为不允许
				int icount=this.getMapper(ComBaseInfoMapper.class).count(cbi);
				if(icount>0){
					rm.setState("500");
					rm.setMsgInfo("当前数据不允许新增子类信息！");
					return rm;
				}
				cbi.setBase_code(null);
				cbi.setFlag(null);
				if(bd.getSort_code().length()>1)
					layer=2;
			}else{
				layer=0;
//				flag=true;
			}
			
			cbi.setSort_code(bd.getSort_code());
			
			String maxCode=this.getMapper(ComBaseInfoMapper.class).doMaxCode(cbi);
			int count=0;
			if(layer==0){
				count=Integer.parseInt(maxCode)+1;
				bd.setBase_code(String.valueOf(count));
			}else if(layer==1){
				if(maxCode.length()>1){
					count=Integer.parseInt(maxCode.substring(maxCode.length()-3,maxCode.length()))+1;
				}else{
					count=Integer.parseInt(maxCode)+1;
				}
				bd.setBase_code(bd.getSort_code()+(String.format("%03d", count)));
			}else{
				if(maxCode.length()>1){
					count=Integer.parseInt(maxCode.substring(maxCode.length()-2,maxCode.length()))+1;
				}else{
					count=Integer.parseInt(maxCode)+1;
				}
				bd.setBase_code(bd.getSort_code()+(String.format("%02d", count)));
			}
			
			
//			if(flag){
//				count=Integer.parseInt(maxCode)+1;
//				bd.setBase_code(String.format("%04d", count));
//			}else{
//				if(maxCode.length()>1){
//					count=Integer.parseInt(maxCode.substring(maxCode.length()-2,maxCode.length()))+1;
//				}else{
//					count=Integer.parseInt(maxCode)+1;
//				}
//				bd.setBase_code(bd.getSort_code()+(String.format("%02d", count)));
//			}
			
			
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setValidflag("1");
			bd.setCreate_time(new Date());
			bd.setMnemonic_code(PbMethod.ChineseSpell(bd.getBase_name()));
			
			this.getMapper(ComBaseInfoMapper.class).insert(bd);
			rm.setState("200");
			rm.setMsgInfo(bd.getBase_code());
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<ComBaseInfo> sum) {
		ResultModel rm=new ResultModel();
		ComBaseInfo bd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			bd.setUpdate_man_no(userId);
			bd.setUpdate_man_name(userName);
			bd.setUpdate_time(new Date());
			bd.setMnemonic_code(PbMethod.ChineseSpell(bd.getBase_name()));
			this.getMapper(ComBaseInfoMapper.class).update(bd);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 启用、停用数据
	 * @param bd
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateFlagWeb(CommonPage<ComBaseInfo> sum) {
		ResultModel rm=new ResultModel();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<ComBaseInfo> obd = sum.getObjectList();
		for(ComBaseInfo cbi:obd){
			if("0".equals(cbi.getValidflag())){
				cbi.setDelete_man_no(userId);
				cbi.setDelete_man_name(userName);
				cbi.setDelete_time(new Date());
			}
			this.getMapper(ComBaseInfoMapper.class).doUpdateFlag(cbi);
		}
		rm.setState("200");
		return rm;
	}
	
	public ResultModel findComBaseInfoBySortCode(CommonPage<ComBaseInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			String sortcode = sum.getMemo1();
			if (sortcode!=null && !"".equals(sortcode)) {
				rm.setData(this.getMapper(ComBaseInfoMapper.class).findComBaseInfoBySortCode(sortcode));
				rm.setState("200");
			}
		}catch (Exception ex){ 
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		} 
		return rm; 
	}
	
	/***
	 * 查询单个记录并返回
	 */
	@Override
	public List<ComBaseInfo> doQueryCbi(String code){
		List<ComBaseInfo> Cbi=new ArrayList<ComBaseInfo>();
		try {
			Cbi=this.getMapper(ComBaseInfoMapper.class).findComBaseInfocode(code);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Cbi;
	}
	
	/**
	 * 通过父类编码，一次性查询出所有的基础信息
	 * @param objectList里面必传sort_code
	 * @return
	 */
	public ResultModel doQueryBySorts(CommonPage<ComBaseInfo> cp){
		ResultModel rm = new ResultModel();		
		try {
			List<ComBaseInfo> cbiList = cp.getObjectList();
			if (cbiList != null && cbiList.size() > 0) {
				List<String> sortcodeList = new ArrayList<String>();
				for (ComBaseInfo cbi : cbiList) {
					sortcodeList.add(cbi.getSort_code());
				}
				List<ComBaseInfo> lists = this.getMapper(ComBaseInfoMapper.class).doQueryBySorts(sortcodeList);
				rm.setData(lists);
				rm.setState("200");
				rm.setMsgInfo("查询成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rm;
	}

	
}
