package com.example.controller;
import com.example.bean.DailyReport;
import com.example.bean.MedicalRecord;
import com.example.bean.Drugs;
import com.example.bean.RespBean;
import com.example.mapper.MedicalRecordMapper;
import com.example.service.DoctorService;
import com.example.service.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/medicalrecord")
public class MedicalRecordController {
    @Autowired
    private MedicalRecordService medicalRecordService;
    @Autowired
    private DoctorService doctorService;

    @PostMapping("/addMedicalRecord")
    public RespBean addNewMedicalRecord(@RequestBody MedicalRecord medicalRecord) {
        System.out.println(medicalRecord);
        int result = medicalRecordService.addNewMedicalRecord(medicalRecord);
        if(result == 1){
            return new RespBean("success","添加成功");
        }
        else{
            return new RespBean("error","添加失败");
        }
    }

    @PostMapping("/addDrugs")
    public RespBean addDrugs(@RequestBody Drugs drugs){
        int result = medicalRecordService.addDrugs(drugs);
        if(result == 2){
            return new RespBean("success","添加成功",drugs.getId());
        }
        else {
            return new RespBean("error","添加失败");
        }
    }

    @GetMapping("/deleteMedicalrecord")
    public RespBean deleteMedicalRecord(int mid){
        int result = medicalRecordService.deleteMedicalRecordByMid(mid);
        if(result == 1){
            return new RespBean("success","删除成功");
        }
        else{
            return new RespBean("error","删除失败");
        }
    }

    @GetMapping("/deleteDrugs")
    public RespBean deleteDrugs(int did)
    {
        int result = medicalRecordService.deleteDrugs(did);
        if(result == 1){
            return new RespBean("success","删除成功");
        }
        else{
            return new RespBean("error","删除失败");
        }
    }

    @PostMapping("/updateMedicalRecord")
    public RespBean updateMedicalRecord(@RequestBody MedicalRecord medicalRecord)
    {
        System.out.println(medicalRecord);
        int result = medicalRecordService.updateMedicalRecord(medicalRecord);
        if(result == 1){
            return new RespBean("success","更改成功");
        }
        else{
            return new RespBean("error","更改失败");
        }
    }

    @GetMapping("showUSummary")
    public RespBean showSummary(@RequestParam("usid") String uid){
        int result = medicalRecordService.getSummaryByUid(uid);
        if(result == 1){
            return new RespBean("success","获取成功");
        }
        else{
            return new RespBean("error","获取失败");
        }
    }

    @GetMapping("selectRecordByUid")
    public List<MedicalRecord> selectRecordByUid(String uid)
    {
        System.out.println(uid);
        return medicalRecordService.selectByUserid(uid);
    }

    @GetMapping("/selectByUseridDoctor")
    public List<RespBean> selectRecordByUid2(String userid)
    {
        List<MedicalRecord> medicalRecords = medicalRecordService.selectByUserid(userid);
        List<RespBean> respBeans = new ArrayList<>();
        for (MedicalRecord medicalRecord : medicalRecords) {
            RespBean respBean = new RespBean(medicalRecord);
            respBean.setMsg(doctorService.getDoctorById(medicalRecord.getDid()).getUsername());
            respBeans.add(respBean);
        }
        return respBeans;
    }

    @GetMapping("selectRecordByDid")
    public List<MedicalRecord> selectRecordByDid(String did)
    {
        return medicalRecordService.selectByDid(did);
    }

    @GetMapping("/showDetail")
    public RespBean showDetail(int mid)//返回一个病历的详细信息，包括其中的处方
    {
        MedicalRecord medicalRecord = medicalRecordService.getMedicalRecordByMid(mid);
        medicalRecord.setDrugs(medicalRecordService.getDrugsById(mid));
        return new RespBean(medicalRecord);
    }
}
