/**
 * 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_MasterMapper;
import com.emr.dao.Sign_ListMapper;
import com.emr.entity.*;
import com.emr.service.Archive_MasterService;
import com.emr.service.Emr_DictionaryService;
import com.emr.service.PowerUserService;
import com.emr.service.Sign_ListService;
import com.emr.vo.Power_User_Dto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
public class Sign_ListServiceImpl implements Sign_ListService {

    @Autowired
    private Sign_ListMapper signListMapper;

    @Autowired
    private Emr_DictionaryService emrDictionaryService;

    @Autowired
    private PowerUserService powerUserService;

    @Override
    public OffsetLimitPage selectSignList(Sign_List_Vo signListVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Sign_List_Vo> list =signListMapper.selectSignList(signListVo);
        if(list!=null && list.size()>0){
            //查询用户集合
            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 (Sign_List_Vo sign_list_vo : list) {
                if (null != powerUserList) {
                    //获取签收人
                    String singer = sign_list_vo.getC1();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(singer) && powerUserDto.getUserName().equals(singer)) {
                            sign_list_vo.setC1(name);
                        }

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

    @Override
    public List<Sign_List_Vo> selectSignList(Sign_List_Vo signListVo) {
        List<Sign_List_Vo> list =signListMapper.selectSignList(signListVo);
        if(list!=null && list.size()>0){
            //查询用户集合
            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 (Sign_List_Vo sign_list_vo : list) {
                if (null != powerUserList) {
                    //获取签收人
                    String singer = sign_list_vo.getC1();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(singer) && powerUserDto.getUserName().equals(singer)) {
                            sign_list_vo.setC1(name);
                        }

                    }
                }
            }
        }
        return list;
    }

    @Override
    public  List<Sign_List> selectSignListWith(Sign_List signList) {
        return signListMapper.selectSignListWith(signList);
    }

    @Override
    public Sign_List selectSignListById(Integer id) {
        return signListMapper.selectSignListById(id);
    }

    @Override
    public int insertSignList(Sign_List signList) {
        return signListMapper.insertSignList(signList);
    }

    @Override
    public int updateSignList(Sign_List signList) {
        return signListMapper.updateSignList(signList);
    }

    @Override
    public int deleteSignListById(Integer id) {
        return signListMapper.deleteSignListById(id);
    }

    @Override
    public int deleteSignListByIds(String ids) {
        return signListMapper.deleteSignListByIds(ids);
    }

    @Override
    public List<Sign_List> selSignByColm(Sign_List_Vo signListVo) {
        List<Sign_List> list=signListMapper.selSignByColm(signListVo);
        if(list!=null && list.size()>0){
            //查询用户集合
            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 (Sign_List signObj : list) {
                if (null != powerUserList) {
                    //获取签收人
                    String singer = signObj.getC1();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(singer) && powerUserDto.getUserName().equals(singer)) {
                            signObj.setC1(name);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public OffsetLimitPage selSignByColm(Sign_List_Vo signListVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Sign_List> list= selSignByColm(signListVo);
        return new OffsetLimitPage((Page) list);
    }


}
 

