package com.xinelu.manage.controller.patientvisitrecord;

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.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.patientvisitrecord.PatientVisitRecord;
import com.xinelu.manage.dto.patientvisitrecord.PatientVisitRecordDto;
import com.xinelu.manage.dto.patientvisitrecord.PatientVisitRecordImportDto;
import com.xinelu.manage.dto.patientvisitrecord.PatientVisitRecordInfoSaveDto;
import com.xinelu.manage.dto.patientvisitrecord.PatientVisitRecordSaveDto;
import com.xinelu.manage.service.patientvisitrecord.IPatientVisitRecordService;
import com.xinelu.manage.vo.patientvisitrecord.PatientVisitRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 患者就诊记录基本信息Controller
 *
 * @author haown
 * @date 2024-02-26
 */
@Api(tags = "患者就诊记录控制器")
@RestController
@RequestMapping("/manage/visitRecord")
public class PatientVisitRecordController extends BaseController {

    @Resource
    private IPatientVisitRecordService patientVisitRecordService;

	/**
	 * 查询患者就诊记录基本信息列表
	 */
	@ApiOperation("查询患者就诊记录基本信息列表")
	//@PreAuthorize("@ss.hasPermi('manage:visit:list')")
	@GetMapping("/getList")
	public AjaxResult getList(PatientVisitRecordDto patientVisitRecord) {
		List<PatientVisitRecord> list = patientVisitRecordService.selectPatientVisitRecordList(patientVisitRecord);
		return AjaxResult.success(list);
	}

    /**
     * 导出患者就诊记录基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:visit:export')")
    @Log(title = "患者就诊记录基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatientVisitRecordDto patientVisitRecord) {
        List<PatientVisitRecord> list = patientVisitRecordService.selectPatientVisitRecordList(patientVisitRecord);
        ExcelUtil<PatientVisitRecord> util = new ExcelUtil<PatientVisitRecord>(PatientVisitRecord. class);
        util.exportExcel(response, list, "患者就诊记录基本信息数据");
    }

    /**
     * 获取患者就诊记录基本信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('manage:visit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(patientVisitRecordService.selectPatientVisitRecordById(id));
    }

	/**
	 * 查询患者最新一次就诊记录信息
	 */
	//@PreAuthorize("@ss.hasPermi('manage:visit:query')")
	@GetMapping(value = "/getByPatientId/{patientId}")
	public R<PatientVisitRecordVo> getByPatientId(@PathVariable("patientId") Long patientId) {
		return R.ok(patientVisitRecordService.getByPatientId(patientId));
	}

    /**
     * 新增患者就诊记录基本信息
     */
    @ApiOperation("新增患者就诊记录基本信息")
    //@PreAuthorize("@ss.hasPermi('manage:visit:add')")
    @Log(title = "患者就诊记录基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@Valid @RequestBody PatientVisitRecordSaveDto patientVisitRecord) {
	    patientVisitRecordService.insertPatientVisitRecord(patientVisitRecord);
        return R.ok();
    }

	/**
	 * 修改患者就诊记录基本信息
	 */
	@ApiOperation("修改患者就诊记录基本信息")
	//@PreAuthorize("@ss.hasPermi('manage:visit:edit')")
	@Log(title = "患者就诊记录基本信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Valid @RequestBody PatientVisitRecord patientVisitRecord) {
		return toAjax(patientVisitRecordService.updatePatientVisitRecord(patientVisitRecord));
	}

	/**
	 * 患者配置——就诊信息保存
	 */
	@ApiOperation("患者配置——就诊信息保存")
	//@PreAuthorize("@ss.hasPermi('manage:visit:add')")
	@Log(title = "患者就诊记录基本信息", businessType = BusinessType.INSERT)
	@PostMapping("saveRecord")
	public R<String> saveRecord(@Valid @RequestBody PatientVisitRecordInfoSaveDto saveDto) {
		patientVisitRecordService.saveRecord(saveDto);
		return R.ok();
	}


	/**
     * 修改患者就诊记录基本信息
     */
	@ApiOperation("患者配置——就诊信息修改")
    //@PreAuthorize("@ss.hasPermi('manage:visit:edit')")
    @Log(title = "患者就诊记录基本信息", businessType = BusinessType.UPDATE)
    @PutMapping("updateRecord")
    public AjaxResult updateRecord(@Valid @RequestBody PatientVisitRecordInfoSaveDto patientVisitRecord) {
        return toAjax(patientVisitRecordService.updateRecord(patientVisitRecord));
    }

    /**
     * 删除患者就诊记录基本信息
     */
    //@PreAuthorize("@ss.hasPermi('manage:visit:remove')")
    @Log(title = "患者就诊记录基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(patientVisitRecordService.deletePatientVisitRecordByIds(ids));
    }

	/**
	 * 导入患者信息
	 *
	 * @param file 模板文件
	 * @return 导入结果
	 * @throws Exception 异常信息
	 */
	@ApiOperation(value = "导入患者就诊记录", notes = "patientType: outpatient:门诊患者,inhospital:在院患者，discharged：出院患者")
	@Log(title = "导入患者就诊信息", businessType = BusinessType.IMPORT)
	@PostMapping("/importPatientInfo")
	public AjaxResult importPatientList(MultipartFile file, @RequestParam(value = "patientType") String patientType) 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<PatientVisitRecordImportDto> util = new ExcelUtil<>(PatientVisitRecordImportDto.class);
		List<PatientVisitRecordImportDto> list = util.importExcel(file.getInputStream());
		return patientVisitRecordService.importPatientList(list, patientType);
	}
}
