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

import cn.idev.excel.FastExcel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.entity.AddressEntity;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.form.AddressAddForm;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.form.AddressImportForm;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.form.AddressUpdateForm;
import net.lab1024.sa.admin.module.business.wms.base.address.manager.AddressManager;
import net.lab1024.sa.admin.module.business.wms.base.address.service.AddressQueryService;
import net.lab1024.sa.admin.module.business.wms.base.address.service.AddressService;
import net.lab1024.sa.admin.util.JoinerResult;
import net.lab1024.sa.admin.util.ResponseDTOUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressManager addressManager;

    @Resource
    private AddressQueryService addressQueryService;

    /**
     * 添加
     *
     * @param addForm 添加参数
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(AddressAddForm addForm) {
        AddressEntity addressEntity = SmartBeanUtil.copy(addForm, AddressEntity.class);
        addressManager.save(addressEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm 更新参数
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(AddressUpdateForm updateForm) {
        AddressEntity addressEntity = SmartBeanUtil.copy(updateForm, AddressEntity.class);
        //更新并更新缓存
        addressManager.update(addressEntity);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     *
     * @param idList 集合
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        //删除并删除缓存
        addressManager.batchDelete(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     *
     * @param addressId id
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long addressId) {
        if (null == addressId) {
            return ResponseDTO.ok();
        }

        //删除并删除缓存
        addressManager.deleteById(addressId);
        return ResponseDTO.ok();
    }

    /**
     * 收货地址导入
     *
     * @param file 上传文件
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> importAddress(MultipartFile file) {
        List<AddressImportForm> dataList;
        try {
            dataList = FastExcel.read(file.getInputStream()).head(AddressImportForm.class)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            throw new BusinessException("数据格式存在问题，无法读取");
        }
        if (CollectionUtils.isEmpty(dataList)) {
            return ResponseDTO.userErrorParam("数据为空");
        }

        //获取所以收货单位
        List<String> addressName = dataList.stream().map(AddressImportForm::getName).toList();

        //查询数据库存在的收获单位
        Map<String, AddressEntity> exitAddressMap = addressQueryService.queryByNamesToMap(addressName);

        List<AddressEntity> insertToAddress = new ArrayList<>();
        List<AddressEntity> updateToAddress = new ArrayList<>();
        for (AddressImportForm addressImportForm : dataList) {
            //收获单位
            AddressEntity address = exitAddressMap.get(addressImportForm.getName());

            //收获单位为空则新增，否则更新
            if (address == null) {
                insertToAddress.add(createAddress(addressImportForm.getName(), addressImportForm.getPerson(), addressImportForm.getTelephone(), addressImportForm.getAddress()));
            } else {
                updateToAddress.add(createAddress(addressImportForm.getName(), addressImportForm.getPerson(), addressImportForm.getTelephone(), addressImportForm.getAddress()));
            }
        }

        JoinerResult resultMsg = JoinerResult.createJoiner();
        return ResponseDTOUtil.buildResponseDTO(insertToAddress, updateToAddress, addressManager::saveBatch, addressManager::batchUpdate, resultMsg);
    }

    /**
     * 创建收货地址
     *
     * @param name      收货单位
     * @param person    联系人
     * @param telephone 电话
     * @param address   地址
     * @return AddressEntity
     */
    public AddressEntity createAddress(String name, String person, String telephone, String address) {
        return AddressEntity.builder()
                .name(name)
                .person(person)
                .telephone(telephone)
                .address(address)
                .createUserId(SmartRequestUtil.getRequestUser().getUserId())
                .createUserName(SmartRequestUtil.getRequestUser().getUserName())
                .build();
    }


}
