package com.ruoyi.logistics.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.logistics.mapper.BizAddressBookMapper;
import com.ruoyi.logistics.domain.BizAddressBook;
import com.ruoyi.logistics.service.IBizAddressBookService;

/**
 * 地址簿管理Service业务层处理
 * 
 * @author RuiJing
 * @date 2026-01-29
 */
@Service
public class BizAddressBookServiceImpl implements IBizAddressBookService 
{
    @Autowired
    private BizAddressBookMapper bizAddressBookMapper;

    /**
     * 查询地址簿管理
     * 
     * @param addressId 地址簿管理主键
     * @return 地址簿管理
     */
    @Override
    public BizAddressBook selectBizAddressBookByAddressId(Long addressId)
    {
        return bizAddressBookMapper.selectBizAddressBookByAddressId(addressId);
    }

    /**
     * 查询地址簿管理列表
     * 
     * @param bizAddressBook 地址簿管理
     * @return 地址簿管理
     */
    @Override
    public List<BizAddressBook> selectBizAddressBookList(BizAddressBook bizAddressBook)
    {
        bizAddressBook.setUserId(SecurityUtils.getLoginUser().getUserid());
        return bizAddressBookMapper.selectBizAddressBookList(bizAddressBook);
    }

    /**
     * 新增地址簿管理
     * 
     * @param bizAddressBook 地址簿管理
     * @return 结果
     */
    @Override
    public int insertBizAddressBook(BizAddressBook bizAddressBook)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        bizAddressBook.setUserId(loginUser.getUserid());
        bizAddressBook.setDeptId(loginUser.getSysUser().getDeptId());
        bizAddressBook.setCreateTime(DateUtils.getNowDate());
        bizAddressBook.setCreateBy(loginUser.getUsername());

        if ("1".equals(bizAddressBook.getDefaultFlag())) {
            bizAddressBookMapper.updateBizAddressBookDefault(loginUser.getUserid());
        }

        return bizAddressBookMapper.insertBizAddressBook(bizAddressBook);
    }

    /**
     * 修改地址簿管理
     * 
     * @param bizAddressBook 地址簿管理
     * @return 结果
     */
    @Override
    public int updateBizAddressBook(BizAddressBook bizAddressBook)
    {
        bizAddressBook.setCreateTime(DateUtils.getNowDate());
        bizAddressBook.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        bizAddressBook.setUpdateTime(DateUtils.getNowDate());
        return bizAddressBookMapper.updateBizAddressBook(bizAddressBook);
    }

    /**
     * 批量删除地址簿管理
     * 
     * @param addressIds 需要删除的地址簿管理主键
     * @return 结果
     */
    @Override
    public int deleteBizAddressBookByAddressIds(Long[] addressIds)
    {
        return bizAddressBookMapper.deleteBizAddressBookByAddressIds(addressIds);
    }

    /**
     * 删除地址簿管理信息
     * 
     * @param addressId 地址簿管理主键
     * @return 结果
     */
    @Override
    public int deleteBizAddressBookByAddressId(Long addressId)
    {
        return bizAddressBookMapper.deleteBizAddressBookByAddressId(addressId);
    }

    @Override
    public BizAddressBook selectDefaultAddressBook() {

        BizAddressBook query = new BizAddressBook();
        query.setUserId(SecurityUtils.getLoginUser().getUserid());
        query.setDefaultFlag("1");
        BizAddressBook bizAddressBook = bizAddressBookMapper.selectDefaultAddressBook(query);
        return bizAddressBook;
    }
}
