/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			客户资质信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Slm.CustomerInformation.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.CustomerInformation.mapper.SlmCustomerAptitudesMapper;
import market.Slm.CustomerInformation.service.SlmCustomerAptitudesService;
import market.Slm.CustomerInformation.vo.SlmCustomerAptitudes;

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="SlmCustomerAptitudesService")
public class SlmCustomerAptitudesServiceImpl extends BaseServiceImpl<SlmCustomerAptitudes,Object> implements SlmCustomerAptitudesService {
	/**
	 * 查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmCustomerAptitudes> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();

		PageModel<SlmCustomerAptitudes> pm = new PageModel<SlmCustomerAptitudes>(pageIndex,pageSize);
		
		try {
			SlmCustomerAptitudes sb=sum.getObject();
			String sWhere=" 1=1 ";
			if(sb!=null){
				if(!"".equals(sb.getAptitudes_name())&&sb.getAptitudes_name()!=null){
					sWhere+=" and (aptitudes_name like '%"+sb.getAptitudes_name()+"%') ";
					sb.setAptitudes_name(null);
				}
			}
			

			pm.setRows(this.getMapper(SlmCustomerAptitudesMapper.class).findWithCondition(sb, sWhere, " valid_flag desc,aptitudes_no desc ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerAptitudesMapper.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<SlmCustomerAptitudes> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerAptitudesMapper.class).findById(sum.getObject().getAptitudes_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 添加数据
	 * 
	 */
	public ResultModel doAddWeb(CommonPage<SlmCustomerAptitudes> sum) {
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerAptitudes bd=sum.getObject();
			
			SlmCustomerAptitudes obd = new SlmCustomerAptitudes();
			obd.setCustomer_no(bd.getCustomer_no());
			obd.setAptitudes_name(bd.getAptitudes_name());
			
			List<SlmCustomerAptitudes> list2 = this.getMapper(SlmCustomerAptitudesMapper.class).find(obd, 0, 0);
			if (list2 != null && list2.size() > 0) {
				rm.setState("500");
				rm.setMsgInfo("该公司已存在相同名称的证书");
			}else{
				String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
				String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				bd.setAptitudes_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
				bd.setCreate_man_no(userId);
				bd.setCreate_man_name(userName);
				bd.setValid_flag("1");
				bd.setCreate_time(new Date());
				this.getMapper(SlmCustomerAptitudesMapper.class).insert(bd);
				rm.setState("200");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 修改数据
	 * @param br
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<SlmCustomerAptitudes> sum) {
		ResultModel rm=new ResultModel();
		SlmCustomerAptitudes bd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			SlmCustomerAptitudes sca=this.getMapper(SlmCustomerAptitudesMapper.class).findById(bd.getAptitudes_no());
			if(sca==null){
				rm.setState("500");
				rm.setMsgInfo("当前数据不存在!");
				return rm;
			}
			if(!bd.getAptitudes_name().equals(sca.getAptitudes_name())){
				SlmCustomerAptitudes obd = new SlmCustomerAptitudes();
				obd.setCustomer_no(bd.getCustomer_no());
				obd.setAptitudes_name(bd.getAptitudes_name());
				List<SlmCustomerAptitudes> list2 = this.getMapper(SlmCustomerAptitudesMapper.class).find(obd, 0, 0);
				if (list2 != null && list2.size() > 0) {
					rm.setState("500");
					rm.setMsgInfo("该公司已存在相同名称的证书");
					return rm;
				}
			}
			bd.setUpdate_man_no(userId);
			bd.setUpdate_man_name(userName);
			bd.setUpdate_time(new Date());
			this.getMapper(SlmCustomerAptitudesMapper.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<SlmCustomerAptitudes> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerAptitudes> obd = sum.getObjectList();
		for(SlmCustomerAptitudes sbr:obd){
			this.getMapper(SlmCustomerAptitudesMapper.class).update(sbr);
		}
		rm.setState("200");
		return rm;
	}
	
}
