package com.xinelu.manage.controller.patientprehospitalization;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.patientprehospitalization.PatientPreHospitalization;
import com.xinelu.manage.dto.patientinfo.PatientInfoDto;
import com.xinelu.manage.service.patientprehospitalization.IPatientPreHospitalizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description: 预住院患者信息控制器
 * @author: haown
 * @create: 2024-03-08 10:40
 **/
@RestController
@RequestMapping("/manage/preHospital")
@Api(tags = "预住院患者信息控制器")
public class PatientPreHospitalizationController extends BaseController {

	@Resource
	private IPatientPreHospitalizationService preHospitalizationService;

	/**
	 * 新增预住院患者信息
	 */
	@ApiOperation("新增预住院患者信息")
	@PreAuthorize("@ss.hasPermi('manage:patientInfo:add')")
	@Log(title = "预住院患者信息", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody PatientPreHospitalization preHospitalization) {
		return toAjax(preHospitalizationService.insert(preHospitalization));
	}

	/**
	 * 查询预住院患者信息列表
	 */
	@ApiOperation("查询预住院患者信息列表")
	@PreAuthorize("@ss.hasPermi('manage:preHospitalized:list')")
	@GetMapping("/list")
	public TableDataInfo list(PatientInfoDto patientInfo) {
		startPage();
		List<PatientPreHospitalization> list = preHospitalizationService.selectList(patientInfo);
		return getDataTable(list);
	}

	/**
	 * 获取住院患者信息详细信息
	 */
	@PreAuthorize("@ss.hasPermi('manage:preHospitalized:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(preHospitalizationService.getById(id));
	}

	/**
	 * 修改预住院患者信息
	 */
	@PreAuthorize("@ss.hasPermi('manage:preHospitalized:edit')")
	@Log(title = "预住院患者", businessType = BusinessType.UPDATE)
	@PutMapping
	public R<String> edit(@RequestBody PatientPreHospitalization preHospitalization) {
		int flag = preHospitalizationService.update(preHospitalization);
		return flag < 0 ? R.fail() : R.ok();
	}

	/**
	 * 删除预住院患者信息
	 */
	@PreAuthorize("@ss.hasPermi('manage:preHospitalized:remove')")
	@Log(title = "患者信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(preHospitalizationService.deleteByIds(ids));
	}

	/**
	 * 导入预住院患者信息
	 *
	 * @param file 模板文件
	 * @return 导入结果
	 * @throws Exception 异常信息
	 */
	@ApiOperation("导入预住院患者信息")
	@Log(title = "导入预住院患者信息", businessType = BusinessType.IMPORT)
	@PostMapping("/importInfo")
	public AjaxResult importPatientList(MultipartFile file) throws Exception {
		// 判断excel里面是否有数据/文件格式
		if (Objects.isNull(file) || StringUtils.isBlank(file.getOriginalFilename())) {
			return AjaxResult.error("请选择需要导入的文件！");
		}
		// 获取文件名
		String orgName = file.getOriginalFilename();
		if (!orgName.endsWith(Constants.XLSX) && !orgName.endsWith(Constants.XLS)) {
			return AjaxResult.error("导入文件格式不正确，请导入xlsx或xls格式的文件！");
		}
		ExcelUtil<PatientPreHospitalization> util = new ExcelUtil<>(PatientPreHospitalization.class);
		List<PatientPreHospitalization> list = util.importExcel(file.getInputStream());
		return preHospitalizationService.importList(list);
	}
}
