/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/19 15:39
 * Description:患者住院信息主索引管理
 */
package com.emr.service.ipml;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emr.dao.Archive_DetailMapper;
import com.emr.dao.Archive_MasterMapper;
import com.emr.dao.MedicalRecordHomepageCacheMapper;
import com.emr.entity.*;
import com.emr.service.Archive_MasterService;
import com.emr.service.Emr_DictionaryService;
import com.emr.service.PowerUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Function;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.collect.Maps;

import java.util.*;

@Service
public class Archive_MasterServiceImpl implements Archive_MasterService {

    @Autowired
    private Archive_MasterMapper archiveMasterMapper;

    @Autowired
    private Emr_DictionaryService emrDictionaryService;

    @Autowired
    private PowerUserService powerUserService;

    @Autowired
    private MedicalRecordHomepageCacheMapper medicalRecordHomepageCacheMapper;

    @Override
    public Archive_Master selectById(String id) {
        return archiveMasterMapper.selectById(id);
    }

    @Override
    public List<Archive_Master> selByCol(Archive_Master archiveMaster) {
        return archiveMasterMapper.selByCol(archiveMaster);
    }

    @Override
    public OffsetLimitPage selectByCol(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master> list= archiveMasterMapper.selectByCol(archiveMasterVo);

        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 archive_master : list) {

                JSONObject userInfo = mappedMovies.get(archive_master.getDoctorName());
                if (userInfo==null){
                    archive_master.setDoctorName(archive_master.getDoctorName());
                }else {
                    archive_master.setDoctorName(userInfo.getString("name"));
                }

                userInfo = mappedMovies.get(archive_master.getNurseName());
                if (userInfo==null){
                    archive_master.setNurseName(archive_master.getNurseName());
                }else {
                    archive_master.setNurseName(userInfo.getString("name"));
                }
            }

        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master> selectByCol(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByCol(archiveMasterVo);
    }

    @Override
    public List<Archive_Master_Vo> selectByColumn(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByColumn(archiveMasterVo);
    }

    @Override
    public List<Archive_Master_Vo> selectByUnfile(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByUnfile(archiveMasterVo);
    }

   /*   @Override
    public List<Archive_Master> selectByUnfile2(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByUnfile2(archiveMasterVo);
    }*/

   /* @Override
    public List<Archive_Master> sel7DayByCol(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.sel7DayByCol(archiveMasterVo);
    }*/

    @Override
    public List<Archive_Master_Vo> selectByCmtCol(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByCmtCol(archiveMasterVo);
    }

    @Override
    public List<Archive_Master_Vo> selectByLast(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByLast(archiveMasterVo);
    }

