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.DsBroadcast;
import com.ltgk.video.service.IDsBroadcastService;
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;

/**
 * 广播设备 (DsBroadcast) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-05-06
 */
@RestController
@RequestMapping("/fishingPort/dsBroadcast")
@Api(tags = "广播设备")
public class DsBroadcastController extends BaseController {

    @Resource
    private IDsBroadcastService dsBroadcastService;

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

    /**
     * 获取广播设备详细数据
     *
     * @param req: 广播设备 实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-06
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取广播设备详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody DsBroadcast req) {
        return AjaxResult.success(dsBroadcastService.getById(req.getId()));
    }

    /**
     * 新增广播设备数据
     * 接口权限: fishingPort:dsBroadcast:add
     *
     * @param dsBroadcast: 广播设备实体对象
     * @return com.ltgk.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 DsBroadcast dsBroadcast) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsBroadcast.setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsBroadcastService.save(dsBroadcast));
    }

    /**
     * 修改广播设备数据
     * 接口权限: fishingPort:dsBroadcast:edit
     *
     * @param dsBroadcast: 广播设备实体对象
     * @return com.ltgk.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 DsBroadcast dsBroadcast) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        dsBroadcast.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(dsBroadcastService.updateById(dsBroadcast));
    }

    /**
     * 删除广播设备数据
     * 接口权限: fishingPort:dsBroadcast:remove
     *
     * @param req: 广播设备 实体对象
     * @return com.ltgk.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 DsBroadcast req) {
        return toAjax(dsBroadcastService.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, DsBroadcast req) {
        LambdaQueryWrapper<DsBroadcast> wrapper = getLambdaQueryWrapperByReq(req);
        List<DsBroadcast> list = dsBroadcastService.list(wrapper);
        ExcelUtil<DsBroadcast> util = new ExcelUtil<>(DsBroadcast.class);
        util.exportExcel(response, list, "广播设备数据");
    }

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

