package thyyxxk.webserver.service.highreport;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.highreport.FullScreenDao;
import thyyxxk.webserver.dao.his.highreport.HighReportDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.highreport.HighReportDto;
import thyyxxk.webserver.entity.highreport.ReportBaseInfo;
import thyyxxk.webserver.entity.highreport.TableGroupInfo;
import thyyxxk.webserver.utils.ReportUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 数据大屏统一入口
 * @Author: hsh
 * @CreateTime: 2022-12-13  15:49
 * @Version: 1.0
 */
@Service
@Slf4j
public class FullScreenService {

    private final FullScreenDao dao;

    private final HighReportDao highDao;

    @Autowired
    public FullScreenService(FullScreenDao dao, HighReportDao highDao) {
        this.dao = dao;
        this.highDao = highDao;
    }

    public ResultVo<Map<String, Object>> selectFullScreenData(HighReportDto dto) {
        String type = dto.getType();
        Map<String, Object> map = new HashMap<>(0);

        // 查询数字大屏展示指标
        List<ReportBaseInfo> reportList = dao.selectFullScreenReport(type);

        // 根据条件查询指标数据
        for(ReportBaseInfo report : reportList){
            // 获取数据sql
            String sql = ReportUtil.callSqlFormat(dto, report.getBaseSql(), report.getGatherSql(), null, null, null);
            // 查询总览数据结果
            String overview = highDao.selectReportPortalOverview(report.getReportId(), report.getMenuId(), report.getReportType(), sql);
            report.setOverview(overview);

            TableGroupInfo info = highDao.selectTableGroupInfoByLevel(report.getReportId(), report.getLevel());
            if(info == null){
                info = new TableGroupInfo();
                info.setFstGroup("dept");
                info.setFstName("科室");
            }
            String sql1 = ReportUtil.callSqlFormatByGroup(dto, report, info, false);
            // log.info("指标: {}, sql: {}", report.getReportName(), sql1);
            List<Map<String, Object>> dataList = highDao.selectReportPortalData(report.getReportId(), report.getMenuId(), report.getReportType(), sql1);
            report.setDataList(dataList);

        }

        map.put("data", reportList);
        return ResultVoUtil.success(map);
    }

}
