package com.emr.service.ipml;

import com.alibaba.fastjson.JSONArray;
import com.emr.dao.StatisticsMapper;
import com.emr.entity.*;
import com.emr.service.Emr_DictionaryService;
import com.emr.service.PowerUserService;
import com.emr.service.StatisticsService;
import com.emr.vo.Power_User_Dto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    StatisticsMapper statisticsMapper;
    @Autowired
    Emr_DictionaryService emrDictionaryService;
    @Autowired
    private PowerUserService powerUserService;


    @Override
    public OffsetLimitPage getStatisticsVo(StatisticsVo statisticsVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<StatisticsVo> statisticsVos = statisticsMapper.getStatisticsVo(statisticsVo);
        for (StatisticsVo list : statisticsVos) {
            int vxPrintNum = list.getVxPrintNum();
            int vxPrintNum1 = list.getVxPrintNum1();
            list.setVxPrintNum(vxPrintNum + vxPrintNum1);
        }
        return new OffsetLimitPage((Page) statisticsVos);
    }

    @Override
    public OffsetLimitPage getRecoveryStatistics(Archive_otherVo archiveOtherVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_otherVo> recoveryStatistics = statisticsMapper.getRecoveryStatistics(archiveOtherVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
        for (int i = 0; i < recoveryStatistics.size(); i++) {
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                String deptName = recoveryStatistics.get(i).getDeptName();
                //入院科室dept_admission_to
                String dept2 = recoveryStatistics.get(i).getDeptAdmissionTo();

                if ((deptName != null && deptName.equals(dicList.get(k).getCode()))) {
                    //出院科室
                    deptName = deptName.replace(deptName, dicList.get(k).getName());
                    recoveryStatistics.get(i).setDeptName(deptName);
                }
                if (dept2 != null && dept2.equals(dicList.get(k).getCode())) {
                    //入院科室
                    dept2 = dept2.replace(dept2, dicList.get(k).getName());
                    recoveryStatistics.get(i).setDeptAdmissionTo(dept2);
                }
            }
        }
        return new OffsetLimitPage((Page) recoveryStatistics);
    }

    @Override
    public OffsetLimitPage getFileStatistics(FileVo fileVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<FileVo> fileStatistics = statisticsMapper.getFileStatistics(fileVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
        for (int i = 0; i < fileStatistics.size(); i++) {
            String masterId = fileStatistics.get(i).getID();
            FileVo fileHandle = statisticsMapper.gethandleName(masterId);
            if (fileHandle != null) {
                fileStatistics.get(i).setHandleTime(fileHandle.getHandleTime());
                fileStatistics.get(i).setHandleName(fileHandle.getHandleName());
            }
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                if (StringUtils.isNotBlank(fileStatistics.get(i).getDeptName())) {
                    String deptName = fileStatistics.get(i).getDeptName();
                    if ((deptName != null && deptName.equals(dicList.get(k).getCode()))) {
                        //出院科室
                        deptName = deptName.replace(deptName, dicList.get(k).getName());
                        fileStatistics.get(i).setDeptName(deptName);
                    }
                }
                if (StringUtils.isNotBlank(fileStatistics.get(i).getDeptAdmissionTo())) {
                    //入院科室dept_admission_to
                    String dept2 = fileStatistics.get(i).getDeptAdmissionTo();
                    if (dept2 != null && dept2.equals(dicList.get(k).getCode())) {
                        //入院科室
                        dept2 = dept2.replace(dept2, dicList.get(k).getName());
                        fileStatistics.get(i).setDeptAdmissionTo(dept2);
                    }
                }
            }
        }
        return new OffsetLimitPage((Page) fileStatistics);
    }

    @Override
    public OffsetLimitPage getBorrowingStatistics(BorrowingVo borrowingVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<BorrowingVo> borrowingStatistics = statisticsMapper.getBorrowingStatistics(borrowingVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
        for (int i = 0; i < borrowingStatistics.size(); i++) {
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                if (StringUtils.isNotBlank(borrowingStatistics.get(i).getDeptName())) {
                    String deptName = borrowingStatistics.get(i).getDeptName();
                    if ((deptName != null && deptName.equals(dicList.get(k).getCode()))) {
                        //出院科室
                        deptName = deptName.replace(deptName, dicList.get(k).getName());
                        borrowingStatistics.get(i).setDeptName(deptName);
                    }
                }
                if (StringUtils.isNotBlank(borrowingStatistics.get(i).getDeptAdmissionTo())) {
                    String dept2 = borrowingStatistics.get(i).getDeptAdmissionTo();
                    if (dept2 != null && dept2.equals(dicList.get(k).getCode())) {
                        //入院科室
                        dept2 = dept2.replace(dept2, dicList.get(k).getName());
                        borrowingStatistics.get(i).setDeptAdmissionTo(dept2);
                    }
                }

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

    @Override
    public OffsetLimitPage getWindowPrintStatistics(WindowPrintVo windowPrintVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<WindowPrintVo> windowPrintStatistics = statisticsMapper.getWindowPrintStatistics(windowPrintVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
        for (int i = 0; i < windowPrintStatistics.size(); i++) {
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                if (StringUtils.isNotBlank(windowPrintStatistics.get(i).getDeptName())){
                    String deptName = windowPrintStatistics.get(i).getDeptName();
                    if ((deptName != null && deptName.equals(dicList.get(k).getCode()))) {
                        //出院科室
                        deptName = deptName.replace(deptName, dicList.get(k).getName());
                        windowPrintStatistics.get(i).setDeptName(deptName);
                    }
                }
                if (StringUtils.isNotBlank(windowPrintStatistics.get(i).getDeptAdmissionTo())){
                    //入院科室dept_admission_to
                    String dept2 = windowPrintStatistics.get(i).getDeptAdmissionTo();
                    if (dept2 != null && dept2.equals(dicList.get(k).getCode())) {
                        //入院科室
                        dept2 = dept2.replace(dept2, dicList.get(k).getName());
                        windowPrintStatistics.get(i).setDeptAdmissionTo(dept2);
                    }
                }
            }
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (WindowPrintVo emrFaultVo : windowPrintStatistics) {
                if (null != powerUserList) {
                    //获取处理人
                    String dealPerson = emrFaultVo.getPrintUserid();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(dealPerson) && powerUserDto.getUserName().equals(dealPerson)) {
                            emrFaultVo.setPrintUserName(name);
                        }

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

    }
}
