package com.ltgk.boat.controller;

import com.ltgk.boat.domain.dto.*;
import com.ltgk.boat.domain.entity.DsBoatData;
import com.ltgk.boat.domain.entity.DsBoatDevice;
import com.ltgk.boat.domain.entity.DsBoatInsurance;
import com.ltgk.boat.domain.vo.BoatDeviceCountVO;
import com.ltgk.boat.domain.vo.BoatHomePortExportVO;
import com.ltgk.boat.domain.vo.DsBoatCredentialVO;
import com.ltgk.boat.service.DsBoatDataService;
import com.ltgk.boat.service.IDsBoatDeviceService;
import com.ltgk.common.annotation.RepeatSubmit;
import com.ltgk.common.core.controller.BaseController;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.core.page.TableDataInfo;
import com.ltgk.common.enums.DsBoatPowerQueryEnums;
import com.ltgk.common.enums.DsBoatTypeEnums;
import com.ltgk.common.utils.bean.BeanUtils;
import com.ltgk.common.utils.poi.ExcelUtil;
import com.ltgk.common.vo.DsBoatPowerQueryEnumVO;
import com.ltgk.common.vo.DsBoatTypeEnumVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: wan kun
 * @Description: TODO
 * @Date: 2024/10/25 17:24
 * @Version: 1.0
 */
@RestController
@Api(tags = "鱼船档案管理", description = "鱼船档案管理")
@RequestMapping("/fishingPort/fishingBoatArchives")
@Valid
public class FishingBoatArchivesController extends BaseController {
    @Resource
    private DsBoatDataService dsBoatDataService;
    @Resource
    private IDsBoatDeviceService dsBoatDeviceService;

    /**
     * 查询所有的渔船种类枚举类
     *
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/3/28
     */
    @PostMapping("/findAllBoatTypeEnumList")
    public AjaxResult findAllBoatTypeEnumList() {
        return success(Arrays.stream(DsBoatTypeEnums.values())
                .map(item -> {
                    DsBoatTypeEnumVO dsBoatTypeEnumVO = new DsBoatTypeEnumVO();
                    BeanUtils.copyBeanProp(dsBoatTypeEnumVO, item);
                    return dsBoatTypeEnumVO;
                }).collect(Collectors.toList()));
    }

    /**
     * 查询所有的查询的渔船功率的范围的数据
     *
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/4/3
     */
    @PostMapping("/findAllDsBoatPowerQueryEnumList")
    public AjaxResult findAllDsBoatPowerQueryEnumList() {
        return success(Arrays.stream(DsBoatPowerQueryEnums.values())
                .map(item -> {
                    DsBoatPowerQueryEnumVO dsBoatPowerQueryEnumVO = new DsBoatPowerQueryEnumVO();
                    BeanUtils.copyBeanProp(dsBoatPowerQueryEnumVO, item);
                    return dsBoatPowerQueryEnumVO;
                }).collect(Collectors.toList()));
    }

    /**
     * 获取渔船档案列表
     *
     * @param getBoatListReq:
     * @return {@link List<DsBoatData>}
     */
    @PostMapping("/getBoatList")
    public TableDataInfo getBoatList(@RequestBody GetBoatListReq getBoatListReq) throws Throwable {
        return getDataTable(dsBoatDataService.getBoatList(getBoatListReq));
    }

    /**
     * 查看系统中的渔船设置的终端的相关的统计信息
     *
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/4/3
     */
    @ApiOperation(value = "查看系统中渔船的终端相关统计")
    @PostMapping("/getBoatDeviceCount")
    public AjaxResult getBoatDeviceCount() {
        // 查询所有的渔船的数据
        List<String> dsBoatIdList = dsBoatDataService.list()
                .stream()
                .map(DsBoatData::getBoatId)
                .collect(Collectors.toList());
        // 查询所有的终端的数据
        List<DsBoatDevice> dsBoatDeviceList = dsBoatDeviceService.list();

        List<String> dsBoatDeviceIdList = dsBoatDeviceList.stream()
                .map(DsBoatDevice::getBoatId)
                .collect(Collectors.toList());

        // AIS 设备
        List<String> aisBoatIdList = dsBoatDeviceList.stream()
                .filter(item -> Objects.nonNull(item.getDeviceType()) && "AIS".equals(item.getDeviceType()))
                .map(DsBoatDevice::getBoatId)
                .collect(Collectors.toList());

        // 北斗设备
        List<String> beidouBoatIdList = dsBoatDeviceList.stream()
                .filter(item -> Objects.nonNull(item.getDeviceType()) && "Beidou".equals(item.getDeviceType()))
                .map(DsBoatDevice::getBoatId)
                .collect(Collectors.toList());

        BoatDeviceCountVO boatDeviceCountVO = new BoatDeviceCountVO();

        boatDeviceCountVO.setAisCount(dsBoatIdList.stream().filter(aisBoatIdList::contains).count());
        boatDeviceCountVO.setBeidouCount(dsBoatIdList.stream().filter(beidouBoatIdList::contains).count());
        boatDeviceCountVO.setAisAndBeidouCount(dsBoatIdList.stream().filter(item -> aisBoatIdList.contains(item) && beidouBoatIdList.contains(item)).count());
        boatDeviceCountVO.setNoDeviceCount(dsBoatIdList.stream().filter(item -> !dsBoatDeviceIdList.contains(item)).count());
        return success(boatDeviceCountVO);
    }

