package com.fs.hospital.controller.hospital;

import java.util.List;

import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.system.domain.SysConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.hospital.domain.HospitalConfig;
import com.fs.hospital.service.IHospitalConfigService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 参数配置Controller
 *
 * @author fs
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/hospital/hospitalConfig")
public class HospitalConfigController extends BaseController
{
    @Autowired
    private IHospitalConfigService hospitalConfigService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalConfig hospitalConfig)
    {
        startPage();
        List<HospitalConfig> list = hospitalConfigService.selectHospitalConfigList(hospitalConfig);
        return getDataTable(list);
    }

    /**
     * 导出参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalConfig:export')")
    @Log(title = "参数配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalConfig hospitalConfig)
    {
        List<HospitalConfig> list = hospitalConfigService.selectHospitalConfigList(hospitalConfig);
        ExcelUtil<HospitalConfig> util = new ExcelUtil<HospitalConfig>(HospitalConfig.class);
        return util.exportExcel(list, "参数配置数据");
    }

    /**
     * 获取参数配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalConfig:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return AjaxResult.success(hospitalConfigService.selectHospitalConfigByConfigId(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(hospitalConfigService.selectHospitalConfigByKey(configKey,loginUser.getHospital().getHospitalId()));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalConfig:add')")
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HospitalConfig hospitalConfig)
    {
        return toAjax(hospitalConfigService.insertHospitalConfig(hospitalConfig));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalConfig:edit')")
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalConfig hospitalConfig)
    {
        return toAjax(hospitalConfigService.updateHospitalConfig(hospitalConfig));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalConfig:remove')")
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(hospitalConfigService.deleteHospitalConfigByConfigIds(configIds));
    }

    @PostMapping(value = "/updateHospitalConfig")
    @RepeatSubmit
    public AjaxResult updateConfigByKey(@Validated @RequestBody HospitalConfig hospitalConfig)
    {
        hospitalConfig.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(hospitalConfigService.updateHospitalConfig(hospitalConfig));
    }
}
