package com.storlead.system.controller;

import com.storlead.framework.common.result.Result;
import com.storlead.framework.redis.RedisService;
import com.storlead.system.constant.ReidsCacheConstant;
import com.storlead.system.pojo.dto.CompanyCustomSettingsEntityDTO;
import com.storlead.system.pojo.entity.CompanyCustomSettingsEntity;
import com.storlead.system.pojo.entity.SystemPersonalizeSettingEntity;
import com.storlead.system.service.CompanyCustomSettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 公司配置信息 前端控制器
 * </p>
 *
 * @author chenkq
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/sys/custom/settings")
@Api(tags = "平台中心 - 系统: 企业个性化设置")
public class CompanyCustomSettingsController {


    @Resource
    private CompanyCustomSettingsService customSettingsService;

    @Resource
    private RedisService redisService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "配置系统个性设置", notes = "配置系统个性设置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SystemPersonalizeSettingEntity.class)
    })
    public Result saveSetting(CompanyCustomSettingsEntityDTO entity) {
        CompanyCustomSettingsEntity setting = customSettingsService.getOne(null);
        if (Objects.nonNull(setting)) {
            entity.setSettingsId(setting.getSettingsId());
        }
        redisService.setCacheObject(ReidsCacheConstant.SYS_CACHE_ORG_SYNC_MODE,setting.getSyncMode());
        customSettingsService.saveOrUpdate((CompanyCustomSettingsEntity) entity);
        return Result.ok();
    }

    @PostMapping(value = "/get")
    @ApiOperation(value = "获取系统个性设置", notes = "获取系统个性设置")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = CompanyCustomSettingsEntityDTO.class)
    })
    public Result<?> getSetting() {
        CompanyCustomSettingsEntity setting = customSettingsService.getOne(null);
        return Result.result(setting);
    }
}
