package com.ltgk.port.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ltgk.common.core.domain.BaseEntity;
import com.ltgk.common.enums.EnterOutPortType;
import com.ltgk.port.domain.dto.DsPortDTO;
import com.ltgk.port.domain.dto.GetOutEnterPortListReq;
import com.ltgk.port.domain.entity.DsPort;
import com.ltgk.port.domain.vo.BoatEnterOutVo;
import com.ltgk.port.domain.vo.BoatNavigateStatusVo;
import com.ltgk.port.domain.vo.BusinessIndexPortStatisticsVO;
import com.ltgk.port.mapper.DsPortMapper;
import com.ltgk.port.service.IDsPortService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * 渔港信息 服务接口实现
 *
 * @author Qi ChengBin
 * @date 2024-12-09
 */
@Service
public class DsPortServiceImpl extends ServiceImpl<DsPortMapper, DsPort> implements IDsPortService {

    @Resource
    private PortStatisticsServiceImpl portStatisticsService;

    /**
     * 业务板块 首页 渔港统计查询
     * -- 根据权限进行查询
     * -- 后台接口权限：后台暂无权限设置
     *
     * @param baseEntity:用于接收数据权限组装参数
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/3/21
     */
    @Override
    public List<DsPort> getBusinessIndexPortStatistics(BaseEntity baseEntity) {
        // 根据数据权限查询渔港列表
        List<DsPort> dsPortList = baseMapper.getBusinessIndexPortStatistics(baseEntity);
        dsPortList.forEach(dsPort -> {
            try {
                List<BoatNavigateStatusVo> currentTimeInPortBoatCount = portStatisticsService.getCurrentTimeInPortBoatCount(dsPort);
                if (CollectionUtils.isNotEmpty(currentTimeInPortBoatCount)) {
                    dsPort.setBoatNumInPort((long) currentTimeInPortBoatCount.size());
                } else {
                    dsPort.setBoatNumInPort(0L);
                }
            } catch (JsonProcessingException e) {
                log.error("获取当前时间在港内渔船数量失败", e);
            }
        });
        return dsPortList;
    }

    /**
     * 根据港口id 查询渔港的进港数、离港数、在港数
     * -- 数据均为当天00:00:00到当前时间的数据
     *
     * @param dsPortDTO:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/3/21
     */
    @Override
    public BusinessIndexPortStatisticsVO getPortStatisticsByPortId(DsPortDTO dsPortDTO) {
        BusinessIndexPortStatisticsVO vo = new BusinessIndexPortStatisticsVO();
        // 当前时间
        Date nowDate = new Date();
        // 当前天的开始时间
        Date beginOfDay = DateUtil.beginOfDay(nowDate);

        // 进港数量
        vo.setEnterPortCount(baseMapper.getEnterOrOutPortCount(EnterOutPortType.ENTER_PORT.getCode(), beginOfDay, nowDate, dsPortDTO.getId()));

        // 出港数量
        vo.setOutPortCount(baseMapper.getEnterOrOutPortCount(EnterOutPortType.OUT_PORT.getCode(), beginOfDay, nowDate, dsPortDTO.getId()));

        // 在港数量
        DsPort dbDsPort = baseMapper.selectById(dsPortDTO.getId());
        try {
            vo.setInPortCount(CollectionUtils.isNotEmpty(portStatisticsService.getCurrentTimeInPortBoatCount(dbDsPort)) ? (long) portStatisticsService.getCurrentTimeInPortBoatCount(dbDsPort).size():0L);
        } catch (JsonProcessingException e) {
            log.error("获取当前时间在港内渔船数量失败", e);
        }
        return vo;
    }

    /**
     * 当日进出港列表
     * -- 00：00：00 到当前时间 的数据列表
     *
     * @param req:
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025/3/21
     */
    @Override
    public List<BoatEnterOutVo> findOutEnterPortList(GetOutEnterPortListReq req) {
        // 当前时间
        Date nowDate = new Date();
        // 当前天的开始时间
        Date beginOfDay = DateUtil.beginOfDay(nowDate);
        startPage(req.getPageNum(), req.getPageSize());
        if (req.getType().equals(0)) {
            return baseMapper.getEnterOrOutPortList(req, EnterOutPortType.ENTER_PORT.getCode(), beginOfDay, nowDate);
        } else {
            return baseMapper.getEnterOrOutPortList(req, EnterOutPortType.OUT_PORT.getCode(), beginOfDay, nowDate);
        }
    }
}
