package com.xinelu.manage.controller.patientinfo;

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.patientinfo.PatientInfo;
import com.xinelu.manage.domain.patientinfoimport.PatientInfoImport;
import com.xinelu.manage.dto.patientinfo.PatientBaseInfoDto;
import com.xinelu.manage.dto.patientinfo.PatientInfoDto;
import com.xinelu.manage.service.patientinfo.IPatientInfoService;
import com.xinelu.manage.vo.patientinfo.PatientInfoVo;
import com.xinelu.manage.vo.patientinfoimport.PatientInfoImportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 患者信息Controller
 *
 * @author haown
 * @date 2024-02-26
 */
@RestController
@RequestMapping("/manage/patientInfo")
@Api(tags = "患者信息控制器")
public class PatientInfoController extends BaseController {
    @Resource
    private IPatientInfoService patientInfoService;

	/**
	 * 查询患者信息列表
	 */
	@ApiOperation("查询患者信息列表")
	@PreAuthorize("@ss.hasPermi('manage:patientInfo:list')")
	@GetMapping("/list")
    public TableDataInfo list(PatientInfoDto patientInfo) {
        startPage();
        List<PatientInfo> list = patientInfoService.selectPatientInfoList(patientInfo);
        return getDataTable(list);
    }

	/**
	 * @description 患者管理-患者档案
	 * @param patientInfo 患者档案查询传输对象
	 * @return 患者信息列表
	 * @Author haown
	 * @Date 2024-08-05 08:45
	*/
	@ApiOperation("患者管理-患者档案")
	@PreAuthorize("@ss.hasPermi('manage:patientInfo:list')")
	@GetMapping("/getPatientList")
	public TableDataInfo getPatientList(PatientInfoDto patientInfo) {
		startPage();
		List<PatientInfoVo> list = patientInfoService.getPatientList(patientInfo);
		return getDataTable(list);
	}

    /**
     * 导出患者信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:patientInfo:export')")
    @Log(title = "患者信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatientInfoDto patientInfo) {
        List<PatientInfo> list = patientInfoService.selectPatientInfoList(patientInfo);
        ExcelUtil<PatientInfo> util = new ExcelUtil<PatientInfo>(PatientInfo. class);
        util.exportExcel(response, list, "患者信息数据");
    }

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

    /**
     * 新增患者信息
     */
    @PreAuthorize("@ss.hasPermi('manage:patientInfo:add')")
    @Log(title = "患者信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<PatientInfo> add(@RequestBody PatientInfo patientInfo) {
        return R.ok(patientInfoService.insertPatientInfo(patientInfo));
    }

    /**
     * 修改患者信息
     */
    @ApiOperation("修改患者信息")
    @PreAuthorize("@ss.hasPermi('manage:patientInfo:edit')")
    @Log(title = "患者信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<PatientInfo> edit(@RequestBody PatientInfo patientInfo) {
        return R.ok(patientInfoService.updatePatientInfo(patientInfo));
    }

	/**
	 * 患者配置--修改患者信息
	 */
	@ApiOperation("患者配置--修改患者信息")
	@PreAuthorize("@ss.hasPermi('manage:patientInfo:edit')")
	@Log(title = "患者信息", businessType = BusinessType.UPDATE)
	@PutMapping("/updateBaseInfo")
	public R<PatientInfo> updateBaseInfo(@RequestBody PatientBaseInfoDto patientInfo) {
		return R.ok(patientInfoService.updateBaseInfo(patientInfo));
	}

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

    //isDistinct==1，表示自动去重；==0表示不去重；
    @ApiOperation("患者信息导入")
    @PostMapping("/patientUpload")
    public AjaxResult patientUpload(MultipartFile file, Integer isDistinct,String importName) throws Exception {
        //判断excel里面是否有数据/文件格式
        if (Objects.isNull(file) || StringUtils.isBlank(file.getOriginalFilename())) {
            return AjaxResult.error("请选择需要导入的文件！");
        }
        // 获取文件名
        String filename = file.getOriginalFilename();
        if (!filename.endsWith(Constants.XLSX) && !filename.endsWith(Constants.XLS)) {
            return AjaxResult.error("导入文件格式不正确，请导入xlsx或xls格式的文件！");
        }
        ExcelUtil<PatientInfoImport> util = new ExcelUtil<>(PatientInfoImport.class);
        List<PatientInfoImport> list = util.importExcel(file.getInputStream());
        return patientInfoService.patientUpload(list, isDistinct, filename,importName);
    }

    @ApiOperation("未识别科室名称导入")
    @PostMapping("/secondaryUpload")
    public AjaxResult secondaryUpload(@RequestBody PatientInfoImportVO patientInfoImportVO) {
        return patientInfoService.secondaryUpload(patientInfoImportVO);
    }
}
