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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.common.core.domain.entity.SysUser;
import com.sundot.airport.common.domain.BaseAttachment;
import com.sundot.airport.common.enums.BaseAttachmentSourceTypeEnum;
import com.sundot.airport.common.service.IBaseAttachmentService;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.common.utils.StringUtils;
import com.sundot.airport.item.domain.*;
import com.sundot.airport.system.mapper.BaseSeizeItemMapper;
import com.sundot.airport.system.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sundot.airport.item.mapper.ItemSeizureItemsMapper;
import com.sundot.airport.item.service.IItemSeizureItemsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 查获物品明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@Service
public class ItemSeizureItemsServiceImpl extends ServiceImpl<ItemSeizureItemsMapper, ItemSeizureItems> implements IItemSeizureItemsService {
    @Autowired
    private IBaseAttachmentService baseAttachmentService;
    @Autowired
    private ItemSeizureItemsMapper itemSeizureItemsMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private BaseSeizeItemMapper baseSeizeItemMapper;


    /**
     * 查询查获物品明细
     *
     * @param id 查获物品明细主键
     * @return 查获物品明细
     */
    @Override
    public ItemSeizureItems selectItemSeizureItemsById(Long id) {
        return itemSeizureItemsMapper.selectItemSeizureItemsById(id);
    }

