package com.cf.imes.module.system.controller.admin.systemconfig;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.system.controller.admin.systemconfig.vo.*;
import com.cf.imes.module.system.dal.dataobject.systemconfig.SystemConfigDO;
import com.cf.imes.module.system.service.systemconfig.SystemConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.UPDATE;

/**
 * 系统配置管理控制器
 *
 * @author Gqr
 * @since 2024/11/7 16:36
 */
@Tag(name = "管理后台 - 系统配置")
@RestController
@RequestMapping("/system/config")
@Validated
public class SystemConfigController {
    @Resource
    private SystemConfigService systemConfigService;

    @GetMapping()
    @Operation(summary = "获得系统配置的分页列表")
//    @PreAuthorize("@ss.hasAnyPermissions('system:base-setting:query','unplannedOrders:query','placeorder:query','manage:un-order-olan:create')")
    public CommonResult<List<ConfigRespVO>> getSystemConfigPage(@Valid ConfigPageReqVO pageReqVO) {
        List<SystemConfigDO> pageResult = systemConfigService.getSystemConfigList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigRespVO.class));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询系统配置")
//    @PreAuthorize("@ss.hasAnyPermissions('system:base-setting:query','unplannedOrders:query','placeorder:query')")
    public CommonResult<ConfigRespVO> getSystemConfig(@PathVariable("id") Long id,@RequestParam("type") Integer type) {
        SystemConfigDO systemConfigDO = systemConfigService.getSystemConfig(id,type);
        return success(BeanUtils.toBean(systemConfigDO, ConfigRespVO.class));
    }

    @PostMapping("/status")
    @Operation(summary = "修改系统配置状态")
    @OperateLog(type = UPDATE)
    @PreAuthorize("@ss.hasPermission('system:base-setting:update')")
    public CommonResult<Boolean> updateConfigStatus(@Valid @RequestBody ConfigUpdateStatusReqVO reqVO){
        systemConfigService.updateStatus(reqVO);
        return success(true);
    }

    @PostMapping("/sort")
    @Operation(summary = "修改系统配置的顺序")
    @OperateLog(type = UPDATE)
    @PreAuthorize("@ss.hasPermission('system:base-setting:update')")
    public CommonResult<Boolean> updateConfigSort(@Valid @RequestBody List<ConfigUpdateSortReqVO> reqVO){
        systemConfigService.updateConfigSort(reqVO);
        return success(true);
    }


    @PostMapping()
    @Operation(summary = "新增/修改系统配置")
    @PreAuthorize("@ss.hasAnyPermissions('system:base-setting:update','system:base-setting:save','system:base-setting:copy','system:base-setting:inset')")
    public CommonResult<Boolean> modifySystemConfig(@Valid @RequestBody ConfigSaveReqVO reqVO) {
        systemConfigService.modifySystemConfig(reqVO);
        return success(true);
    }

    @DeleteMapping("")
    @Operation(summary = "删除系统配置")
    @PreAuthorize("@ss.hasPermission('system:base-setting:delete')")
    public CommonResult<Boolean> deleteSystemConfig(@Valid @RequestBody ConfigDeleteReqVO reqVO) {
        systemConfigService.deleteSystemConfig(reqVO);
        return success(true);
    }


}
