package com.ltgk.catches.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.catches.domain.dto.QueryTpFishCatchReq;
import com.ltgk.catches.domain.dto.TimeFrameReq;
import com.ltgk.catches.domain.entity.TpFishCatch;
import com.ltgk.catches.service.ITpFishCatchService;
import com.ltgk.common.annotation.Log;
import com.ltgk.common.annotation.RepeatSubmit;
import com.ltgk.common.constant.Constants;
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.StringUtils;
import com.ltgk.common.utils.bean.BeanUtils;
import com.ltgk.common.validation.groups.Detail;
import com.ltgk.common.validation.groups.Update;
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;

/**
 * 渔获上岸记录 前端控制器
 *
 * @author Qi ChengBin
 * @date 2025-01-07
 */
@Api(tags = "渔获上岸记录")
@RestController
@RequestMapping("/fishingPort/tpFishCatch")
public class TpFishCatchController extends BaseController {

    @Resource
    private ITpFishCatchService tpFishCatchService;

    /**
     * 分页查询渔获上岸记录列表
     *
     * @param queryTpFishCatchReq: 查询渔获上岸记录数据请求参数对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-01-07
     */
    @PreAuthorize("@ss.hasPermi('fishingPort:tpFishCatch:page')")
    @PostMapping("/page")
    @ApiOperation(value = "分页查询渔获上岸记录列表")
    public TableDataInfo page(@RequestBody QueryTpFishCatchReq queryTpFishCatchReq) {
        queryTpFishCatchReq.queryCheck();
        TpFishCatch tpFishCatch = new TpFishCatch();
        BeanUtils.copyProperties(queryTpFishCatchReq, tpFishCatch);

        LambdaQueryWrapper<TpFishCatch> wrapper = new LambdaQueryWrapper<>(tpFishCatch);
        wrapper.eq(TpFishCatch::getDelFlag, Constants.FLAG_FALSE)
                .ge(StringUtils.isNotBlank(queryTpFishCatchReq.getBeginTime()), TpFishCatch::getLandTime, queryTpFishCatchReq.getBeginTime())
                .le(StringUtils.isNotBlank(queryTpFishCatchReq.getEndTime()), TpFishCatch::getLandTime, queryTpFishCatchReq.getEndTime())
                .orderByDesc(TpFishCatch::getUpdateAt);
        startPage(queryTpFishCatchReq.getPageNum(), queryTpFishCatchReq.getPageSize());
        return getDataTable(tpFishCatchService.list(wrapper));
    }

    /**
     * 获取渔获上岸记录详细信息
     *
     * @param queryTpFishCatchReq:查询渔获上岸记录数据请求参数对象，只要里边的id
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-07
     */
    @PreAuthorize("@ss.hasPermi('fishingPort:tpFishCatch:detail')")
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取渔获上岸记录详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody QueryTpFishCatchReq queryTpFishCatchReq) {
        return success(tpFishCatchService.getTpFishCatchDetailById(queryTpFishCatchReq.getId()));
    }

    /**
     * 新增渔获上岸记录信息
     *
     * @param tpFishCatch:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-07
     */
    @RepeatSubmit(message = "请勿重复提交")
    @PreAuthorize("@ss.hasPermi('fishingPort:tpFishCatch:add')")
    @Log(title = "新增渔获上岸记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增渔获上岸记录数据")
    public AjaxResult add(@RequestBody TpFishCatch tpFishCatch) {
        return toAjax(tpFishCatchService.saveTpFishCatch(tpFishCatch));
    }

    /**
     * 修改渔获上岸记录信息
     *
     * @param tpFishCatch:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-07
     */
    @PreAuthorize("@ss.hasPermi('fishingPort:tpFishCatch:edit')")
    @Log(title = "修改渔获上岸记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改渔获上岸记录数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody TpFishCatch tpFishCatch) {
        return toAjax(tpFishCatchService.updateTpFishCatch(tpFishCatch));
    }

    /**
     * 删除渔获上岸记录信息
     *
     * @param tpFishCatch: 渔获上岸记录主键
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-01-07
     */
    @PreAuthorize("@ss.hasPermi('fishingPort:tpFishCatch:remove')")
    @Log(title = "删除渔获上岸记录", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除渔获上岸记录信息")
    public AjaxResult delete(@Validated(Update.class) @RequestBody TpFishCatch tpFishCatch) {
        return toAjax(tpFishCatchService.removeFishCatchAndCatchRecordById(tpFishCatch.getId()));
    }

    /**
     * 获取指定时间范围内渔获上岸总量
     * -- 传输数据为：timeInterval
     * ---- day: 当天
     * ---- week:当周
     * ---- month:当月
     * -- startTime：开始时间，传输参数中不能和 timeInterval 共存
     * -- endTime: 结束时间，传输参数中不能和 timeInterval 共存
     *
     * @param timeFrameReq: 获取渔获上岸总量查询请求参数
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/1/8 15:20
     */
    @PreAuthorize("@ss.hasPermi('fishingPort:tpFishCatch:rangeTotalNum')")
    @PostMapping("/rangeTotalNum")
    @ApiOperation(value = "获取指定时间范围内渔获上岸总量")
    public AjaxResult rangeTotalNum(@RequestBody TimeFrameReq timeFrameReq) {
        return success(tpFishCatchService.rangeTotalNum(timeFrameReq.getStartTime(), timeFrameReq.getEndTime()));
    }
}
