package com.ltgk.video.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.common.annotation.Log;
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.SecurityUtils;
import com.ltgk.common.utils.StringUtils;
import com.ltgk.common.utils.poi.ExcelUtil;
import com.ltgk.common.validation.groups.Add;
import com.ltgk.common.validation.groups.Detail;
import com.ltgk.common.validation.groups.Update;
import com.ltgk.video.domain.entity.DsRadar;
import com.ltgk.video.service.IDsRadarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 雷达管理表 (DsRadar) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-04-30
 */
@RestController
@RequestMapping("/fishingPort/dsRadar")
@Api(tags = "雷达管理表")
public class DsRadarController extends BaseController {

    @Resource
    private IDsRadarService dsRadarService;

    /**
     * 分页查询雷达管理表列表
     *
     * @param req: 雷达管理表 实体对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询雷达管理表列表")
    public TableDataInfo page(@RequestBody DsRadar req) {
        LambdaQueryWrapper<DsRadar> wrapper = getLambdaQueryWrapperByReq(req);
        startPage(req.getPageNum(), req.getPageSize());
        List<DsRadar> list = dsRadarService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 获取雷达管理表详细数据
     *
     * @param req: 雷达管理表 实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取雷达管理表详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody DsRadar req) {
        return AjaxResult.success(dsRadarService.getById(req.getId()));
    }

    /**
     * 新增雷达管理表数据
     * 接口权限: fishingPort:dsRadar:add
     *
     * @param dsRadar: 雷达管理表实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    @Log(title = "新增雷达管理表数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增雷达管理表数据")
    public AjaxResult add(@Validated(Add.class) @RequestBody DsRadar dsRadar) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsRadar.setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsRadarService.save(dsRadar));
    }

    /**
     * 修改雷达管理表数据
     * 接口权限: fishingPort:dsRadar:edit
     *
     * @param dsRadar: 雷达管理表实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    @Log(title = "修改雷达管理表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改雷达管理表数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody DsRadar dsRadar) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsRadar.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsRadarService.updateById(dsRadar));
    }

    /**
     * 删除雷达管理表数据
     * 接口权限: fishingPort:dsRadar:remove
     *
     * @param req: 雷达管理表 实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    @Log(title = "删除雷达管理表数据", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除雷达管理表数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody DsRadar req) {
        return toAjax(dsRadarService.removeById(req.getId()));
    }

    /**
     * 导出雷达管理表数据
     *
     * @param response:
     * @param req:      雷达管理表 实体对象
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    @Log(title = "导出雷达管理表数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出雷达管理表数据")
    public void export(HttpServletResponse response, DsRadar req) {
        LambdaQueryWrapper<DsRadar> wrapper = getLambdaQueryWrapperByReq(req);
        List<DsRadar> list = dsRadarService.list(wrapper);
        ExcelUtil<DsRadar> util = new ExcelUtil<>(DsRadar.class);
        util.exportExcel(response, list, "雷达管理表数据");
    }

    /**
     * 根据请求参数转换成 LambdaQueryWrapper
     *
     * @param req: 雷达管理表 实体对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    private static LambdaQueryWrapper<DsRadar> getLambdaQueryWrapperByReq(DsRadar req) {
        req.queryCheck();
        String startTime = req.getStartTime();
        String endTime = req.getEndTime();
        boolean beNotBlankTime = StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime);
        return new LambdaQueryWrapper<>(req)
                .between(beNotBlankTime, DsRadar::getUpdateAt, startTime, endTime);
    }
}

