package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.UserAddressMapper;
import com.gxwebsoft.shop.service.UserAddressService;
import com.gxwebsoft.shop.entity.UserAddress;
import com.gxwebsoft.shop.param.UserAddressParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收货地址Service实现
 *
 * @author 科技小王子
 * @since 2024-05-26 10:58:37
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Override
    public PageResult<UserAddress> pageRel(UserAddressParam param) {
        PageParam<UserAddress, UserAddressParam> page = new PageParam<>(param);
        page.setDefaultOrder("is_default desc, id desc");
        List<UserAddress> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<UserAddress> listRel(UserAddressParam param) {
        List<UserAddress> list = baseMapper.selectListRel(param);
      // 排序
        PageParam<UserAddress, UserAddressParam> page = new PageParam<>();
        page.setDefaultOrder("is_default desc, id desc");
        return page.sortRecords(list);
    }

    @Override
    public UserAddress getByIdRel(Integer id) {
        UserAddressParam param = new UserAddressParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public void clearDefault(Integer userId) {
        update(
                new LambdaUpdateWrapper<UserAddress>()
                        .eq(UserAddress::getUserId, userId)
                        .eq(UserAddress::getIsDefault, 1)
                        .set(UserAddress::getIsDefault, 0)
        );
    }
}
