/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/19 15:43
 * Description:缺陷管理
 */
package com.emr.service.ipml;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emr.dao.Emr_Fault_DetailMapper;
import com.emr.entity.*;
import com.emr.service.Emr_DictionaryService;
import com.emr.service.Emr_Fault_DetailService;
import com.emr.service.Emr_Fault_TypeService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class Emr_Fault_DetailServiceImpl implements Emr_Fault_DetailService {

    @Autowired
    private Emr_Fault_DetailMapper emrFaultDetailMapper;

    @Autowired
    private Emr_Fault_TypeService emrFaultTypeService;

    @Autowired
    private Emr_DictionaryService emrDictionaryService;

    @Autowired
    private PowerUserService powerUserService;
    /**
     * 根据条件查询缺陷列表
     * @param emrFaultVo
     * @return
     */
    @Override
    public OffsetLimitPage selectByCol(Emr_Fault_Vo emrFaultVo, Integer offset, Integer limit) {
        //return emrFaultDetailMapper.selectByCol(emrFaultDetail);
        PageHelper.offsetPage(offset, limit);
        List<Emr_Fault_Vo> list = emrFaultDetailMapper.selectByCol(emrFaultVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
        //获取缺陷类别列表
        Emr_Fault_Type obj = new Emr_Fault_Type();
        obj.setEffective(1);
        List<Emr_Fault_Type> typeLis = emrFaultTypeService.selectByCol(obj);
        for (int i = 0; i < list.size(); i++) {
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                String deptName = list.get(i).getDeptName();
                //入院科室dept_admission_to
                String dept2 = list.get(i).getDeptAdmissionTo();

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

            String assortId = list.get(i).getAssortId();
            if(assortId!="" && assortId!=null) {
                String[] assorArr = assortId.split(",");
                //替换类别
                for (int j = 0; j < typeLis.size(); j++) {
                    String id = String.valueOf(typeLis.get(j).getId());
                    if (Arrays.asList(assorArr).contains(id)) {
                        assortId = assortId.replace(id, typeLis.get(j).getTypeName());
                        list.get(i).setAssortId(assortId);
                    }
                }
            }
        }

        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Emr_Fault_Vo> selectByCol(Emr_Fault_Vo emrFaultVo) {
        return emrFaultDetailMapper.selectByCol(emrFaultVo);
    }

    @Override
    public int updateCloByPrimaryKey(Emr_Fault_Detail emrFaultDetail) {
        return emrFaultDetailMapper.updateCloByPrimaryKey(emrFaultDetail);
    }

    @Override
    public Emr_Fault_Detail selectByArchiveDetailId(Emr_Fault_Detail emrFaultDetail) {
        return emrFaultDetailMapper.selectByArchiveDetailId(emrFaultDetail);
    }

    @Override
    public int insertSel(Emr_Fault_Detail record) {
        return emrFaultDetailMapper.insertSel(record);
    }

    @Override
    public List<Emr_Fault_Vo> selectByColumn(Emr_Fault_Detail emrFaultDetail) {
        return emrFaultDetailMapper.selectByColumn(emrFaultDetail);
    }

    @Override
    public OffsetLimitPage selectByColumn(Emr_Fault_Detail emrFaultDetail, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Emr_Fault_Vo> list = emrFaultDetailMapper.selectByColumn(emrFaultDetail);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public int deleteById(Integer id) {
        return emrFaultDetailMapper.deleteById(id);
    }

    @Override
    public int delByArchiveId(Emr_Fault_Detail record) {
        return emrFaultDetailMapper.delByArchiveId(record);
    }

    @Override
    public int delbyCol(Emr_Fault_Detail record) {
        return emrFaultDetailMapper.delbyCol(record);
    }

    @Override
    public Emr_Fault_Detail selectById(Integer id) {
        return emrFaultDetailMapper.selectById(id);
    }

    @Override
    public Emr_Fault_Detail selectByDept(Emr_Fault_Detail emrFaultDetail) {
        return emrFaultDetailMapper.selectByDept(emrFaultDetail);
    }

    @Override
    public List<Emr_Fault_Detail> selectByArchiveId(Emr_Fault_Detail emrFaultDetail) {
        return emrFaultDetailMapper.selectByArchiveId(emrFaultDetail);
    }

    @Override
    public OffsetLimitPage selectStorageByCol(Emr_Fault_Vo emrFaultVo, Integer offset, Integer limit) {
        //return emrFaultDetailMapper.selectByCol(emrFaultDetail);
        PageHelper.offsetPage(offset, limit);
        List<Emr_Fault_Vo> list = emrFaultDetailMapper.selectStorageByCol(emrFaultVo);
        Emr_Dictionary dic = new Emr_Dictionary();
        dic.setEffective(1);
        dic.setTypecode("dept_code");
        //科室列表
        List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
        //获取缺陷类别列表
        Emr_Fault_Type obj = new Emr_Fault_Type();
        obj.setEffective(1);
        List<Emr_Fault_Type> typeLis = emrFaultTypeService.selectByCol(obj);
        for (int i = 0; i < list.size(); i++) {
            //替换科室
            for (int k = 0; k < dicList.size(); k++) {
                String deptName = list.get(i).getDeptName();
                //入院科室dept_admission_to
                String dept2 = list.get(i).getDeptAdmissionTo();
                if (deptName != null && deptName.equals(dicList.get(k).getCode())) {
                    //出院科室
                    deptName = deptName.replace(deptName, dicList.get(k).getName());
                    list.get(i).setDeptName(deptName);
                }
                if(dept2 != null && dept2.equals(dicList.get(k).getCode())){
                    //入院科室dept_admission_to
                    dept2 = dept2.replace(dept2, dicList.get(k).getName());
                    list.get(i).setDeptAdmissionTo(dept2);
                }
            }

            String assortId = list.get(i).getAssortId();
            if(assortId!="" && assortId!=null) {
                String[] assorArr = assortId.split(",");
                //替换类别
                for (int j = 0; j < typeLis.size(); j++) {
                    String id = String.valueOf(typeLis.get(j).getId());
                    if (Arrays.asList(assorArr).contains(id)) {
                        assortId = assortId.replace(id, typeLis.get(j).getTypeName());
                        list.get(i).setAssortId(assortId);
                    }
                }
            }
        }

        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Emr_Fault_Vo> selectStorageByCol(Emr_Fault_Vo emrFaultVo) {
        return emrFaultDetailMapper.selectStorageByCol(emrFaultVo);
    }

    @Override
    public List<Emr_Fault_Vo> getQualityControlList(Emr_Fault_Vo emrFaultVo) {
        return emrFaultDetailMapper.getQualityControlList(emrFaultVo);
    }

    @Override
    public OffsetLimitPage getQualityControlList(Emr_Fault_Vo emrFaultVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Emr_Fault_Vo> list = emrFaultDetailMapper.getQualityControlList(emrFaultVo);

        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 (Emr_Fault_Vo emr_fault_vo : list) {

                JSONObject userInfo = mappedMovies.get(emr_fault_vo.getDoctorInCharge());
                if (userInfo==null){
                    emr_fault_vo.setDoctorInCharge(emr_fault_vo.getDoctorInCharge());
                }else {
                    emr_fault_vo.setDoctorInCharge(userInfo.getString("name"));
                }

                userInfo = mappedMovies.get(emr_fault_vo.getCreater());
                if (userInfo==null){
                    emr_fault_vo.setCreater(emr_fault_vo.getCreater());
                }else {
                    emr_fault_vo.setCreater(userInfo.getString("name"));
                }
            }

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

