package thyyxxk.webserver.controller.dailypatientcount;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.dao.his.scheduled.DailyPatientCountDao;
import thyyxxk.webserver.entity.scheduled.ClinicCount;
import thyyxxk.webserver.entity.scheduled.DailyPatientCount;
import thyyxxk.webserver.service.outpatient.wxapi.SendWxInfoService;
import thyyxxk.webserver.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dailyPatientCount")
public class DailyPatientCountController {
    private final DailyPatientCountDao dao;
    private final SendWxInfoService service;

    @Autowired
    public DailyPatientCountController(DailyPatientCountDao dao, SendWxInfoService service) {
        this.dao = dao;
        this.service = service;
    }

    @GetMapping("/notifyDailyCount")
    @PassToken
    public void notifyDailyCount() {
        DailyPatientCount count = dao.selectDailyCount();
        List<ClinicCount> list = dao.selectClinicCount();
        String message = makeDescription(count, list);
        service.sendGroupMsg("740552821090355200", message);
    }

    private String makeDescription(DailyPatientCount count, List<ClinicCount> list) {
        if (null == count) {
            count = new DailyPatientCount();
            count.initAllCount();
        }
        if (null == list) {
            list = new ArrayList<>();
        }
        int clinicCount = 0;
        StringBuilder builder = new StringBuilder();
        if (!list.isEmpty()) {
            builder.append("\n\n以下为挂号详情：\n");
            for (ClinicCount item : list) {
                clinicCount += item.getCount();
                builder.append(item.getUnitName()).append("：").append(item.getCount()).append("\n");
            }
        }
        String mainpart = String.format("%s总值班情况汇报如下：住院总人数：%d，入院人数：%d，出院人数：%d，病重人数：%d，病危人数：%d，死亡人数：%d，" +
                        "手术台次：%d，抢救人次：%d，挂号人数：%d。",
                DateUtil.getYesterday(), count.getInHospCount(), count.getAdmissCount(), count.getDismissCount(),
                count.getHeavyCount(), count.getDangerCount(), count.getDeadCount(), count.getOperationCount(),
                count.getRescueCount(), clinicCount);
        return mainpart + builder;
    }
}
