/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   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.SlmCustomerPersonInfoMapper;
import market.Slm.CustomerInformation.service.SlmCustomerPersonInfoService;
import market.Slm.CustomerInformation.vo.SlmCustomerPersonInfo;

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.basic.util.DataEncoder;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="SlmCustomerPersonInfoService")
public class SlmCustomerPersonInfoServiceImpl extends BaseServiceImpl<SlmCustomerPersonInfo,Object> implements SlmCustomerPersonInfoService {
	/**
	 * 查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmCustomerPersonInfo> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();

		PageModel<SlmCustomerPersonInfo> pm = new PageModel<SlmCustomerPersonInfo>(pageIndex,pageSize);

		try {
			SlmCustomerPersonInfo sb=sum.getObject();
			
			String sWhere=" 1=1 ";
			if(sb!=null){
				if(!"".equals(sb.getPerson_name())&&sb.getPerson_name()!=null){
					sWhere+=" and (person_name like '%"+sb.getPerson_name()+"%') ";
					sb.setPerson_name(null);
				}
			}
			
			pm.setRows(this.getMapper(SlmCustomerPersonInfoMapper.class).findWithCondition(sb, sWhere, " person_name ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerPersonInfoMapper.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<SlmCustomerPersonInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerPersonInfoMapper.class).findById(sum.getObject().getPerson_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 添加数据
	 * @throws Exception 
	 * 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmCustomerPersonInfo> sum) throws Exception {
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerPersonInfo bd=sum.getObject();

			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setPerson_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());
			//======eason add 2019-07-09===============================
			if(bd.getPassword()!=null&&!"".equals(bd.getPassword().trim())){
				bd.setPassword(DataEncoder.shaEncode(bd.getPassword().trim()));
			}
			if(bd.getUser_id()!=null&&!"".equals(bd.getUser_id().trim())){
				SlmCustomerPersonInfo sp=new SlmCustomerPersonInfo();
				sp.setUser_id(bd.getUser_id());
				int icount = this.getMapper(SlmCustomerPersonInfoMapper.class).count(sp);
				if(icount>0){
					rm.setState("500");
					rm.setMsgInfo("该用户登录账户已存在");
					return rm;
				}
			}
			//========================================================
			this.getMapper(SlmCustomerPersonInfoMapper.class).insert(bd);
			rm.setState("200");

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 修改数据
	 * @param br
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdateWeb(CommonPage<SlmCustomerPersonInfo> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerPersonInfo 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());
			//======eason add 2019-07-09===============================
			if(bd.getPassword()!=null&&!"".equals(bd.getPassword().trim())){
				bd.setPassword(DataEncoder.shaEncode(bd.getPassword().trim()));
			}
			if(bd.getUser_id()!=null&&!"".equals(bd.getUser_id().trim())){
				SlmCustomerPersonInfo sp=new SlmCustomerPersonInfo();
				sp.setUser_id(bd.getUser_id());
				int icount = this.getMapper(SlmCustomerPersonInfoMapper.class).countWithCondition(sp, " person_no!='"+bd.getPerson_no().trim()+"'");
				if(icount>0){
					rm.setState("500");
					rm.setMsgInfo("该用户登录账户已存在");
					return rm;
				}
			}
			//========================================================
			this.getMapper(SlmCustomerPersonInfoMapper.class).update(bd);

			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;			
	}

	/**
	 * 启用、停用数据
	 * @param bd
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateFlagWeb(CommonPage<SlmCustomerPersonInfo> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerPersonInfo> obd = sum.getObjectList();
		for(SlmCustomerPersonInfo sbr:obd){
			this.getMapper(SlmCustomerPersonInfoMapper.class).update(sbr);
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 客户登录模块
	 * eason add 2019-07-09
	 * @param personInfo
	 * @return
	 */
	public ResultModel doLogin(CommonPage<SlmCustomerPersonInfo> oci){
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerPersonInfo personInfo=oci==null?new SlmCustomerPersonInfo():oci.getObject();
			if(oci==null || personInfo.getUser_id()==null||"".equals(personInfo.getUser_id().trim())
					||personInfo.getPassword()==null||"".equals(personInfo.getPassword().trim())){
				rm.setState("500");
				rm.setMsgInfo("请输入账户及密码!");
				return rm;
			}
			String secret=DataEncoder.shaEncode(personInfo.getPassword());
//			String secret=personInfo.getPassword();
			SlmCustomerPersonInfo scp=new SlmCustomerPersonInfo();
			scp.setUser_id(personInfo.getUser_id());
			scp.setPassword(secret);
			List<SlmCustomerPersonInfo> lsc=this.getMapper(SlmCustomerPersonInfoMapper.class).find(scp, 0, 1);
			if(lsc!=null&&lsc.size()>0){
				this.getSession().setAttribute(HomeService.SESSION_USER_ID, personInfo.getUser_id());
				this.getSession().setAttribute(HomeService.SESSION_USER_NAME, personInfo.getPerson_name());
				this.getSession().setAttribute(HomeService.SESSION_DEPT_ID, lsc.get(0).getCustomer_no());
				this.getSession().setAttribute(HomeService.SESSION_DEPT_NAME, lsc.get(0).getCustomer_name());
				rm.setState("200");
				SlmCustomerPersonInfo sc=new SlmCustomerPersonInfo();
				sc.setCustomer_no(lsc.get(0).getCustomer_no());
				sc.setCustomer_name(lsc.get(0).getCustomer_name());
				sc.setPerson_no(lsc.get(0).getPerson_no());
				sc.setPerson_name(lsc.get(0).getPerson_name());
				sc.setPhone(lsc.get(0).getPhone());
				sc.setEmail(lsc.get(0).getEmail());
				rm.setData(sc);
			}else{
				rm.setState("500");
				rm.setMsgInfo("账户或密码错误!");
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("登录失败："+e.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * app端客户修改密码
	 * eason add 2019-07-09
	 * @param personInfo
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdatePassword(CommonPage<SlmCustomerPersonInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerPersonInfo personInfo = sum.getObject();
			if(personInfo==null
					||sum.getMemo1()==null||"".equals(sum.getMemo1().trim())
					||personInfo.getPassword()==null||"".equals(personInfo.getPassword().trim())){
				rm.setState("500");
				rm.setMsgInfo("请输入原密码及新密码!");
				return rm;
			}
			String secret=DataEncoder.shaEncode(personInfo.getPassword());
			SlmCustomerPersonInfo scp=new SlmCustomerPersonInfo();
			String user_id = sum.getMemo2();
			if (user_id==null) {
				user_id = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			}
			scp.setUser_id(user_id);
			scp.setPassword(secret);
			List<SlmCustomerPersonInfo> slp=this.getMapper(SlmCustomerPersonInfoMapper.class).find(scp, 0, 0);
			if(slp!=null&&slp.size()>0){
				scp.setPassword(DataEncoder.shaEncode(sum.getMemo1().trim()));
				scp.setPerson_no(slp.get(0).getPerson_no());
				this.getMapper(SlmCustomerPersonInfoMapper.class).update(scp);
				rm.setState("200");
				rm.setMsgInfo("修改密码成功");
			}else{
				rm.setState("500");
				rm.setMsgInfo("原密码错误!");
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("密码修改错误："+e.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		
		return rm;
	}
	
	//=====================APP端 eason add 2017-07-15===================
	/**
	 * 查询
	 * @return
	 */
	public ResultModel doQueryApp(CommonPage<SlmCustomerPersonInfo> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();

		PageModel<SlmCustomerPersonInfo> pm = new PageModel<SlmCustomerPersonInfo>(pageIndex,pageSize);

		try {
			SlmCustomerPersonInfo sb=sum.getObject();
			
			String sWhere=" customer_no='"+this.getSession().getAttribute(HomeService.SESSION_DEPT_ID)+"' ";
			
			if(sb!=null){
				if(!"".equals(sb.getPerson_name())&&sb.getPerson_name()!=null){
					sWhere+=" and (person_name like '%"+sb.getPerson_name()+"%') ";
					sb.setPerson_name(null);
				}
			}
			
			pm.setRows(this.getMapper(SlmCustomerPersonInfoMapper.class).findWithCondition(sb, sWhere, " person_name ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerPersonInfoMapper.class).countWithCondition(sb, sWhere));
			rm.setState("200");
			rm.setData(pm);

		} catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
}
