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.DsAisStation;
import com.ltgk.video.service.IDsAisStationService;
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;

/**
 * AIS基站管理表 (DsAisStation) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-04-30
 */
@RestController
@RequestMapping("/fishingPort/dsAisStation")
@Api(tags = "AIS基站管理表")
public class DsAisStationController extends BaseController {

    @Resource
    private IDsAisStationService dsAisStationService;

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

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

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

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

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

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

    /**
     * 根据请求参数转换成 LambdaQueryWrapper
     *
     * @param req: AIS基站管理表 实体对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-04-30
     */
    private static LambdaQueryWrapper<DsAisStation> getLambdaQueryWrapperByReq(DsAisStation 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, DsAisStation::getUpdateAt, startTime, endTime);
    }
}

