UmsAddressServiceImpl.java 3.8 KB
Newer Older
H
hxrui 已提交
1 2
package com.youlai.mall.ums.service.impl;

3 4
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
5
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
H
hxrui 已提交
6
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7
import com.youlai.common.constant.GlobalConstants;
H
haoxr 已提交
8
import com.youlai.common.security.util.SecurityUtils;
9
import com.youlai.mall.ums.dto.MemberAddressDTO;
H
hxrui 已提交
10
import com.youlai.mall.ums.mapper.UmsAddressMapper;
11 12
import com.youlai.mall.ums.model.entity.UmsAddress;
import com.youlai.mall.ums.model.form.AddressForm;
H
hxrui 已提交
13 14
import com.youlai.mall.ums.service.IUmsAddressService;
import org.springframework.stereotype.Service;
15
import org.springframework.transaction.annotation.Transactional;
H
hxrui 已提交
16

17 18 19 20 21 22 23 24 25
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 会员地址业务实现类
 *
 * @author haoxr
26
 * @since 2022/2/12
27
 */
H
hxrui 已提交
28 29 30
@Service
public class UmsAddressServiceImpl extends ServiceImpl<UmsAddressMapper, UmsAddress> implements IUmsAddressService {

31
    /**
32
     * 新增地址
33
     *
34
     * @param addressForm
35 36 37
     * @return
     */
    @Override
38 39
    @Transactional
    public boolean addAddress(AddressForm addressForm) {
H
haoxr 已提交
40
        Long memberId = SecurityUtils.getMemberId();
41 42 43 44 45 46 47 48 49 50 51

        UmsAddress umsAddress = new UmsAddress();
        BeanUtil.copyProperties(addressForm, umsAddress);
        umsAddress.setMemberId(memberId);
        boolean result = this.save(umsAddress);
        if (result) {
            // 修改其他默认地址为非默认
            if (GlobalConstants.STATUS_YES.equals(addressForm.getDefaulted())) {
                this.update(new LambdaUpdateWrapper<UmsAddress>()
                        .eq(UmsAddress::getMemberId, memberId)
                        .eq(UmsAddress::getDefaulted, 1)
52
                        .ne(UmsAddress::getId,umsAddress.getId())
53 54 55
                        .set(UmsAddress::getDefaulted, 0)
                );
            }
56
        }
57
        return result;
58 59 60 61 62
    }

    /**
     * 修改地址
     *
63
     * @param addressForm
64 65 66
     * @return
     */
    @Override
67
    public boolean updateAddress(AddressForm addressForm) {
H
haoxr 已提交
68
        Long memberId = SecurityUtils.getMemberId();
69 70 71 72 73 74 75 76 77 78 79 80

        UmsAddress umsAddress = new UmsAddress();
        BeanUtil.copyProperties(addressForm, umsAddress);

        boolean result = this.updateById(umsAddress);

        if(result){
            // 修改其他默认地址为非默认
            if (GlobalConstants.STATUS_YES.equals(addressForm.getDefaulted())) {
                this.update(new LambdaUpdateWrapper<UmsAddress>()
                        .eq(UmsAddress::getMemberId, memberId)
                        .eq(UmsAddress::getDefaulted, 1)
81
                        .ne(UmsAddress::getId, umsAddress.getId())
82 83 84
                        .set(UmsAddress::getDefaulted, 0)
                );
            }
85
        }
86
        return result;
87 88
    }

89 90 91 92 93 94 95
    /**
     * 获取当前登录会员的地址列表
     *
     * @return
     */
    @Override
    public List<MemberAddressDTO> listCurrentMemberAddresses() {
H
haoxr 已提交
96
        Long memberId = SecurityUtils.getMemberId();
97 98 99 100 101 102 103 104 105 106 107 108
        List<UmsAddress> umsAddressList = this.list(new LambdaQueryWrapper<UmsAddress>()
                .eq(UmsAddress::getMemberId, memberId)
                .orderByDesc(UmsAddress::getDefaulted) // 默认地址排在首位
        );
        List<MemberAddressDTO> memberAddressList = Optional.ofNullable(umsAddressList).orElse(new ArrayList<>()).stream()
                .map(umsAddress -> {
                    MemberAddressDTO memberAddressDTO = new MemberAddressDTO();
                    BeanUtil.copyProperties(umsAddress, memberAddressDTO);
                    return memberAddressDTO;
                }).collect(Collectors.toList());
        return memberAddressList;
    }
H
hxrui 已提交
109
}