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

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="SlmCustomerAccountService")
public class SlmCustomerAccountServiceImpl extends BaseServiceImpl<SlmCustomerAccount,Object> implements SlmCustomerAccountService {
	/**
	 * 查询数据
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmCustomerAccount> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmCustomerAccount> pm = new PageModel<SlmCustomerAccount>(pageIndex,pageSize);
			SlmCustomerAccount sb=sum.getObject()==null?(new SlmCustomerAccount()):sum.getObject();
			
			pm.setRows(this.getMapper(SlmCustomerAccountMapper.class).find(sb, (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerAccountMapper.class).count(sb));
	
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 查看详情
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmCustomerAccount> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerAccountMapper.class).findById(sum.getObject().getAccount()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	

	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<SlmCustomerAccount> sum) {
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerAccount bd=sum.getObject();
			
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setAccount(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(SlmCustomerAccountMapper.class).insert(bd);
			rm.setState("200");
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改数据
	 * 
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<SlmCustomerAccount> sum) {
		ResultModel rm=new ResultModel();
		SlmCustomerAccount 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());
			this.getMapper(SlmCustomerAccountMapper.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<SlmCustomerAccount> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerAccount> obd = sum.getObjectList();
		SlmCustomerAccount sca=new SlmCustomerAccount();
		sca.setCustomer_no(obd.get(0).getCustomer_no());
//		//先将数据全部设置为无效
//		this.getMapper(SlmCustomerAccountMapper.class).doUpdateFlag(sca);
		
		for(SlmCustomerAccount sci :obd){
			this.getMapper(SlmCustomerAccountMapper.class).update(sci);
		}
		rm.setState("200");
		return rm;
	}

}
