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.EnvironmentService;
import com.gxwebsoft.common.system.entity.Environment;
import com.gxwebsoft.common.system.param.EnvironmentParam;
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.List;

/**
 * 环境管理控制器
 *
 * @author 科技小王子
 * @since 2023-10-18 08:45:13
 */
@Tag(name = "环境管理")
@RestController
@RequestMapping("/api/system/environment")
public class EnvironmentController extends BaseController {
    @Resource
    private EnvironmentService environmentService;

    @PreAuthorize("hasAuthority('sys:company:list')")
    @OperationLog
    @Operation(summary = "分页查询环境管理")
    @GetMapping("/page")
    public ApiResult<PageResult<Environment>> page(EnvironmentParam param) {
        PageParam<Environment, EnvironmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(environmentService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(environmentService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:company:list')")
    @OperationLog
    @Operation(summary = "查询全部环境管理")
    @GetMapping()
    public ApiResult<List<Environment>> list(EnvironmentParam param) {
        PageParam<Environment, EnvironmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(environmentService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(environmentService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:company:list')")
    @OperationLog
    @Operation(summary = "根据id查询环境管理")
    @GetMapping("/{id}")
    public ApiResult<Environment> get(@PathVariable("id") Integer id) {
        return success(environmentService.getById(id));
        // 使用关联查询
        //return success(environmentService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:company:save')")
    @OperationLog
    @Operation(summary = "添加环境管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Environment environment) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          environment.setUserId(loginUser.getUserId());
        }
        if (environmentService.save(environment)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:company:update')")
    @OperationLog
    @Operation(summary = "修改环境管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Environment environment) {
        if (environmentService.updateById(environment)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:company:remove')")
    @OperationLog
    @Operation(summary = "删除环境管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (environmentService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:company:save')")
    @OperationLog
    @Operation(summary = "批量添加环境管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Environment> list) {
        if (environmentService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:company:update')")
    @OperationLog
    @Operation(summary = "批量修改环境管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Environment> batchParam) {
        if (batchParam.update(environmentService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:company:remove')")
    @OperationLog
    @Operation(summary = "批量删除环境管理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (environmentService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
