/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.wechat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wechat.dao.MdeAddressDao;
import com.wechat.model.dbEntity.MdeAddress;
import com.wechat.model.dto.AddressesDto;


/**
 * 配送地址处理类
 * @author jadyn.wu
 * @date: 2019-09-02 10:38
 */
@Service
@Transactional
public class MdeAddressService {
	
	@Autowired
	MdeAddressDao mdeAddressDao;
	
	
	/**
	 * 获取配送地址列表
	 * 
	 * @return
	 */
	public List<AddressesDto> getAddressesByUser(String userId) {
		List<AddressesDto> list = mdeAddressDao.selectByUser(userId);
		return list;
	}
	
	/**
	 *  保存地址信息
	 *  	并返回自增主key
	 * 
	 * @return
	 */
	public int insert(MdeAddress entity) {
		return mdeAddressDao.insert(entity);
	}

	
	/**
	 *  修改\保存地址信息
	 *  	并返回自增主key
	 * 
	 * @return
	 */
	public int updAddress(MdeAddress entity) {
		// 获取配送地址id
		Integer addId = entity.getAddId();
		int count = 0;
		
		if("1".equals(entity.getDefaultFlg()))
		{
			//修改默认地址时，先重置原默认地址
			mdeAddressDao.undefault(entity.getUserId());
		}
		
		if(addId == null || addId == 0)
		{
			// 保存
			count = mdeAddressDao.insert(entity);
		} else {
			// 更新
			count = mdeAddressDao.update(entity);
		}
		return count;
	}
	
	/**
	 *  删除地址信息
	 * 
	 * @return
	 */
	public int delete(Integer addId,String userId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("addId", addId);
		map.put("userId", userId);
		return mdeAddressDao.delete(map);
	}

	
}