package com.ltgk.message.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.message.domain.entity.PushAppLog;
import com.ltgk.message.service.IPushAppLogService;
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;

/**
 * APP推送日志 (PushAppLog) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-05-19
 */
@RestController
@RequestMapping("/fishingPort/pushAppLog")
@Api(tags = "APP推送日志")
public class PushAppLogController extends BaseController {

    @Resource
    private IPushAppLogService pushAppLogService;

    /**
     * 分页查询APP推送日志列表
     *
     * @param req: APP推送日志 实体对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-05-19
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询APP推送日志列表")
    public TableDataInfo page(@RequestBody PushAppLog req) {
        LambdaQueryWrapper<PushAppLog> wrapper = getLambdaQueryWrapperByReq(req);
        wrapper.orderByDesc(PushAppLog::getNoticeContent);
        startPage(req.getPageNum(), req.getPageSize());
        List<PushAppLog> list = pushAppLogService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 获取APP推送日志详细数据
     *
     * @param req: APP推送日志 实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-19
     */
    @PostMapping(value = "/detail")
    @ApiOperation(value = "获取APP推送日志详细数据")
    public AjaxResult detail(@Validated(Detail.class) @RequestBody PushAppLog req) {
        return AjaxResult.success(pushAppLogService.getById(req.getId()));
    }

    /**
     * 新增APP推送日志数据
     * 接口权限: fishingPort:pushAppLog:add
     *
     * @param pushAppLog: APP推送日志实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-19
     */
    @Log(title = "新增APP推送日志数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增APP推送日志数据")
    public AjaxResult add(@Validated(Add.class) @RequestBody PushAppLog pushAppLog) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        pushAppLog.setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(pushAppLogService.save(pushAppLog));
    }

    /**
     * 修改APP推送日志数据
     * 接口权限: fishingPort:pushAppLog:edit
     *
     * @param pushAppLog: APP推送日志实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-19
     */
    @Log(title = "修改APP推送日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改APP推送日志数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody PushAppLog pushAppLog) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        pushAppLog.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(pushAppLogService.updateById(pushAppLog));
    }

    /**
     * 删除APP推送日志数据
     * 接口权限: fishingPort:pushAppLog:remove
     *
     * @param req: APP推送日志 实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-05-19
     */
    @Log(title = "删除APP推送日志数据", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除APP推送日志数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody PushAppLog req) {
        return toAjax(pushAppLogService.removeById(req.getId()));
    }

    /**
     * 导出APP推送日志数据
     * 接口权限: fishingPort:pushAppLog:export
     *
     * @param response:
     * @param req:      APP推送日志 实体对象
     * @author Qi ChengBin
     * @date 2025-05-19
     */
    @Log(title = "导出APP推送日志数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出APP推送日志数据")
    public void export(HttpServletResponse response, PushAppLog req) {
        LambdaQueryWrapper<PushAppLog> wrapper = getLambdaQueryWrapperByReq(req);
        List<PushAppLog> list = pushAppLogService.list(wrapper);
        ExcelUtil<PushAppLog> util = new ExcelUtil<>(PushAppLog.class);
        util.exportExcel(response, list, "APP推送日志数据");
    }

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

