package com.ltgk.web.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.common.annotation.Anonymous;
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.validation.groups.Add;
import com.ltgk.common.validation.groups.Detail;
import com.ltgk.common.validation.groups.Update;
import com.ltgk.system.domain.SysAppConfig;
import com.ltgk.system.service.ISysAppConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
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 java.util.Date;
import java.util.List;

/**
 * APP配置表 (SysAppConfig) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/system/sysAppConfig")
@Api(tags = "APP配置表")
public class SysAppConfigController extends BaseController {

    @Resource
    private ISysAppConfigService sysAppConfigService;

    /**
     * 获取最后一条相同系统下相同客户端下的最后一条数据
     * 接口权限: system:sysAppConfig:getLastAppConfig
     *
     * @param req:
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025/3/19
     */
    @Anonymous
    @PostMapping("/getLastAppConfig")
    @ApiOperation(value = "查询相同系统下相同客户端最后一条数据")
    public AjaxResult getLastAppConfig(@RequestBody SysAppConfig req) {
        SysAppConfig appConfig = sysAppConfigService.lambdaQuery()
                .eq(SysAppConfig::getAppOs, req.getAppOs())
                .eq(SysAppConfig::getAppType, req.getAppType())
                .orderByDesc(SysAppConfig::getAppVersion)
                .last("limit 1")
                .one();
        return AjaxResult.success(appConfig);
    }

    /**
     * 分页查询APP配置表列表
     * 接口权限: system:sysAppConfig:page
     *
     * @param req: APP配置表 实体对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-03-19
     */
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermi('system:sysAppConfig:page')")
    @ApiOperation(value = "分页查询APP配置表列表")
    public TableDataInfo page(@RequestBody SysAppConfig req) {
        LambdaQueryWrapper<SysAppConfig> wrapper = getLambdaQueryWrapperByReq(req);
        startPage(req.getPageNum(), req.getPageSize());
        List<SysAppConfig> list = sysAppConfigService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 新增APP配置表数据
     * 接口权限: system:sysAppConfig:add
     *
     * @param sysAppConfig: APP配置表实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-03-19
     */
    @PreAuthorize("@ss.hasPermi('system:sysAppConfig:add')")
    @Log(title = "新增APP配置表数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增APP配置表数据")
    public AjaxResult add(@Validated(Add.class) @RequestBody SysAppConfig sysAppConfig) {
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        Date nowDate = new Date();
        sysAppConfig
                .setCreateBy(nickName)
                .setCreateAt(nowDate)
                .setUpdateBy(nickName)
                .setUpdateAt(nowDate);
        return toAjax(sysAppConfigService.save(sysAppConfig));
    }

    /**
     * 修改APP配置表数据
     * 接口权限: system:sysAppConfig:edit
     *
     * @param sysAppConfig: APP配置表实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-03-19
     */
    @PreAuthorize("@ss.hasPermi('system:sysAppConfig:edit')")
    @Log(title = "修改APP配置表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改APP配置表数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody SysAppConfig sysAppConfig) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        sysAppConfig.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(sysAppConfigService.updateById(sysAppConfig));
    }

    /**
     * 删除APP配置表数据
     * 接口权限: system:sysAppConfig:remove
     *
     * @param req: APP配置表 实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-03-19
     */
    @PreAuthorize("@ss.hasPermi('system:sysAppConfig:remove')")
    @Log(title = "删除APP配置表数据", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除APP配置表数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody SysAppConfig req) {
        return toAjax(sysAppConfigService.removeById(req.getId()));
    }

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

