package com.joju.datamanager.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joju.datamanager.common.annotation.EleganceLog;
import com.joju.datamanager.common.result.Result;
import com.joju.datamanager.common.result.ResultUtil;
import com.joju.datamanager.model.medicalprescription.MedicalPrescription;
import com.joju.datamanager.service.MedicalPrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 医嘱开单接口
 * @author: Mr.zs
 * @create: 2024-04-02 19:11
 **/
@Slf4j
@RestController
@RequestMapping("/MedicalPrescription")
public class MedicalPrescriptionController {


    @Autowired
    MedicalPrescriptionService medicalPrescriptionService;


    @GetMapping("/getLocalMedicalPrescription")
    @EleganceLog(description = "获取本地的医嘱开单信息")
    public Result getLocalMedicalPrescription() {
        String today = DateUtil.today();
        String todayString = today + " 00:00:00";
        DateTime tomorrow = DateUtil.tomorrow();
        String s = DateUtil.formatDate(tomorrow);
        String tomorrowString = s + " 00:00:00";

        List<MedicalPrescription> medicalPrescriptions = medicalPrescriptionService.list(new QueryWrapper<MedicalPrescription>().between("lrrq", todayString, tomorrowString));

        if (!CollUtil.isEmpty(medicalPrescriptions)) {
            log.info("medicalPrescriptions is :" + medicalPrescriptions);
            return ResultUtil.successData(medicalPrescriptions);
        }
        return ResultUtil.failureMsg("获取失败！！");
    }


    @PostMapping("insertLocalMedicalPrescriptions")
    @EleganceLog(description = "批量插入本地医嘱开单信息")
    public Result insertLocalMedicalPrescriptions(List<MedicalPrescription> medicalPrescriptions) {
        boolean b = medicalPrescriptionService.saveBatch(medicalPrescriptions);
        if (b) {
            return ResultUtil.success();
        } else {
            return ResultUtil.failureMsg("插入失败!!");
        }
    }
}


