package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.WebsiteFieldService;
import com.gxwebsoft.common.system.entity.WebsiteField;
import com.gxwebsoft.common.system.param.WebsiteFieldParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
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.HashMap;
import java.util.List;

/**
 * 应用参数控制器
 *
 * @author 科技小王子
 * @since 2024-08-27 15:18:05
 */
@Tag(name = "应用参数管理")
@RestController
@RequestMapping("/api/system/website-field")
public class WebsiteFieldController extends BaseController {
    @Resource
    private WebsiteFieldService websiteFieldService;

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

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

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

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

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

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

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

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

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

    @Operation(summary = "获取网站配置参数-对象形式")
    @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);
    }

}
