package com.emr.service.ipml;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emr.dao.Archive_MasterMapper;
import com.emr.entity.Archive_Master_Vo;
import com.emr.entity.OffsetLimitPage;
import com.emr.service.Observation_ReportService;
import com.emr.service.PowerUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class Observation_ReportServiceImpl implements Observation_ReportService {
    @Autowired
    private Archive_MasterMapper archiveMasterMapper;

    @Autowired
    private PowerUserService powerUserService;

    @Override
    public OffsetLimitPage getObservationReportList(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list=selectByCol(archiveMasterVo);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> getObservationReportList(Archive_Master_Vo archiveMasterVo) {
        return selectByCol(archiveMasterVo);
    }

    public List<Archive_Master_Vo> selectByCol(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list=archiveMasterMapper.getObservationReportList(archiveMasterVo);
        if(list!=null && list.size()>0) {
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<JSONObject> powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), JSONObject.class);
            if (powerUserList != null) {
                Map<String, JSONObject> mappedMovies = Maps.uniqueIndex(powerUserList, new Function<JSONObject, String>() {
                    @Override
                    public String apply(JSONObject temp) {
                        return temp.getString("userName");
                    }
                });
                for (Archive_Master_Vo archiveMaster : list) {
                    //主管医生
                    JSONObject userInfo = mappedMovies.get(archiveMaster.getDoctorInCharge());
                    if (userInfo == null) {
                        archiveMaster.setDoctorInCharge(archiveMaster.getDoctorInCharge());
                    } else {
                        archiveMaster.setDoctorInCharge(userInfo.getString("name"));
                    }
                }
            }
        }
        return list;
    }

}