    /**
     * 查询查获物品明细列表
     *
     * @param itemSeizureItems 查获物品明细
     * @return 查获物品明细
     */
    @Override
    public List<ItemSeizureItems> selectItemSeizureItemsList(ItemSeizureItems itemSeizureItems) {
        List<ItemSeizureItems> result = itemSeizureItemsMapper.selectItemSeizureItemsList(itemSeizureItems);
        if (!CollectionUtils.isEmpty(result)) {
            for (ItemSeizureItems itemSeizureItem : result) {
                String itemType = baseSeizeItemMapper.getItemTypeByName(itemSeizureItem.getItemName());
                if (StringUtils.isNotEmpty(itemType)) {
                    itemSeizureItem.setCategoryNameOne(itemType);
                }
            }
        }
        LambdaQueryWrapper<BaseAttachment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseAttachment::getSourceType, BaseAttachmentSourceTypeEnum.ITEM_SEIZURE_ITEMS.getCode())
                .in(BaseAttachment::getSourceId, result.stream().map(ItemSeizureItems::getId).toArray());
        List<BaseAttachment> tempBaseAttachmentList = baseAttachmentService.getBaseMapper().selectList(wrapper);
        Map<Long, List<BaseAttachment>> tempBaseAttachmentMap = tempBaseAttachmentList.stream().collect(Collectors.groupingBy(BaseAttachment::getSourceId));
        result.forEach(item -> {
            item.setBaseAttachmentList(tempBaseAttachmentMap.get(item.getId()));
        });
        return result;
    }

    /**
     * 新增查获物品明细
     *
     * @param itemSeizureItems 查获物品明细
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertItemSeizureItems(ItemSeizureItems itemSeizureItems) {
        itemSeizureItems.setCreateTime(DateUtils.getNowDate());
        int result = itemSeizureItemsMapper.insertItemSeizureItems(itemSeizureItems);
        if (!CollectionUtils.isEmpty(itemSeizureItems.getBaseAttachmentList())) {
            itemSeizureItems.getBaseAttachmentList().forEach(baseAttachment -> {
                baseAttachment.setCreateBy(itemSeizureItems.getCreateBy());
                baseAttachment.setCreateTime(DateUtils.getNowDate());
                baseAttachment.setSourceType(BaseAttachmentSourceTypeEnum.ITEM_SEIZURE_ITEMS.getCode());
                baseAttachment.setSourceTypeName(BaseAttachmentSourceTypeEnum.ITEM_SEIZURE_ITEMS.getInfo());
                baseAttachment.setSourceId(itemSeizureItems.getId());
            });
            baseAttachmentService.saveBatch(itemSeizureItems.getBaseAttachmentList());
        }
        return result;
    }

    /**
     * 修改查获物品明细
     *
     * @param itemSeizureItems 查获物品明细
     * @return 结果
     */
    @Override
    public int updateItemSeizureItems(ItemSeizureItems itemSeizureItems) {
        itemSeizureItems.setUpdateTime(DateUtils.getNowDate());
        return itemSeizureItemsMapper.updateItemSeizureItems(itemSeizureItems);
    }

    /**
     * 批量删除查获物品明细
     *
     * @param ids 需要删除的查获物品明细主键
     * @return 结果
     */
    @Override
    public int deleteItemSeizureItemsByIds(Long[] ids) {
        return itemSeizureItemsMapper.deleteItemSeizureItemsByIds(ids);
    }

    /**
     * 删除查获物品明细信息
     *
     * @param id 查获物品明细主键
     * @return 结果
     */
    @Override
    public int deleteItemSeizureItemsById(Long id) {
        return itemSeizureItemsMapper.deleteItemSeizureItemsById(id);
    }

    /**
     * 根据记录ID查询查获物品明细列表
     *
     * @param recordId 记录ID
     * @return 查获物品明细集合
     */
    @Override
    public List<ItemSeizureItems> listByRecorId(Long recordId) {
        ItemSeizureItems itemSeizureItems = new ItemSeizureItems();
        itemSeizureItems.setRecordId(recordId);
        return selectItemSeizureItemsList(itemSeizureItems);
    }

    /**
     * 根据筛选条件查询查获物品明细列表
     *
     * @param itemSeizureParam 筛选条件
     * @return 查获物品明细集合
     */
    @Override
    public List<ItemSeizureDto> selectByConditions(ItemSeizureParam itemSeizureParam) {
        List<ItemSeizureDto> itemSeizureParams = itemSeizureItemsMapper.selectByConditions(itemSeizureParam);
        if (!CollectionUtils.isEmpty(itemSeizureParams)) {
            for (ItemSeizureDto itemSeizureDto : itemSeizureParams) {
                if (itemSeizureDto.getCheckMethod().equals("AUDIO")) {
                    itemSeizureDto.setCategoryNameTwo(itemSeizureDto.getItemName());
                    itemSeizureDto.setRegionalName(itemSeizureDto.getTerminlName() + "/" + itemSeizureDto.getRegionalName() + "/" + itemSeizureDto.getAttendanceStationName());
                    SysUser sysUser = sysUserService.selectUserById(itemSeizureDto.getInspectUserId());
                    if (Objects.nonNull(sysUser)) {
                        itemSeizureDto.setInspectUserName(sysUser.getNickName());
                    }
                } else {
                    SysUser sysUser = sysUserService.selectUserById(itemSeizureDto.getInspectUserId());
                    if (Objects.nonNull(sysUser)) {
                        itemSeizureDto.setInspectUserName(sysUser.getNickName());
                    }
                }
            }
            return itemSeizureParams;
        }
        return itemSeizureParams;
    }

    /**
     * 根据筛选条件查询分类数量
     *
     * @param itemSeizureParam 筛选条件
     * @return 分类数量集合
     */
    @Override
    public List<ItemSeizureCount> selectGroupCount(ItemSeizureParam itemSeizureParam) {
        return itemSeizureItemsMapper.selectGroupCount(itemSeizureParam);
    }

    /**
     * 根据筛选条件查询数量
     *
     * @param itemSeizureParam 筛选条件
     * @return 数量
     */
    @Override
    public ItemSeizureTotal selectCount(ItemSeizureParam itemSeizureParam) {
        return itemSeizureItemsMapper.selectCount(itemSeizureParam);
    }

    /**
     * 根据记录ID列表查询查获物品明细列表
     *
     * @param recordIds 记录ID列表
     * @return 查获物品明细集合
     */
    @Override
    public List<ItemSeizureItems> selectItemSeizureItemsByRecordIds(List<Long> recordIds) {
        if (recordIds == null || recordIds.isEmpty()) {
            return new ArrayList<>();
        }
        return itemSeizureItemsMapper.selectItemSeizureItemsByRecordIds(recordIds);
    }
}
