package com.sundot.airport.item.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.domain.entity.SysUser;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.item.domain.ItemSeizureDto;
import com.sundot.airport.item.domain.ItemSeizureItems;
import com.sundot.airport.item.domain.ItemSeizureRecordDTO;
import com.sundot.airport.item.service.IItemSeizureItemsService;
import com.sundot.airport.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sundot.airport.item.mapper.ItemSeizureRecordMapper;
import com.sundot.airport.item.domain.ItemSeizureRecord;
import com.sundot.airport.item.service.IItemSeizureRecordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 查获记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@Service
public class ItemSeizureRecordServiceImpl extends ServiceImpl<ItemSeizureRecordMapper, ItemSeizureRecord> implements IItemSeizureRecordService {
    @Autowired
    private IItemSeizureItemsService itemSeizureItemsService;
    @Autowired
    private ItemSeizureRecordMapper itemSeizureRecordMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询查获记录
     *
     * @param id 查获记录主键
     * @return 查获记录
     */
    @Override
    public ItemSeizureRecord selectItemSeizureRecordById(Long id) {
        ItemSeizureRecord result = itemSeizureRecordMapper.selectItemSeizureRecordById(id);
        if (Objects.nonNull(result)) {
            SysUser sysUser = sysUserService.selectUserById(result.getInspectUserId());
            if (Objects.nonNull(sysUser)) {
                result.setInspectUserName(sysUser.getNickName());
            }
        }
        ItemSeizureItems itemSeizureItems = new ItemSeizureItems();
        itemSeizureItems.setRecordId(result.getId());
        List<ItemSeizureItems> itemSeizureItemsList = itemSeizureItemsService.selectItemSeizureItemsList(itemSeizureItems);
        result.setItemSeizureItemsList(itemSeizureItemsList);
        return result;
    }

    /**
     * 查询查获记录列表
     *
     * @param itemSeizureRecord 查获记录
     * @return 查获记录
     */
    @Override
    public List<ItemSeizureRecord> selectItemSeizureRecordList(ItemSeizureRecordDTO itemSeizureRecord) {
        List<ItemSeizureRecord> itemSeizureRecords = itemSeizureRecordMapper.selectItemSeizureRecordList(itemSeizureRecord);
        return itemSeizureRecords;
    }

    /**
     * 查询查获记录列表（用于导出）
     *
     * @param itemSeizureRecord 查获记录
     * @return 查获记录集合
     */
    @Override
    public List<ItemSeizureRecord> selectItemSeizureRecordListForExport(ItemSeizureRecordDTO itemSeizureRecord) {
        // 先查询主表记录
        List<ItemSeizureRecord> itemSeizureRecords = itemSeizureRecordMapper.selectItemSeizureRecordList(itemSeizureRecord);

        // 如果没有主表记录，直接返回
        if (itemSeizureRecords == null || itemSeizureRecords.isEmpty()) {
            return itemSeizureRecords;
        }

        // 提取所有记录的ID
        List<Long> recordIds = new ArrayList<>();
        for (ItemSeizureRecord record : itemSeizureRecords) {
            recordIds.add(record.getId());
        }

        // 根据记录ID列表查询所有相关的物品信息
        List<ItemSeizureItems> itemsList = itemSeizureItemsService.selectItemSeizureItemsByRecordIds(recordIds);

        // 将物品信息按记录ID分组
        Map<Long, List<ItemSeizureItems>> itemsMap = new HashMap<>();
        if (itemsList != null) {
            for (ItemSeizureItems item : itemsList) {
                Long recordId = item.getRecordId();
                if (recordId != null) {
                    if (!itemsMap.containsKey(recordId)) {
                        itemsMap.put(recordId, new ArrayList<>());
                    }
                    itemsMap.get(recordId).add(item);
                }
            }
        }

        // 为每个记录设置物品列表和检查人员名称
        for (ItemSeizureRecord record : itemSeizureRecords) {
            // 设置物品列表
            List<ItemSeizureItems> recordItems = itemsMap.get(record.getId());
            record.setItemSeizureItemsList(recordItems);

            // 填充检查人员名称
            if (Objects.isNull(record.getInspectUserName())) {
                SysUser sysUser = sysUserService.selectUserById(record.getInspectUserId());
                if (Objects.nonNull(sysUser)) {
                    record.setInspectUserName(sysUser.getNickName());
                }
            }
        }

        return itemSeizureRecords;
    }

    /**
     * 新增查获记录
     *
     * @param itemSeizureRecord 查获记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long insertItemSeizureRecord(ItemSeizureRecord itemSeizureRecord) {
        itemSeizureRecord.setCreateTime(DateUtils.getNowDate());
        int result = itemSeizureRecordMapper.insertItemSeizureRecord(itemSeizureRecord);
        if (result < 0) {
            throw new RuntimeException("新增查获记录失败");
        }
        if (!CollectionUtils.isEmpty(itemSeizureRecord.getItemSeizureItemsList())) {
            itemSeizureRecord.getItemSeizureItemsList().forEach(itemSeizureItems -> {
                itemSeizureItems.setRecordId(itemSeizureRecord.getId());
                itemSeizureItemsService.insertItemSeizureItems(itemSeizureItems);
            });
        }
        return itemSeizureRecord.getId();
    }

    /**
     * 修改查获记录
     *
     * @param itemSeizureRecord 查获记录
     * @return 结果
     */
    @Override
    public int updateItemSeizureRecord(ItemSeizureRecord itemSeizureRecord) {
        itemSeizureRecord.setUpdateTime(DateUtils.getNowDate());
        return itemSeizureRecordMapper.updateItemSeizureRecord(itemSeizureRecord);
    }

    /**
     * 批量删除查获记录
     *
     * @param ids 需要删除的查获记录主键
     * @return 结果
     */
    @Override
    public int deleteItemSeizureRecordByIds(Long[] ids) {
        return itemSeizureRecordMapper.deleteItemSeizureRecordByIds(ids);
    }

    /**
     * 删除查获记录信息
     *
     * @param id 查获记录主键
     * @return 结果
     */
    @Override
    public int deleteItemSeizureRecordById(Long id) {
        return itemSeizureRecordMapper.deleteItemSeizureRecordById(id);
    }
}
