package com.ltgk.catches.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.boat.domain.dto.GetBoatListReq;
import com.ltgk.boat.domain.entity.DsBoatData;
import com.ltgk.boat.service.DsBoatDataService;
import com.ltgk.catches.domain.dto.GetFishLimitAllocationListReq;
import com.ltgk.catches.domain.dto.SaveFishLimitAllocationReq;
import com.ltgk.catches.domain.entity.TcFishLimitAllocation;
import com.ltgk.catches.domain.vo.FishLimitAllocationVo;
import com.ltgk.catches.service.TcFishLimitAllocationService;
import com.ltgk.common.annotation.Log;
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.BusinessType;
import com.ltgk.common.utils.bean.BeanUtils;
import com.ltgk.port.domain.dto.RemoveByIdReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 限额捕捞 限额分配(tc_fish_limit_allocation)表控制层
 *
 * @author xxxxx
 */
@Api(tags = "限额分配")
@Validated
@RestController
@RequestMapping("/fishingPort/fishLimit")
public class TcFishLimitAllocationController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TcFishLimitAllocationService tcFishLimitAllocationService;
    @Resource
    private DsBoatDataService dsBoatDataService;

    @Log(title = "限额捕捞限额分配新增修改", businessType = BusinessType.INSERT_OR_UPDATE)
    @ApiOperation(value = "限额捕捞限额分配新增修改")
    @PostMapping("/save")
    @RepeatSubmit(message = "请勿重复提交")
    public AjaxResult save(@Valid @RequestBody List<SaveFishLimitAllocationReq> reqList) {
        return tcFishLimitAllocationService.save(reqList);
    }

    @PreAuthorize("@ss.hasPermi('fishingPort:fishLimit:delete')")
    @Log(title = "限额捕捞限额分配删除", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "限额捕捞限额分配删除")
    public AjaxResult delete(@Valid @RequestBody RemoveByIdReq removeByIdReq) {
        return AjaxResult.success(tcFishLimitAllocationService.removeById(removeByIdReq.getId()));
    }

    /**
     * 限额捕捞限额分配列表
     *
     * @param getFishLimitAllocationListReq
     * @return {@link List<FishLimitAllocationVo>}
     */
    @PreAuthorize("@ss.hasPermi('fishingPort:fishLimit:list')")
    @Log(title = "限额捕捞限额分配列表", businessType = BusinessType.PAGE_QUERY)
    @PostMapping("/list")
    @ApiOperation(value = "限额捕捞限额分配列表")
    public TableDataInfo list(@Valid @RequestBody GetFishLimitAllocationListReq getFishLimitAllocationListReq) {
        return getDataTable(tcFishLimitAllocationService.list(getFishLimitAllocationListReq));
    }

    /**
     * 查询排除已经配置过捕捞限额数据的渔船的信息
     *
     * @param req：渔船查询信息
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025/2/28
     */
    @PreAuthorize("@ss.hasPermi('fishingPort:fishLimitSet:findBoatNotSetLimit')")
    @PostMapping("/findBoatNotSetLimit")
    public TableDataInfo findBoatNotSetLimit(@RequestBody GetBoatListReq req) {
        req.queryCheck();

        // 查询所有已经配置了限额的渔船       tc_fish_limit_allocation
        Set<String> setLimitBoatIdSet = tcFishLimitAllocationService.list()
                .stream()
                .map(TcFishLimitAllocation::getBoatId)
                .collect(Collectors.toSet());

        DsBoatData dsBoatData = new DsBoatData();
        BeanUtils.copyBeanProp(dsBoatData, req);
        LambdaQueryWrapper<DsBoatData> wrapper = new LambdaQueryWrapper<>(dsBoatData)
                .notIn(CollectionUtil.isNotEmpty(setLimitBoatIdSet), DsBoatData::getBoatId, setLimitBoatIdSet);
        startPage(req.getPageNum(), req.getPageSize());
        return getDataTable(dsBoatDataService.list(wrapper));
    }
}
