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

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

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

			pm.setRows(this.getMapper(SlmCustomerRecipientMapper.class).findWithCondition(sb, sWhere, "", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerRecipientMapper.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<SlmCustomerRecipient> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerRecipientMapper.class).findById(sum.getObject().getRecipient_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<SlmCustomerRecipient> sum) throws Exception {
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerRecipient bd=sum.getObject();
			
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setRecipient_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(SlmCustomerRecipientMapper.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<SlmCustomerRecipient> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerRecipient 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(SlmCustomerRecipientMapper.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<SlmCustomerRecipient> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerRecipient> obd = sum.getObjectList();
		for(SlmCustomerRecipient sbr:obd){
			this.getMapper(SlmCustomerRecipientMapper.class).update(sbr);
		}
		rm.setState("200");
		return rm;
	}
}
