package com.fs.hospital.controller.hospital;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.hospital.vo.HospitalMemberLevelHVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.HospitalMemberLevel;
import com.fs.hospital.service.IHospitalMemberLevelService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 会员等级Controller
 *
 * @author fs
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/hospital/hospitalMemberLevel")
public class HospitalMemberLevelController extends BaseController
{
    @Autowired
    private IHospitalMemberLevelService hospitalMemberLevelService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询会员等级列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberLevel:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalMemberLevel hospitalMemberLevel)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalMemberLevel.setHospitalId(loginUser.getHospital().getHospitalId());
        List<HospitalMemberLevelHVO> list = hospitalMemberLevelService.selectHospitalMemberLevelListPVO(hospitalMemberLevel);
        return getDataTable(list);
    }

    /**
     * 导出会员等级列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberLevel:export')")
    @Log(title = "会员等级", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalMemberLevel hospitalMemberLevel)
    {
        List<HospitalMemberLevel> list = hospitalMemberLevelService.selectHospitalMemberLevelList(hospitalMemberLevel);
        ExcelUtil<HospitalMemberLevel> util = new ExcelUtil<HospitalMemberLevel>(HospitalMemberLevel.class);
        return util.exportExcel(list, "会员等级数据");
    }

    /**
     * 获取会员等级详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberLevel:query')")
    @GetMapping(value = "/{levelId}")
    public AjaxResult getInfo(@PathVariable("levelId") Long levelId)
    {
        return AjaxResult.success(hospitalMemberLevelService.selectHospitalMemberLevelByLevelId(levelId));
    }

    /**
     * 新增会员等级
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberLevel:add')")
    @Log(title = "会员等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HospitalMemberLevel hospitalMemberLevel)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalMemberLevel.setHospitalId(loginUser.getHospital().getHospitalId());
        return toAjax(hospitalMemberLevelService.insertHospitalMemberLevel(hospitalMemberLevel));
    }

    /**
     * 修改会员等级
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberLevel:edit')")
    @Log(title = "会员等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalMemberLevel hospitalMemberLevel)
    {

        return toAjax(hospitalMemberLevelService.updateHospitalMemberLevel(hospitalMemberLevel));
    }

    /**
     * 删除会员等级
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalMemberLevel:remove')")
    @Log(title = "会员等级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{levelIds}")
    public AjaxResult remove(@PathVariable Long[] levelIds)
    {
        return toAjax(hospitalMemberLevelService.deleteHospitalMemberLevelByLevelIds(levelIds));
    }

    /**
     * 查询所有会员等级
     */
    @GetMapping("/allLevel")
    public R allLevel()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<HospitalMemberLevel> list = hospitalMemberLevelService.selectAllLevel(loginUser.getHospital().getHospitalId());
        return R.ok().put("data",list);
    }


}
