package com.gxwebsoft.cms.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.CmsAppSettingService;
import com.gxwebsoft.cms.entity.CmsAppSetting;
import com.gxwebsoft.cms.param.CmsAppSettingParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.BatchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 网站设置控制器
 *
 * @author 科技小王子
 * @since 2025-02-19 01:35:44
 */
@Tag(name = "网站设置管理")
@RestController
@RequestMapping("/api/cms/cms-app-setting")
public class CmsAppSettingController extends BaseController {
    @Resource
    private CmsAppSettingService cmsAppSettingService;

    @Operation(summary = "分页查询网站设置")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsAppSetting>> page(CmsAppSettingParam param) {
        // 使用关联查询
        return success(cmsAppSettingService.pageRel(param));
    }

    @Operation(summary = "查询全部网站设置")
    @GetMapping()
    public ApiResult<List<CmsAppSetting>> list(CmsAppSettingParam param) {
        // 使用关联查询
        return success(cmsAppSettingService.listRel(param));
    }

    @Operation(summary = "根据id查询网站设置")
    @GetMapping("/{id}")
    public ApiResult<CmsAppSetting> get(@PathVariable("id") Integer id) {
        // 使用关联查询
      final CmsAppSetting cmsAppSetting = cmsAppSettingService.getOne(new LambdaQueryWrapper<CmsAppSetting>().eq(CmsAppSetting::getAppId, id));
      if(ObjectUtil.isEmpty(cmsAppSetting)){
        final CmsAppSetting setting = new CmsAppSetting();
        setting.setAppId(id);
        cmsAppSettingService.save(setting);
        return success(cmsAppSettingService.getOne(new LambdaQueryWrapper<CmsAppSetting>().eq(CmsAppSetting::getAppId, id)));
      }
      return success(cmsAppSetting);
    }

    @PreAuthorize("hasAuthority('cms:cmsAppSetting:save')")
    @Operation(summary = "添加网站设置")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CmsAppSetting cmsAppSetting) {
        if (cmsAppSettingService.save(cmsAppSetting)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:website:update')")
    @Operation(summary = "修改网站设置")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CmsAppSetting cmsAppSetting) {
        if (cmsAppSettingService.updateById(cmsAppSetting)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsAppSetting:remove')")
    @Operation(summary = "删除网站设置")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsAppSettingService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsAppSetting:save')")
    @Operation(summary = "批量添加网站设置")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<CmsAppSetting> list) {
        if (cmsAppSettingService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsAppSetting:update')")
    @Operation(summary = "批量修改网站设置")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<CmsAppSetting> batchParam) {
        if (batchParam.update(cmsAppSettingService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsAppSetting:remove')")
    @Operation(summary = "批量删除网站设置")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (cmsAppSettingService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
