package com.fs.company.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.CompanyConfig;
import com.fs.company.service.ICompanyConfigService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统配置
 *

 */
@RestController
@RequestMapping("/company/companyConfig")
public class CompanyConfigController extends BaseController
{

    @Autowired
    private ICompanyConfigService companyConfigService;

    @Autowired
    private TokenService tokenService;



    @PreAuthorize("@ss.hasPermi('company:config:edit')")
    @PutMapping
    public R edit(@Validated @RequestBody CompanyConfig companyConfig)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyConfig.setCompanyId(loginUser.getCompany().getCompanyId());
        companyConfigService.updateCompanyConfig(companyConfig);
        return R.ok("操作成功");
    }


    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public R getConfigKey(@PathVariable String configKey)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanyConfig config=companyConfigService.selectCompanyConfigByKey(loginUser.getCompany().getCompanyId(),configKey);
        if(config==null){
            config=new CompanyConfig();
            config.setCompanyId(loginUser.getCompany().getCompanyId());
            config.setConfigKey(configKey);
            config.setConfigType("N");
            companyConfigService.insertCompanyConfig(config);

        }
        return R.ok().put("data",config);

    }





}
