package com.fs.hospital.controller.hospital;

import java.util.List;

import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.hospital.service.IHospitalMemberService;
import com.fs.hospital.vo.HospitalPatientHVO;
import com.fs.hospital.vo.HospitalPharmacyOrderHVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.HospitalPatient;
import com.fs.hospital.service.IHospitalPatientService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 患者Controller
 *
 * @author fs
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/hospital/hospitalPatient")
public class HospitalPatientController extends BaseController
{
    @Autowired
    private IHospitalPatientService hospitalPatientService;

    @Autowired
    private IHospitalMemberService hospitalMemberService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询患者列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPatient:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalPatient hospitalPatient)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPatient.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalPatient> list = hospitalPatientService.selectHospitalPatientList(hospitalPatient);
        return getDataTable(list);
    }

    /** 根据条件模糊查询患者列表 */
    @GetMapping("/patientScreenlist")
    public TableDataInfo patientScreenlist(HospitalPatient hospitalPatient)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPatient.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalPatient> list = hospitalPatientService.selectHospitalPatientScreenList(hospitalPatient);
        return getDataTable(list);
    }

    /**
     * 导出患者列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPatient:export')")
    @Log(title = "患者", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HospitalPatient hospitalPatient)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPatient.setHospitalId( loginUser.getHospital().getHospitalId());
        List<HospitalPatient> list = hospitalPatientService.selectHospitalPatientList(hospitalPatient);
        ExcelUtil<HospitalPatient> util = new ExcelUtil<HospitalPatient>(HospitalPatient.class);
        return util.exportExcel(list, "患者数据");
    }

    /**
     * 获取患者详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPatient:query')")
    @GetMapping(value = "/{patientId}")
    public AjaxResult getInfo(@PathVariable("patientId") Long patientId)
    {
        HospitalPatientHVO hospitalPatientPVO = hospitalPatientService.selectHospitalPatientPVOByPatientId(patientId);
        if (hospitalPatientPVO.getMemberId()!=null){
            hospitalPatientPVO.setHospitalMember(hospitalMemberService.selectHospitalMemberByMemberId(hospitalPatientPVO.getMemberId()));
        }
        return AjaxResult.success(hospitalPatientPVO);
    }

    /**
     * 新增患者
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPatient:add')")
    @Log(title = "患者", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody HospitalPatient hospitalPatient)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPatient.setHospitalId( loginUser.getHospital().getHospitalId());
        return toAjax(hospitalPatientService.insertHospitalPatient(hospitalPatient));
    }

    /**
     * 修改患者
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPatient:edit')")
    @Log(title = "患者", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HospitalPatient hospitalPatient)
    {
        return toAjax(hospitalPatientService.updateHospitalPatient(hospitalPatient));
    }

    /**
     * 删除患者
     */
    @PreAuthorize("@ss.hasPermi('hospital:hospitalPatient:remove')")
    @Log(title = "患者", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patientIds}")
    public AjaxResult remove(@PathVariable Long[] patientIds)
    {
        return toAjax(hospitalPatientService.deleteHospitalPatientByPatientIds(patientIds));
    }

    @GetMapping("/patientByMemberList")
    public TableDataInfo patientByMemberList(HospitalPatient hospitalPatient)
    {
        startPage();
        hospitalPatient.setIsDel(0);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPatient.setHospitalId(loginUser.getHospital().getHospitalId());
        List<HospitalPatient> list = hospitalPatientService.selectHospitalPatientListPVO(hospitalPatient);
        return getDataTable(list);
    }


    /**
     *  新增患者
     */
    @Log(title = "患者", businessType = BusinessType.INSERT)
    @PostMapping("/addPatient")
    public R addPatient(@RequestBody HospitalPatient hospitalPatient)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPatient.setHospitalId( loginUser.getHospital().getHospitalId());
        if (hospitalPatientService.insertHospitalPatient(hospitalPatient)>0){
            return R.ok().put("data",hospitalPatient);
        }
        return R.error("新增失败");
    }

    @GetMapping("/allPatient")
    public R allPatient(HospitalPatient hospitalPatient)
    {
        hospitalPatient.setIsDel(0);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        hospitalPatient.setHospitalId(loginUser.getHospital().getHospitalId());
        List<HospitalPatient> list = hospitalPatientService.selectHospitalPatientList(hospitalPatient);
        return R.ok().put("data",list);
    }

    /**
     *  获取患者病例就诊大概记录
     * @param patientId 患者ID
     */
    @GetMapping("/getCaseRecords/{patientId}")
    public R getCaseRecords(@PathVariable Long patientId){
        return R.ok().put("data",hospitalPatientService.getCaseRecords(patientId));
    }

    /**
     *  获取详细的病例就诊记录(如诊疗订单)
     * @param orderId hospital_outpatient_order门诊订单id
     */
    @GetMapping("/getDetailedCaseRecords/{orderId}")
    public R getDetailedCaseRecords(@PathVariable Long orderId, @RequestParam("type") int type){

        return R.ok().put("data",hospitalPatientService.getDetailedCaseRecords(orderId,type));
    }

}
