package net.lab1024.sa.admin.module.business.base.address.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.base.address.dao.AddressDao;
import net.lab1024.sa.admin.module.business.base.address.domain.entity.AddressEntity;
import net.lab1024.sa.admin.module.business.base.address.domain.form.AddressQueryForm;
import net.lab1024.sa.admin.module.business.base.address.domain.form.AddressSelect;
import net.lab1024.sa.admin.module.business.base.address.domain.vo.AddressExcelVO;
import net.lab1024.sa.admin.module.business.base.address.domain.vo.AddressVO;
import net.lab1024.sa.admin.module.business.base.address.manager.AddressManager;
import net.lab1024.sa.admin.module.business.base.item.domain.entity.ItemEntity;
import net.lab1024.sa.admin.module.business.base.item.domain.vo.ItemsExcelVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.module.support.dict.constant.DictConst;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AddressQueryService {

    @Resource
    private AddressDao addressDao;

    @Resource
    private AddressManager addressManager;

    /**
     * 分页查询
     *
     * @param queryForm 查询参数
     * @return PageResult<AddressVO>
     */
    public PageResult<AddressVO> queryPage(AddressQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<AddressVO> list = addressDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 地址下拉查询
     *
     * @param addressSelect 入参
     * @return List<AddressEntity>
     */
    public List<AddressEntity> queryAddress(AddressSelect addressSelect) {
        return addressDao.selectList(null);
    }

    /**
     * 根据地址id集合查询地址信息
     *
     * @param addressIdList 地址id集合
     * @return Map<Long, AreaEntity>
     */
    public Map<Long, AddressEntity> queryAddressList(List<Long> addressIdList) {
        if (CollectionUtils.isEmpty(addressIdList)) {
            return Collections.emptyMap();
        }
        addressIdList = addressIdList.stream().distinct().collect(Collectors.toList());
        Map<Long, AddressEntity> addressMap = Maps.newHashMap();
        for (Long addressId : addressIdList) {
            AddressEntity address = addressManager.queryAddress(addressId);
            if (address != null) {
                addressMap.put(addressId, address);
            }
        }
        return addressMap;
    }

    /**
     * 根据地址name集合查询地址信息
     *
     * @param addressNameList 地址name集合
     * @return Map<Long, AddressEntity>
     */
    public Map<String, AddressEntity> queryAddressByNameList(List<String> addressNameList) {
        if (CollectionUtils.isEmpty(addressNameList)) {
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<AddressEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AddressEntity::getName, addressNameList);

        List<AddressEntity> addressList = addressDao.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(addressList)) {
            return Collections.emptyMap();
        }
        return addressList.stream()
                .collect(Collectors.toMap(AddressEntity::getName, address -> address, (existing, replacement) -> existing));
    }

    /**
     * 根据地址名称查询地址信息
     *
     * @param addressName 地址名称
     * @return AddressEntity
     */
    public AddressEntity queryByAddressName(String addressName) {
        LambdaQueryWrapper<AddressEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressEntity::getName, addressName);
        return addressDao.selectOne(queryWrapper);
    }

    /**
     * 地址导出
     *
     * @return List<AddressExcelVO>
     */
    public List<AddressExcelVO> getAddressExcelVOList() {
        List<AddressEntity> addressList = addressDao.selectList(null);
        return addressList.stream()
                .map(address ->
                        AddressExcelVO.builder()
                                .name(address.getName())
                                .person(address.getPerson())
                                .telephone(address.getTelephone())
                                .address(address.getAddress())
                                .build()
                )
                .collect(Collectors.toList());
    }
}
