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

import java.util.Collections;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.ledger.domain.LedgerSeizureStats;
import com.sundot.airport.ledger.domain.vo.CountQueryReqVO;
import com.sundot.airport.ledger.domain.vo.CountSeizureSingleQuantityResVO;
import com.sundot.airport.ledger.domain.vo.CountSeizureTotalQuantityResVO;
import com.sundot.airport.ledger.domain.vo.SeizeCategoryQuantityVO;
import com.sundot.airport.ledger.domain.vo.SeizeItemVO;
import com.sundot.airport.ledger.domain.vo.SeizeTotalAndItemVO;
import com.sundot.airport.ledger.domain.vo.SeizureAreaVO;
import com.sundot.airport.ledger.mapper.LedgerSeizureStatsMapper;
import com.sundot.airport.ledger.service.ILedgerSeizureStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 查获违规品统计Service实现
 */
@Service
public class LedgerSeizureStatsServiceImpl extends ServiceImpl<LedgerSeizureStatsMapper, LedgerSeizureStats> implements ILedgerSeizureStatsService {

    @Autowired
    private LedgerSeizureStatsMapper mapper;

    @Override
    public List<LedgerSeizureStats> selectList(LedgerSeizureStats query) {
        return mapper.selectList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsert(List<LedgerSeizureStats> list) {
        list.forEach(item -> item.setCreateTime(DateUtils.getNowDate()));
        saveBatch(list);
        return list.size();
    }

    /**
     * 功能描述：近30天查获数量（总表）
     *
     * @param countQueryReq 查询参数
     * @return List<CountSeizureQuantityResVO>
     * @method countSeizureTotalQuantity
     * @author PanHu Sun
     * @date 2026/5/20 10:43
     */
    @Override
    public List<CountSeizureTotalQuantityResVO> countSeizureTotalQuantity(CountQueryReqVO countQueryReq) {
        List<CountSeizureTotalQuantityResVO> seizureQuantityResList = this.baseMapper.countSeizureTotalQuantity(countQueryReq);
        return CollUtil.emptyIfNull(seizureQuantityResList);
    }

    /**
     * 功能描述：近30天查获数量（员工/小组/班组/部门）
     *
     * @param countQueryReq 查询参数
     * @return List<CountSeizureSingleQuantityResVO>
     * @method countSeizureSingleQuantity
     * @author PanHu Sun
     * @date 2026/5/20 10:43
     */
    @Override
    public List<CountSeizureSingleQuantityResVO> countSeizureSingleQuantity(CountQueryReqVO countQueryReq) {
        List<CountSeizureSingleQuantityResVO> seizureQuantityResList = this.baseMapper.countSeizureSingleQuantity(countQueryReq);
        return CollUtil.emptyIfNull(seizureQuantityResList);
    }

    /**
     * 功能描述：查获信息展示
     *
     * @param countQueryReq 查询参数
     * @return SeizeTotalAndItemVO
     * @method getTimeRangeStats
     * @author PanHu Sun
     * @date 2026/5/20 13:43
     */
    @Override
    public SeizeTotalAndItemVO getTimeRangeStats(CountQueryReqVO countQueryReq) {
        SeizeTotalAndItemVO result = new SeizeTotalAndItemVO();
        // 总查获数
        Integer total = this.baseMapper.statsTimeRangeTotal(countQueryReq);
        result.setTotalSeizeNum(total);
        // 柱状明细
        List<SeizeItemVO> itemList = this.baseMapper.statsTimeRangeItem(countQueryReq);
        result.setItemList(itemList);
        return result;
    }

    /**
     * 功能描述：查获物品分布
     *
     * @param countQueryReq 查询参数
     * @return List<SeizeCategoryQuantityVO>
     * @method countSeizeSubjectCategoryQuantity
     * @author PanHu Sun
     * @date 2026/5/20 14:46
     */
    @Override
    public List<SeizeCategoryQuantityVO> countSeizeSubjectCategoryQuantity(CountQueryReqVO countQueryReq) {
        // 无数据自动返回空数组，符合需求
        List<SeizeCategoryQuantityVO> seizeCategoryQuantityList = this.baseMapper.countSeizeSubjectCategoryQuantity(countQueryReq);
        return CollUtil.emptyIfNull(seizeCategoryQuantityList);
    }

    /**
     * 功能描述：查获工作区域分布
     *
     * @param countQueryReq 查询参数
     * @return List<SeizureAreaVO>
     * @method countSeizeAreaQuantity
     * @author PanHu Sun
     * @date 2026/5/20 15:12
     */
    @Override
    public List<SeizureAreaVO> countSeizeAreaQuantity(CountQueryReqVO countQueryReq) {
        // 无数据自动返回空数组，符合需求
        List<SeizureAreaVO> seizureAreaList = this.baseMapper.countSeizeAreaQuantity(countQueryReq);
        return CollUtil.emptyIfNull(seizureAreaList);
    }
}
