package com.xinelu.manage.controller.hospitalinfo;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.hospitalinfo.HospitalInfo;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import com.xinelu.manage.service.hospitalinfo.IHospitalInfoService;
import com.xinelu.manage.service.hospitalpersoninfo.IHospitalPersonInfoService;
import com.xinelu.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

;

/**
 * 医院信息管理Controller
 *
 * @author xinyilu
 * @date 2023-02-13
 */
@Api(tags = "医院信息管理控制器")
@RestController
@RequestMapping("/system/hospital")
public class HospitalInfoController extends BaseController {
    @Resource
    private IHospitalInfoService hospitalInfoService;
	@Resource
	private IHospitalPersonInfoService hospitalPersonInfoService;
	@Resource
	private ISysRoleService roleService;

    /**
     * 查询医院信息管理列表
     */
    @ApiOperation("查询医院信息管理分页列表")
    @PreAuthorize("@ss.hasPermi('system:hospital:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalInfo hospitalInfo) {
	    if (!getLoginUser().getUser().isAdmin()) {
		    HospitalPersonInfo hospitalPersonInfo = hospitalPersonInfoService.selectHospitalPersonInfoById(getLoginUser().getUser().getHospitalPersonId());
		    if (hospitalPersonInfo != null) {
			    hospitalInfo.setId(hospitalPersonInfo.getHospitalId());
		    }
	    }
        startPage();
        List<HospitalInfo> list = hospitalInfoService.selectHospitalInfoList(hospitalInfo);
        return getDataTable(list);
    }

	/**
	 * 查询医院信息管理列表
	 */
	@ApiOperation("查询医院信息管理列表")
	@GetMapping("/getList")
	public AjaxResult getList(HospitalInfo hospitalInfo) {
		if (!getLoginUser().getUser().isAdmin()) {
			HospitalPersonInfo hospitalPersonInfo = hospitalPersonInfoService.selectHospitalPersonInfoById(getLoginUser().getUser().getHospitalPersonId());
			if (hospitalPersonInfo != null) {
				hospitalInfo.setId(hospitalPersonInfo.getHospitalId());
			}
		}
		List<HospitalInfo> list = hospitalInfoService.selectHospitalInfoList(hospitalInfo);
		return AjaxResult.success(list);
	}

    /**
     * 导出医院信息管理列表
     */
    @ApiOperation("导出医院信息管理列表")
    @PreAuthorize("@ss.hasPermi('system:hospital:export')")
    @Log(title = "医院信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HospitalInfo hospitalInfo) {
        List<HospitalInfo> list = hospitalInfoService.selectHospitalInfoList(hospitalInfo);
        ExcelUtil<HospitalInfo> util = new ExcelUtil<>(HospitalInfo.class);
        util.exportExcel(response, list, "医院信息管理数据");
    }

    /**
     * 获取医院信息管理详细信息
     */
    @ApiOperation("获取医院信息管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:hospital:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(hospitalInfoService.selectHospitalInfoById(id));
    }

    /**
     * 新增医院信息管理
     */
    @ApiOperation("新增医院信息管理")
    @PreAuthorize("@ss.hasPermi('system:hospital:add')")
    @Log(title = "医院信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated(Insert.class) HospitalInfo hospitalInfo) {
        return hospitalInfoService.insertHospitalInfo(hospitalInfo);
    }

    /**
     * 修改医院信息管理
     */
    @ApiOperation("修改医院信息管理")
    @PreAuthorize("@ss.hasPermi('system:hospital:edit')")
    @Log(title = "医院信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Validated(Update.class) HospitalInfo hospitalInfo) {
        return hospitalInfoService.updateHospitalInfo(hospitalInfo);
    }

    /**
     * 删除医院信息管理
     */
    @ApiOperation("修改医院信息管理")
    @PreAuthorize("@ss.hasPermi('system:hospital:remove')")
    @Log(title = "医院信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return hospitalInfoService.deleteHospitalInfoByIds(ids);
    }
}
