package com.fs.hospital.controller.hospital;

import java.util.List;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
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.HospitalExamItem;
import com.fs.hospital.service.IHospitalExamItemService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 检验项目Controller
 *
 * @author fs
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/hospital/hospitalExamItem")
public class HospitalExamItemController extends BaseController
{
    @Autowired
    private IHospitalExamItemService hospitalExamItemService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询检验项目列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalExamItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalExamItem hospitalExamItem)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalExamItem.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalExamItem> list = hospitalExamItemService.selectHospitalExamItemList(hospitalExamItem);
        return getDataTable(list);
    }

    /**
     * 导出检验项目列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalExamItem:export')")
    @Log(title = "检验项目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalExamItem hospitalExamItem)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalExamItem.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalExamItem> list = hospitalExamItemService.selectHospitalExamItemList(hospitalExamItem);
        ExcelUtil<HospitalExamItem> util = new ExcelUtil<HospitalExamItem>(HospitalExamItem.class);
        return util.exportExcel(list, "检验项目数据");
    }

    /**
     * 获取检验项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalExamItem:query')")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return AjaxResult.success(hospitalExamItemService.selectHospitalExamItemByExamId(examId));
    }

    /**
     * 新增检验项目
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalExamItem:add')")
    @Log(title = "检验项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HospitalExamItem hospitalExamItem)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalExamItem.setHospitalId( loginUser.getHospital().getHospitalId());
        return toAjax(hospitalExamItemService.insertHospitalExamItem(hospitalExamItem));
    }

    /**
     * 修改检验项目
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalExamItem:edit')")
    @Log(title = "检验项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalExamItem hospitalExamItem)
    {
        return toAjax(hospitalExamItemService.updateHospitalExamItem(hospitalExamItem));
    }

    /**
     * 删除检验项目
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalExamItem:remove')")
    @Log(title = "检验项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds)
    {
        return toAjax(hospitalExamItemService.deleteHospitalExamItemByExamIds(examIds));
    }
}
