package com.gxwebsoft.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.WebsiteField;
import com.gxwebsoft.cms.mapper.WebsiteFieldMapper;
import com.gxwebsoft.cms.param.WebsiteFieldParam;
import com.gxwebsoft.cms.service.WebsiteFieldService;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
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.HashMap;
import java.util.List;

/**
 * 应用参数控制器
 *
 * @author 科技小王子
 * @since 2024-03-05 15:53:44
 */
@Api(tags = "网站参数")
@RestController
@RequestMapping("/api/cms/website-field")
public class WebsiteFieldController extends BaseController {
    @Resource
    private WebsiteFieldService websiteFieldService;
    @Resource
    private WebsiteFieldMapper websiteFieldMapper;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("分页查询应用参数")
    @GetMapping("/page")
    public ApiResult<PageResult<WebsiteField>> page(WebsiteFieldParam param) {
        // 使用关联查询
        return success(websiteFieldService.pageRel(param));
    }

    @ApiOperation("查询全部应用参数")
    @GetMapping()
    public ApiResult<List<WebsiteField>> list(WebsiteFieldParam param) {
        // 使用关联查询
        return success(websiteFieldService.listRel(param));
    }

    @ApiOperation("根据id查询应用参数")
    @GetMapping("/{id}")
    public ApiResult<WebsiteField> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(websiteFieldService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:websiteField:save')")
    @ApiOperation("添加应用参数")
    @PostMapping()
    public ApiResult<?> save(@RequestBody WebsiteField websiteField) {
        if (websiteFieldService.save(websiteField)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:websiteField:update')")
    @ApiOperation("修改应用参数")
    @PutMapping()
    public ApiResult<?> update(@RequestBody WebsiteField websiteField) {
        if (websiteFieldService.updateById(websiteField)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:websiteField:remvoe')")
    @ApiOperation("删除应用参数")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (websiteFieldService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:websiteField:save')")
    @ApiOperation("批量添加应用参数")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<WebsiteField> list) {
        if (websiteFieldService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:websiteField:update')")
    @ApiOperation("批量修改应用参数")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<WebsiteField> batchParam) {
        if (batchParam.update(websiteFieldService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:websiteField:remvoe')")
    @ApiOperation("批量删除应用参数")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (websiteFieldService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:websiteField:update')")
    @ApiOperation("从回收站放回原处")
    @DeleteMapping("/undelete/{id}")
    public ApiResult<?> undelete(@PathVariable("id") Integer id) {
      if (websiteFieldMapper.undelete(id) > 0) {
        return success("恢复成功");
      }
      return fail("恢复失败");
    }

    @ApiOperation("获取网站配置参数-对象形式")
    @GetMapping("/config")
    public ApiResult<?> getConfig(WebsiteFieldParam param) {
      // 使用关联查询
      final List<WebsiteField> fields = websiteFieldService.listRel(param);

      HashMap<String, Object> config = new HashMap<>();
      fields.forEach(d -> {
        config.put(d.getName(), d.getValue());
      });
      System.out.println("config = " + config);
      return success(config);
    }
}
