package com.gxwebsoft.common.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.utils.CacheClient;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.Setting;
import com.gxwebsoft.common.system.param.SettingParam;
import com.gxwebsoft.common.system.service.SettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

import static com.gxwebsoft.common.core.constants.RedisConstants.TEN_ANT_SETTING_KEY;

/**
 * 系统设置控制器
 *
 * @author WebSoft
 * @since 2022-11-19 13:54:27
 */
@Api(tags = "系统设置管理")
@RestController
@RequestMapping("/api/system/setting")
public class SettingController extends BaseController {
    @Resource
    private SettingService settingService;
    @Resource
    private CacheClient cacheClient;

    @PreAuthorize("hasAuthority('sys:setting:save')")
    @OperationLog
    @ApiOperation("分页查询系统设置")
    @GetMapping("/page")
    public ApiResult<PageResult<Setting>> page(SettingParam param) {
        PageParam<Setting, SettingParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(settingService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(settingService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:setting:save')")
    @OperationLog
    @ApiOperation("查询全部系统设置")
    @GetMapping()
    public ApiResult<List<Setting>> list(SettingParam param) {
        PageParam<Setting, SettingParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(settingService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(settingService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:setting:save')")
    @OperationLog
    @ApiOperation("根据id查询系统设置")
    @GetMapping("/{id}")
    public ApiResult<Setting> get(@PathVariable("id") Integer id) {
        return success(settingService.getById(id));
        // 使用关联查询
        //return success(settingService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:setting:save')")
    @OperationLog
    @ApiOperation("添加系统设置")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Setting setting) {
        if (settingService.save(setting)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:setting:save')")
    @OperationLog
    @ApiOperation("修改系统设置")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Setting setting) {
        if (settingService.updateById(setting)) {
          // 更新系统设置信息到缓存
          cacheClient.set(TEN_ANT_SETTING_KEY.concat(":").concat(setting.getSettingKey()), JSON.parseObject(setting.getContent()));
          // 更新租户信息
          if (setting.getSettingKey().equals("setting")) {
            System.out.println("修改系统设置 = " + setting.getContent());
            final String content = setting.getContent();
            final JSONObject jsonObject = JSONObject.parseObject(content);
            final String siteName = jsonObject.getString("siteName");
            final String logo = jsonObject.getString("logo");
            System.out.println("siteName = " + siteName);
          }
          return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:setting:remove')")
    @OperationLog
    @ApiOperation("删除系统设置")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (settingService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:setting:save')")
    @OperationLog
    @ApiOperation("批量添加系统设置")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Setting> list) {
        if (settingService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:setting:update')")
    @OperationLog
    @ApiOperation("批量修改系统设置")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Setting> batchParam) {
        if (batchParam.update(settingService, "setting_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:setting:remove')")
    @OperationLog
    @ApiOperation("批量删除系统设置")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (settingService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:setting:data')")
    @OperationLog
    @ApiOperation("查询租户设置信息")
    @GetMapping("/data")
    public ApiResult<?> data() {
      return success(settingService.getData("setting"));
    }

    @PreAuthorize("hasAuthority('sys:setting:save')")
    @OperationLog
    @ApiOperation("更新主题皮肤")
    @PutMapping("/theme")
    public ApiResult<?> theme(@RequestBody Setting setting) {
      // 新增
      final Setting one = settingService.getOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSettingKey, setting.getSettingKey()));
      if(one == null){
        settingService.save(setting);
        cacheClient.set("theme",setting.getContent());
        return success("保存成功");
      }
      // 更新
      final Setting update = settingService.getOne(new LambdaQueryWrapper<Setting>().eq(Setting::getSettingKey, setting.getSettingKey()));
      update.setContent(setting.getContent());
      if (settingService.updateById(update)) {
        cacheClient.set("theme",setting.getContent());
        return success("更新成功");
      }
      return fail("更新失败");
    }

    @ApiOperation("更新主题皮肤")
    @GetMapping("/getTheme")
    public ApiResult<?> getTheme() {
      return success("获取成功",cacheClient.get("theme"));
    }

}