    @Override
    public OffsetLimitPage selectByColumn(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByColumn(archiveMasterVo);

        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 archive_master : list) {
                JSONObject userInfo = mappedMovies.get(archive_master.getDoctorName());
                if (userInfo==null){
                    archive_master.setDoctorName(archive_master.getDoctorName());
                }else {
                    archive_master.setDoctorName(userInfo.getString("name"));
                }

                userInfo = mappedMovies.get(archive_master.getNurseName());
                if (userInfo==null){
                    archive_master.setNurseName(archive_master.getNurseName());
                }else {
                    archive_master.setNurseName(userInfo.getString("name"));
                }
            }

        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public OffsetLimitPage selectByUnfile(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByUnfileNew(archiveMasterVo);

        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 archive_master : list) {

                JSONObject userInfo = mappedMovies.get(archive_master.getDoctorName());
                if (userInfo==null){
                    archive_master.setDoctorName(archive_master.getDoctorName());
                }else {
                    archive_master.setDoctorName(userInfo.getString("name"));
                }

                userInfo = mappedMovies.get(archive_master.getNurseName());
                if (userInfo==null){
                    archive_master.setNurseName(archive_master.getNurseName());
                }else {
                    archive_master.setNurseName(userInfo.getString("name"));
                }
            }

        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public OffsetLimitPage selectByLast(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByLast(archiveMasterVo);

        JSONArray powerUsers = powerUserService.getPowerUserList("1");
        List<JSONObject> powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), JSONObject.class);

        if (powerUserList != null) {
            Map<String, JSONObject> mappedMovies = Maps.uniqueIndex(powerUserList, temp -> temp.getString("userName"));

            for (Archive_Master_Vo archive_master : list) {

                JSONObject userInfo = mappedMovies.get(archive_master.getDoctorName());
                if (userInfo == null) {
                    archive_master.setDoctorName(archive_master.getDoctorName());
                } else {
                    archive_master.setDoctorName(userInfo.getString("name"));
                }

                userInfo = mappedMovies.get(archive_master.getNurseName());
                if (userInfo == null) {
                    archive_master.setNurseName(archive_master.getNurseName());
                } else {
                    archive_master.setNurseName(userInfo.getString("name"));
                }

                userInfo = mappedMovies.get(archive_master.getFirstInstance());
                if (userInfo == null) {
                    archive_master.setFirstInstance(archive_master.getFirstInstance());
                } else {
                    archive_master.setFirstInstance(userInfo.getString("name"));
                }
            }
        }

        return new OffsetLimitPage((Page) list);
    }

    @Override
    public OffsetLimitPage selectByCmtCol(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByCmtCol(archiveMasterVo);

        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 archive_master : list) {

                JSONObject userInfo = mappedMovies.get(archive_master.getDoctorName());
                if (userInfo==null){
                    archive_master.setDoctorName(archive_master.getDoctorName());
                }else {
                    archive_master.setDoctorName(userInfo.getString("name"));
                }

                userInfo = mappedMovies.get(archive_master.getNurseName());
                if (userInfo==null){
                    archive_master.setNurseName(archive_master.getNurseName());
                }else {
                    archive_master.setNurseName(userInfo.getString("name"));
                }
            }

        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public int updateByClo(Archive_Master record) {
        return archiveMasterMapper.updateByClo(record);
    }

    @Override
    public int updateById(Archive_Master record) {
        return archiveMasterMapper.updateById(record);
    }

    @Override
    public List<Archive_Master> getDoctorInCharge(Archive_Master record) {
        return archiveMasterMapper.getDoctorInCharge(record);
    }

    @Override
    public List<Archive_Master> bbSel(Archive_Master_Vo record) {
        return archiveMasterMapper.bbSel(record);
    }

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

    @Override
    public List<Archive_Master> bbSel2(Archive_Master_Vo record) {
        return archiveMasterMapper.bbSel2(record);
    }

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

    @Override
    public List<Archive_Master_Vo> selectHandleDetail(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterMapper.selectHandleDetail(archiveMasterVo);
        return list;
    }

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

    @Override
    public List<Archive_Master_Vo> selectOverdue(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterMapper.selectOverdue(archiveMasterVo);
        return list;
    }

    @Override
    public OffsetLimitPage selectOverdue(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectOverdue(archiveMasterVo);

        List<MedicalRecordHomepageCache> medicalRecordHomepageCacheList = null;

        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 archive_master : list) {
                String doctorInChargeInHomePage = "";

                //根据masterId查出该条首页缓存表中content内容
                MedicalRecordHomepageCacheExample medicalRecordHomepageCacheExample = new MedicalRecordHomepageCacheExample();
                MedicalRecordHomepageCacheExample.Criteria criteria = medicalRecordHomepageCacheExample.createCriteria();
                if (!StringUtils.isEmpty(archive_master.getId())){
                    criteria.andMasterIdEqualTo(archive_master.getId());
                }
                medicalRecordHomepageCacheList = medicalRecordHomepageCacheMapper.selectByExample(medicalRecordHomepageCacheExample);

                if (medicalRecordHomepageCacheList.size()>0){
                    //查出content
                    String content = medicalRecordHomepageCacheList.get(0).getContent();
                    //根据content内容获取首页中主治医生name
                    doctorInChargeInHomePage = JSONObject.parseObject(content).getJSONObject("commonTable").getString("yZY054");
                }
                //重新赋值主管医生
                if (doctorInChargeInHomePage != null && !doctorInChargeInHomePage.equals("")){
                    archive_master.setDoctorInCharge(doctorInChargeInHomePage);
                }else {
                    archive_master.setDoctorInCharge("首页尚未填写主治医生！");
                }


                JSONObject userInfo = mappedMovies.get(archive_master.getFirstInstance());
                if (userInfo==null){
                    archive_master.setFirstInstance(archive_master.getFirstInstance());
                }else {
                    archive_master.setFirstInstance(userInfo.getString("name"));
                }
            }

        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public OffsetLimitPage selectByFollowinglog(Archive_Master_Vo record, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByFollowinglog(record);

        return  new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> selectByFollowinglog(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByFollowinglog(archiveMasterVo);
    }

    @Override
    public List<Archive_Master> selectByObject(String patientId,String visitId) {
        return archiveMasterMapper.selectByObject(patientId,visitId);
    }

}
 

