package org.cs.mgr.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.cs.core.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import org.cs.util.Pager;
import org.cs.mgr.user.service.IMgmCustomerService;
import org.cs.mgr.user.dao.IMgmCustomerDao;
import org.cs.mgr.user.model.MgmCustomer;
import org.cs.mgr.user.model.MgmInviteRecord;

/**
 * 
 * @ClassName: IMgmCustomerService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:56
 */
@Service("mgmCustomerService")
public class MgmCustomerService extends BaseService<MgmCustomer> implements IMgmCustomerService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmCustomerDao mgmCustomerDao;
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmCustomers(Pager page,Map<String, String> map){
		return this.mgmCustomerDao.queryMgmCustomers(page,map);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmCustomer 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmCustomer(Integer id){
		MgmCustomer mgmCustomer = this.mgmCustomerDao.findById(id);
		if(mgmCustomer != null){
			mgmCustomer.setIsDelete(true);				
			this.mgmCustomerDao.update(mgmCustomer);
		}
		return true;
	}
	
	
	/**
	 * 自动生成
	 * @Title: addMgmCustomer 
	 * @Description: TODO
	 * @param mgmCustomer						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmCustomer(MgmCustomer mgmCustomer){
	
		if(mgmCustomer != null){
			this.mgmCustomerDao.insert(mgmCustomer);
		}
		return true;
	}
	
	
	/**
	 *  自动生成
	 * @Title: updateMgmCustomer 
	 * @Description: TODO
	 * @param mgmCustomer对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmCustomer(MgmCustomer mgmCustomer){
	
		MgmCustomer mgmCustomer2 = this.mgmCustomerDao.findById(mgmCustomer.getId());
		if(mgmCustomer2 != null){
			mgmCustomer.mergeTo(mgmCustomer2);
			this.mgmCustomerDao.update(mgmCustomer2);
		}
		
		return true;
	}

	@Override
	@Transactional(readOnly = false)
	public JSONObject addMgmCustomer(Map<String, String> map) {
		try {
			//TODO 手机号判断客户是否重复
			MgmCustomer mgmCustomer = (MgmCustomer) MgmCustomer.mapToObject(map, MgmCustomer.class);
			mgmCustomer.setCreateDate(new Date());
			 
			MgmCustomer ismgmCustomer = mgmCustomerDao.isPhone(mgmCustomer.getPhone(),mgmCustomer.getRecommendId());
			if(ismgmCustomer==null){
				this.mgmCustomerDao.insert(mgmCustomer);
			    return getJson(1,mgmCustomer.getId(), "添加成功");
			}
			return getJson(0,"", "添加失败，该客户手机号已存在！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return   getJson(0,"", "添加失败");
		}
	}

	@Override
	@Transactional(readOnly = false)
	public JSONObject updateMgmCustomer(Map<String, String> map) {
		Integer id= Integer.valueOf(map.get("id"));
		
		try { 
			MgmCustomer mgmCustomer = this.findById(id);
			//对比数据
			MgmCustomer mgmCustomer2 = (MgmCustomer) MgmCustomer.mapToObject(map, MgmCustomer.class);
			//判断手机号是否修改
			if(mgmCustomer.getPhone().equals(mgmCustomer2.getPhone())){
				mgmCustomer2.mergeTo(mgmCustomer);
				mgmCustomer.setModifyDate(new Date(0));
				this.update(mgmCustomer);
				return   getJson(1,mgmCustomer.getId(), "修改成功");
			}
			
			//判断手机是否重复
			MgmCustomer ismgmCustomer = mgmCustomerDao.isPhone(mgmCustomer2.getPhone(),mgmCustomer.getRecommendId());
			
			if(ismgmCustomer==null){
				mgmCustomer2.mergeTo(mgmCustomer);
				mgmCustomer.setModifyDate(new Date(0));
				this.update(mgmCustomer);
				return   getJson(1,mgmCustomer.getId(), "修改成功");
			} 
			return getJson(0,"", "添加失败，该客户手机号已存在！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return   getJson(0,"", "修改失败");
		}
	}
	
	@Override
	@Transactional(readOnly = false)
	public JSONObject deleteMgmCustomer(Map<String, String> map) {
		Integer id= Integer.valueOf(map.get("id"));
		try {
				MgmCustomer mgmCustomer = this.findById(id);
				mgmCustomer.setIsDelete(true);
				this.update(mgmCustomer);
				return   getJson(1,"", "删除成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return   getJson(0,"", "删除失败");
		}
	}
	/**
	 * 
	 * @Title: getMgmMessageList  
	 * @Description:客户列表
	 * @param: @param map
	 * @param: @return         
	 * @see  
	 */
	@Override
	public Pager getMgmCustomerList(Map<String,String> map){
		String userId=map.get("userId");
		Integer page = Integer.valueOf(map.get("page"));
		return this.mgmCustomerDao.queryMgmCustomerByUserId(new Pager(page),userId);
	} 
	/**
	 * 
	 * @Title: getMgmInviteRecordsList   
	 * @Description: 客户列表  
	 * @param: @param code
	 * @param: @return      
	 * @return: List<MgmInviteRecord>      
	 * @throws
	 */
	@Override
	public List<MgmCustomer> getMgmCustomerList(String userId){
		return this.mgmCustomerDao.queryMgmCustomerByUserId(userId);
	}
	/**
	 * 
	 * @Title: getMgmCustomerById  
	 * @Description:客户详情
	 * @param: @param map
	 * @param: @return         
	 * @see  
	 */
	@Override
	public MgmCustomer getMgmCustomerById(Map<String,String> map){
		String userId=map.get("userId");
		return this.mgmCustomerDao.findById(Integer.parseInt(userId));
	} 

	@Override
	public JSONObject queryMyMgmCustomer(Pager page, Integer userId) {
		JSONObject json = new JSONObject();		
		Pager pager = this.mgmCustomerDao.queryMyMgmCustomer(page, userId);
		json.put("results", pager.getResults());
		json.put("total", pager.getTotal());
		json.put("page", pager.getPage());
		json.put("start", pager.getStart());
		json.put("pageSize", pager.getPageSize());
		json.put("end", pager.getEnd());
		json.put("pageTotal", pager.getPageTotal());
		return getJson(1,json, "获取客户列表成功");
	}
}
