package com.ywt.mg.services;

import com.ywt.mg.core.SqlHelper;
import com.ywt.mg.core.utils.Checker;
import com.ywt.mg.domain.entities.MedicalCard;
import com.ywt.mg.domain.entities.MedicalCardRepository;
import com.ywt.mg.domain.models.ExcelDataMap;
import com.ywt.mg.domain.models.ExcelStyleMap;
import com.ywt.mg.domain.models.pojo.ConstantRightDisplayDef;
import com.ywt.mg.domain.models.pojo.ExcelCollectPojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommonWxPushService {
    private static Logger logger = LoggerFactory.getLogger(CommonWxPushService.class);

    @Autowired
    private SqlHelper sqlHelper;

    @Autowired
    private MedicalCardRepository medicalCardRepository;

    @Autowired
    private AuthService authService;

    @Autowired
    private DownloadRecordService downloadRecordService;


    public List<MedicalCard> getListByHospitalId(int hospitalId) {
        return medicalCardRepository.getListByHospitalId(hospitalId);
    }


    public void downloadHisPatientId(int downloadRecordId, String fileName, Integer hospitalId) {
        try {
//            String downloadHisPatientidRightCode = ConstantRightDisplayDef.COMMON_WX_PUSH_DISPLAY_DOWNLOAD_HISPATIENTID;
//            boolean downloadHisPatientidRight = authService.checkCacheRight(downloadHisPatientidRightCode);
//            if (!downloadHisPatientidRight) {
//                logger.error("CommonWxPushService#downloadHisPatientId>>没有权限，请先设置权限，userId:{}", authService.getCurrentAdmin());
//            }
            List<MedicalCard> infoList = getListByHospitalId(hospitalId);

            String col1 = "患者ID";
            String col2 = "患者姓名";
            String col3 = "身份证号";
            String col4 = "手机号";

            String[] columns = new String[]{col1, col2, col3, col4};
            ExcelDataMap map = new ExcelDataMap(columns);
            String filename = "患者ID";
            if (Checker.isNone(infoList)) {
                return;
            }
            for (MedicalCard item : infoList) {
                map.getStringListSafely(col1).add(item.getHisPatientId());
                map.getStringListSafely(col2).add(item.getPatientName());
                map.getStringListSafely(col3).add(item.getIdNo());
                map.getStringListSafely(col4).add(item.getMobile());
            }
            // 得到统计
            int size = infoList.size();
            List<ExcelCollectPojo> itemList = null;
            ExcelStyleMap excelStyleMap = new ExcelStyleMap(new String[]{});
            int bill = 0;
            downloadRecordService.createFileAndUploadOssAndSaveToDataBase(fileName, downloadRecordId, map, itemList, size, bill);
        } catch (Exception e) {
            logger.error("/CommonWxPushService/downloadHisPatientId(): {}", e.getMessage(), e);
            e.printStackTrace();
        }
    }
}
