package com.fzm.mall.server.front.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.user.model.MailAddress;
import com.fzm.mall.server.front.user.mapper.MailAddressMapper;
import com.fzm.mall.server.front.user.model.vo.AddrVo;
import com.fzm.mall.server.front.user.service.IMailAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户收货地址表 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-30
 */
@Service
public class MailAddressServiceImpl extends ServiceImpl<MailAddressMapper, MailAddress> implements IMailAddressService {

    @Autowired
    private  MailAddressMapper mailAddressMapper;
    @Override
    public List<AddrVo> getListByUid(String uid) {

        return mailAddressMapper.getListByUid(uid);
    }

    @Override
    public void saveAndUpdate(MailAddress mailAddress) {
        if(mailAddress.getDefaultAddr() == 1){
            //把其他的改成非默认
            updateNotDefault(mailAddress.getUid());
        }
        saveOrUpdate(mailAddress);
    }

    @Override
    public void deleteByIdAndUid(Integer id, String uid) {
        mailAddressMapper.deleteByIdAndUid(id,uid);
    }

    private void updateNotDefault(String uid) {
        mailAddressMapper.updateNotDefault(uid);
    }
}
