package com.leromro.core.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leromro.common.core.domain.AjaxResult;
import com.leromro.common.core.domain.R;
import com.leromro.common.utils.SecurityUtils;
import com.leromro.core.mapper.AddressMapper;
import com.leromro.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.leromro.core.mapper.AddressMapper;
import com.leromro.core.domain.Address;
import com.leromro.core.service.IAddressService;

/**
 * 受服务地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-18
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService
{
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询受服务地址
     * 
     * @param addressId 受服务地址主键
     * @return 受服务地址
     */
    @Override
    public Address selectAddressByAddressId(Long addressId)
    {
        return addressMapper.selectAddressByAddressId(addressId);
    }

    /**
     * 查询受服务地址列表
     * 
     * @param address 受服务地址
     * @return 受服务地址
     */
    @Override
    public List<Address> selectAddressList(Address address)
    {
        return addressMapper.selectAddressList(address);
    }

    /**
     * 新增受服务地址
     * 
     * @param address 受服务地址
     * @return 结果
     */
    @Override
    public Boolean insertAddress(Address address)
    {
        address.setUserId(SecurityUtils.getUserId());
        return this.save(address);
    }

    /**
     * 修改受服务地址
     * 
     * @param address 受服务地址
     * @return 结果
     */
    @Override
    public Boolean updateAddress(Address address)
    {
        return this.updateById(address);
    }

    /**
     * 批量删除受服务地址
     * 
     * @param addressIds 需要删除的受服务地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressByAddressIds(Long[] addressIds)
    {
        return addressMapper.deleteAddressByAddressIds(addressIds);
    }

    /**
     * 删除受服务地址信息
     * 
     * @param addressId 受服务地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressByAddressId(Long addressId)
    {
        return addressMapper.deleteAddressByAddressId(addressId);
    }

    /**
     * @param address
     * @return
     */
    @Override
    public AjaxResult setDefaultAddress(Address address) {
        //设置所有的 默认收货地址为0
        Address oldAddress = new Address();
        oldAddress.setIsDefault(0);
        oldAddress.setUserId(SecurityUtils.getUserId());
        addressMapper.updateDefaultAddress(address);
        //设置当前收货地址为1
        address.setIsDefault(1);
        addressMapper.updateAddress(address);
        return AjaxResult.success();
    }

    /**
     * @return
     */
    @Override
    public R<Address> selectDefalutAddress() {
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<Address> defalutAddressQueryWrapper = new QueryWrapper<Address>().eq("user_id", userId).eq("is_default", 1);
        Address address = addressMapper.selectOne(defalutAddressQueryWrapper);
        return R.ok(address);
    }

    /**
     * @param orderId
     * @return
     */
    @Override
    public Address selectAddressInfoBySeconderId(String orderId) {
        return addressMapper.selectAddressBySeconderId(orderId);
    }
}
