package com.ywt.mg.services;

import com.ywt.mg.core.SqlHelper;
import com.ywt.mg.core.utils.CacheUtil;
import com.ywt.mg.core.utils.Checker;
import com.ywt.mg.core.utils.CollectionUtil;
import com.ywt.mg.domain.ywtDrugEntities.Pharmacy;
import com.ywt.mg.domain.ywtDrugEntities.PharmacyRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PharmacyCacheService {

    private static Logger logger = LoggerFactory.getLogger(PharmacyCacheService.class);

    private final String PHARMACY_LIST = "PharmacyList";
    private final String Pharmacy_MAP = "PharmacyMap";

    @Autowired
    private PharmacyRepository pharmacyRepository;

    @Autowired
    private SqlHelper sqlHelperYwtcenter;

    /**
     * 从数据库返回所有药房数据
     *
     * @return
     */
    public List<Pharmacy> getPharmacyList() {
        try {
            List<Pharmacy> pharmacyList = getAllPharmacyList();
            if (!Checker.isNone(pharmacyList)) {
                // display:0-显示，1-不显示， status: 0-正常，1-禁用
                // todo: status，不管禁用不禁用，都展示
                return pharmacyList.stream().filter(p -> Checker.getIntegerValue(p.getDisplay()) == 0).collect(Collectors.toList());
            }
        } catch (Exception e) {
        }
        return new LinkedList<>();
    }

    /**
     * 从数据库返回所有药房数据
     *
     * @return
     */
    public List<Pharmacy> getAllPharmacyList() {
        try {
            return pharmacyRepository.getAllPharmacyList();
        } catch (Exception e) {
        }
        return new LinkedList<>();
    }

    /**
     * 从缓存中返回药房医院
     *
     * @return
     */
    public List<Pharmacy> getPharmacyListHigh() {
        try {
            return (List<Pharmacy>) CacheUtil.getInstance().get(PHARMACY_LIST, () -> getAllPharmacyList());
        } catch (Exception e) {
            logger.error("PharmacyCacheService#getPharmacyListHigh():\n {}", e.getMessage(), e);
        }
        return new LinkedList<>();
    }

    /**
     * 从缓存中返回所有药房 Map by ID
     *
     * @return {@link Map<Integer, Pharmacy>}
     */
    public Map<Integer, Pharmacy> getPharmacyMap() {
        try {
            return (Map<Integer, Pharmacy>) CacheUtil.getInstance().get(Pharmacy_MAP, () -> CollectionUtil.toMap(getPharmacyListHigh(), Pharmacy::getId));
        } catch (Exception e) {
            logger.error("PharmacyCacheService#getPharmacyMap():\n {}", e.getMessage(), e);
        }
        return new HashMap<>();
    }

    public Pharmacy getPharmacyById(int id) {
        Map<Integer, Pharmacy> map = getPharmacyMap();
        Pharmacy pharmacy = map.getOrDefault(id, null);
        return pharmacy;
    }


    public String getPharmacyNameById(int id) {
        Map<Integer, Pharmacy> map = getPharmacyMap();
        Pharmacy pharmacy = map.getOrDefault(id, null);
        if (pharmacy == null) {
            return "";
        }
        return pharmacy.getName();
    }


}
