rk
昨天 3aef471b170a703b501ddb4d9d2a12791d07ff28
server/services/src/main/java/com/doumee/service/business/impl/AddrServiceImpl.java
@@ -14,6 +14,7 @@
import com.doumee.dao.business.AddrMapper;
import com.doumee.dao.business.model.Addr;
import com.doumee.service.business.AddrService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -168,4 +169,119 @@
        QueryWrapper<Addr> wrapper = new QueryWrapper<>(addr);
        return addrMapper.selectCount(wrapper);
    }
    @Override
    public List<Addr> findListWithArea(Integer memberId) {
        MPJLambdaWrapper<Addr> wrapper = new MPJLambdaWrapper<Addr>()
                .selectAll(Addr.class)
                .select("a3.name", Addr::getDistrictName)
                .select("a3.code", Addr::getDistrictCode)
                .select("a2.id", Addr::getCityId)
                .select("a2.name", Addr::getCityName)
                .select("a2.code", Addr::getCityCode)
                .select("a1.id", Addr::getProvinceId)
                .select("a1.name", Addr::getProvinceName)
                .select("a1.code", Addr::getProvinceCode)
                .leftJoin("areas a3 on a3.id = t.AREA_ID")
                .leftJoin("areas a2 on a2.id = a3.PARENT_ID")
                .leftJoin("areas a1 on a1.id = a2.PARENT_ID")
                .eq(Addr::getDeleted, Constants.ZERO)
                .eq(Addr::getMemberId, memberId)
                .orderByDesc(Addr::getIsDefault)
                .orderByDesc(Addr::getCreateTime);
        return addrMapper.selectJoinList(Addr.class, wrapper);
    }
    @Override
    public Addr findByIdWithArea(Integer id) {
        MPJLambdaWrapper<Addr> wrapper = new MPJLambdaWrapper<Addr>()
                .selectAll(Addr.class)
                .select("a3.name", Addr::getDistrictName)
                .select("a3.code", Addr::getDistrictCode)
                .select("a2.id", Addr::getCityId)
                .select("a2.name", Addr::getCityName)
                .select("a2.code", Addr::getCityCode)
                .select("a1.id", Addr::getProvinceId)
                .select("a1.name", Addr::getProvinceName)
                .select("a1.code", Addr::getProvinceCode)
                .leftJoin("areas a3 on a3.id = t.AREA_ID")
                .leftJoin("areas a2 on a2.id = a3.PARENT_ID")
                .leftJoin("areas a1 on a1.id = a2.PARENT_ID")
                .eq(Addr::getId, id);
        Addr addr = addrMapper.selectJoinOne(Addr.class, wrapper);
        if (Objects.isNull(addr)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        return addr;
    }
    @Override
    @Transactional(rollbackFor = {Exception.class, BusinessException.class})
    public Integer createByMember(Addr addr, Integer memberId) {
        validateAddr(addr);
        // 设为默认时,先清除该用户原有的默认地址
        if (Constants.equalsInteger(addr.getIsDefault(), Constants.ONE)) {
            clearDefault(memberId);
        }
        addr.setMemberId(memberId);
        addr.setDeleted(Constants.ZERO);
        addr.setCreateTime(new Date());
        addr.setCreateUser(memberId);
        addr.setUpdateTime(new Date());
        addr.setUpdateUser(memberId);
        addrMapper.insert(addr);
        return addr.getId();
    }
    @Override
    @Transactional(rollbackFor = {Exception.class, BusinessException.class})
    public void updateByMember(Addr addr, Integer memberId) {
        if (Objects.isNull(addr) || Objects.isNull(addr.getId())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        validateAddr(addr);
        // 设为默认时,先清除该用户原有的默认地址
        if (Constants.equalsInteger(addr.getIsDefault(), Constants.ONE)) {
            clearDefault(memberId);
        }
        addr.setMemberId(memberId);
        addr.setUpdateTime(new Date());
        addr.setUpdateUser(memberId);
        addrMapper.updateById(addr);
    }
    private void validateAddr(Addr addr) {
        if (Objects.isNull(addr.getAreaId())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "区域不能为空");
        }
        if (StringUtils.isBlank(addr.getAddr())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "详细地址不能为空");
        }
        if (Objects.isNull(addr.getIsDefault())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "是否默认不能为空");
        }
        if (StringUtils.isBlank(addr.getName())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "姓名不能为空");
        }
        if (StringUtils.isBlank(addr.getPhone())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "电话不能为空");
        }
        if (Objects.isNull(addr.getLongitude())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "经度不能为空");
        }
        if (Objects.isNull(addr.getLatitude())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "纬度不能为空");
        }
    }
    /**
     * 清除该用户所有地址的默认标记
     */
    private void clearDefault(Integer memberId) {
        addrMapper.update(new UpdateWrapper<Addr>().lambda()
                .set(Addr::getIsDefault, Constants.ZERO)
                .eq(Addr::getMemberId, memberId)
                .eq(Addr::getDeleted, Constants.ZERO)
                .eq(Addr::getIsDefault, Constants.ONE));
    }
}