    /**
     * 大屏查询渔船信息 根据终端号进行查询数据
     * 接口权限：后端暂未设置权限
     *
     * @param req:
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025/3/19
     */
    @PostMapping("/findBoatListByMmsi")
    public TableDataInfo findBoatListByMmsi(@RequestBody FindBoatListByMmsiDto req) {
        startPage(req.getPageNum(), req.getPageSize());
        List<DsBoatDevice> dsBoatDeviceList = dsBoatDeviceService.lambdaQuery()
                .like(DsBoatDevice::getMmsi, req.getMmsi())
                .list();
        Optional.ofNullable(dsBoatDeviceList)
                .orElseGet(Collections::emptyList)
                .parallelStream()
                .filter(Objects::nonNull)
                .forEach(item -> item.setTerminalCode(item.getMmsi()));
        return getDataTable(dsBoatDeviceList);
    }

    /**
     * 母港管理导出
     *
     * @param response:
     * @param req:
     * @author Qi ChengBin
     * @date 2025/2/26
     */
    @PostMapping("/exportBoatHomePort")
    public void exportBoatHomePort(HttpServletResponse response, GetBoatListReq req) throws Throwable {
        req.setPageSize(999999);
        List<DsBoatData> boatList = dsBoatDataService.getBoatList(req);
        List<BoatHomePortExportVO> boatHomePortExportVOList = new ArrayList<>();
        boatList.forEach(item -> {
            BoatHomePortExportVO boatHomePortExportVO = new BoatHomePortExportVO();
            BeanUtils.copyBeanProp(boatHomePortExportVO, item);
            boatHomePortExportVOList.add(boatHomePortExportVO);
        });
        ExcelUtil<BoatHomePortExportVO> util = new ExcelUtil<>(BoatHomePortExportVO.class);
        util.exportExcel(response, boatHomePortExportVOList, "母港管理");
    }

    /**
     * 新增渔船档案
     *
     * @param saveBoatReq
     * @return AjaxResult
     */
    @RepeatSubmit(message = "请勿重复提交")
    @ApiOperation(value = "新增渔船档案")
    @PostMapping("/saveBoat")
    public AjaxResult saveBoat(@RequestBody @Validated SaveBoatReq saveBoatReq) {
        return dsBoatDataService.saveBoat(saveBoatReq);
    }

    /**
     * 修改渔船档案信息
     *
     * @param saveBoatReq
     * @return AjaxResult
     */
    @ApiOperation(value = "修改渔船档案信息")
    @PostMapping("/updateBoat")
    public AjaxResult updateBoat(@RequestBody @Validated SaveBoatReq saveBoatReq) {
        return dsBoatDataService.updateBoat(saveBoatReq);
    }

    /**
     * 删除船渔船档案信息
     *
     * @param deleteByIdsReq
     * @return AjaxResult
     * @return
     */
    @ApiOperation(value = "删除船渔船档案信息")
    @PostMapping("/deleteBoat")
    public AjaxResult deleteBoat(@RequestBody @Validated DeleteByIdsReq deleteByIdsReq) {
        return dsBoatDataService.deleteBoat(deleteByIdsReq);
    }

    /**
     * 导出船舶信息
     *
     * @param getBoatListReq
     * @return AjaxResult
     * @throws Exception
     */
    @ApiOperation(value = "导出船舶信息")
    @GetMapping("/export")
    public void export(HttpServletResponse response, @ModelAttribute GetBoatListReq getBoatListReq) throws Throwable {
        dsBoatDataService.export(response, getBoatListReq);
    }

