package com.storlead.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.framework.common.result.Result;
import com.storlead.system.pojo.dto.SystemConfigItemDTO;
import com.storlead.system.pojo.entity.SystemConfigItemEntity;
import com.storlead.system.pojo.entity.SystemPersonalizeSettingEntity;
import com.storlead.system.service.SystemConfigItemService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 系统配置项 前端控制器
 * </p>
 *
 * @author chenkq
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/system/config")
public class SystemConfigItemController {

    @Resource
    private SystemConfigItemService configItemService;

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改配置项信息", notes = "配置系统基础设置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SystemPersonalizeSettingEntity.class)
    })
    public Result update(SystemConfigItemDTO dto) {
        SystemConfigItemEntity configItem = SystemConfigItemDTO.convertConfigItemEntity(dto);
        if (null == configItem) {
            return Result.error("参数错误");
        }
        configItemService.saveOrUpdate(configItem);
        return Result.ok();
    }

    @PostMapping(value = "/enabled")
    @ApiOperation(value = "修改配置项信息", notes = "配置系统基础设置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SystemPersonalizeSettingEntity.class)
    })
    public Result enabled(Long itemId,Boolean enable) {
        LambdaUpdateWrapper<SystemConfigItemEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SystemConfigItemEntity::getEnabled,enable);
        updateWrapper.eq(SystemConfigItemEntity::getItemId,itemId);
        configItemService.update(updateWrapper);
        return Result.ok();
    }
}
