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.DsUav;
import com.ltgk.video.service.IDsUavService;
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;

/**
 * 无人机 (DsUav) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-05-06
 */
@RestController
@RequestMapping("/fishingPort/dsUav")
@Api(tags = "无人机")
public class DsUavController extends BaseController {

    @Resource
    private IDsUavService dsUavService;

    /**
     * 分页查询无人机列表
     *
     * @param req: 无人机 实体对象
     * @return com.yuhuan.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询无人机列表")
    public TableDataInfo page(@RequestBody DsUav req) {
        LambdaQueryWrapper<DsUav> wrapper = getLambdaQueryWrapperByReq(req);
        startPage(req.getPageNum(), req.getPageSize());
        List<DsUav> list = dsUavService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 获取无人机详细数据
     *
     * @param req: 无人机 实体对象
     * @return com.yuhuan.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取无人机详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody DsUav req) {
        return AjaxResult.success(dsUavService.getById(req.getId()));
    }

    /**
     * 新增无人机数据
     * 接口权限: fishingPort:dsUav:add
     *
     * @param dsUav: 无人机实体对象
     * @return com.yuhuan.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    @Log(title = "新增无人机数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增无人机数据")
    public AjaxResult add(@Validated(Add.class) @RequestBody DsUav dsUav) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsUav.setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsUavService.save(dsUav));
    }

    /**
     * 修改无人机数据
     * 接口权限: fishingPort:dsUav:edit
     *
     * @param dsUav: 无人机实体对象
     * @return com.yuhuan.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    @Log(title = "修改无人机", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改无人机数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody DsUav dsUav) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsUav.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsUavService.updateById(dsUav));
    }

    /**
     * 删除无人机数据
     * 接口权限: fishingPort:dsUav:remove
     *
     * @param req: 无人机 实体对象
     * @return com.yuhuan.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    @Log(title = "删除无人机数据", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除无人机数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody DsUav req) {
        return toAjax(dsUavService.removeById(req.getId()));
    }

    /**
     * 导出无人机数据
     *
     * @param response:
     * @param req:      无人机 实体对象
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    @Log(title = "导出无人机数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出无人机数据")
    public void export(HttpServletResponse response, DsUav req) {
        LambdaQueryWrapper<DsUav> wrapper = getLambdaQueryWrapperByReq(req);
        List<DsUav> list = dsUavService.list(wrapper);
        ExcelUtil<DsUav> util = new ExcelUtil<>(DsUav.class);
        util.exportExcel(response, list, "无人机数据");
    }

    /**
     * 根据请求参数转换成 LambdaQueryWrapper
     *
     * @param req: 无人机 实体对象
     * @return com.yuhuan.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    private static LambdaQueryWrapper<DsUav> getLambdaQueryWrapperByReq(DsUav 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, DsUav::getUpdateAt, startTime, endTime);
    }
}