    /**
     * 下载导入船舶信息模板
     *
     * @return AjaxResult
     */
    @ApiOperation(value = "下载导入船舶信息模板")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        dsBoatDataService.importTemplate(response);
    }

    /**
     * 导入渔船数据
     *
     * @param file
     * @return AjaxResult
     * @throws IOException
     */
    @RepeatSubmit(message = "请勿重复提交")
    @ApiOperation(value = "导入渔船数据")
    @PostMapping("/importBoat")
    public AjaxResult importBoat(@RequestPart("file") MultipartFile file) throws IOException {
        return dsBoatDataService.importBoat(file);
    }

    /**
     * 获取证书列表
     *
     * @param getBoatCredentialListReq
     * @return {@link List< DsBoatCredentialVO >}
     */
    @ApiOperation(value = "获取证书列表")
    @PostMapping("/getBoatCredentialList")
    public TableDataInfo getBoatCredentialList(@Validated @RequestBody GetBoatCredentialListReq getBoatCredentialListReq) {
        return getDataTable(dsBoatDataService.getBoatCredentialList(getBoatCredentialListReq));
    }

    /**
     * 新增修改证书
     *
     * @param saveBoatCredentialReq
     * @return AjaxResult
     */
    @RepeatSubmit(message = "请勿重复提交")
    @ApiOperation(value = "新增修改证书")
    @PostMapping("/saveBoatCredential")
    public AjaxResult saveBoatCredential(@Validated @RequestBody SaveBoatCredentialReq saveBoatCredentialReq) {
        return dsBoatDataService.saveBoatCredential(saveBoatCredentialReq);
    }

    /**
     * 获取渔船保险列表
     *
     * @param getBoatInsuranceReq
     * @return {@link List<DsBoatInsurance >}
     */
    @ApiOperation(value = "获取渔船保险列表")
    @PostMapping("/getBoatInsuranceList")
    public TableDataInfo getBoatInsuranceList(@Validated @RequestBody GetBoatInsuranceReq getBoatInsuranceReq) {
        return getDataTable(dsBoatDataService.getBoatInsuranceList(getBoatInsuranceReq));
    }

    /**
     * 分页查询渔船信用列表
     *
     * @param req: 分页查询渔船信用列表请求参数
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025/2/27
     */
    @ApiOperation(value = "分页查询渔船信用列表")
    @PostMapping("/findBoatCreditList")
    public TableDataInfo findBoatCreditList(@Validated @RequestBody FindBoatCreditReq req) {
        return getDataTable(dsBoatDataService.findBoatCreditList(req));
    }

    /**
     * 一船一档
     *
     * @param getBoatMessagesReq
     * @return AjaxResult
     */
    @ApiOperation(value = "一船一档")
    @PostMapping("/getBoatMessages")
    public AjaxResult getBoatMessages(@Validated @RequestBody GetBoatMessagesReq getBoatMessagesReq) {
        return dsBoatDataService.getBoatMessages(getBoatMessagesReq);
    }

    /**
     * 业务模块首页查询卡口抓拍、渔船总数、在线数量、离线数量、在外作业渔船数量 的数据
     * 卡口抓拍：查询从0点到当前抓拍的数量
     *
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/3/4
     */
    @ApiOperation(value = "业务模块右下角总数")
    @PostMapping("/getBusinessIndexStatistics")
    public AjaxResult getBusinessIndexStatistics() {
        return success(dsBoatDataService.getBusinessIndexStatistics());
    }

    /**
     * 根据渔船ID 获取渔船信息
     * 后端权限：后端没有权限设置
     *
     * @param req:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/4/2
     */
    @ApiOperation(value = "根据boatId 获取渔船信息")
    @PostMapping("/getBoatInfoByBoatId")
    public AjaxResult getBoatInfoByBoatId(@RequestBody GetBoatInfoByBoatIdReq req) {
        return success(dsBoatDataService.GetBoatInfoByBoatId(req));
    }

    /**
     * 查询海域渔船明细：
     * -- 因没有管辖海域，则使用港口信息来进行查询
     *
     * @param req：
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/5/19
     */
    @PostMapping("/findSeaBoatList")
    public AjaxResult findSeaBoatList(@RequestBody FindSeaBoatListReq req) {
        return success(dsBoatDataService.findSeaBoatList(req));
    }
}
