package com.emr.service.BatchApplyDetails;

import com.emr.dao.*;
import com.emr.dao.approve.Emr_Apply_ApproveMapper;
import com.emr.dto.FastBorrowingDto;
import com.emr.entity.*;
import com.emr.entity.approve.Emr_Apply_Approve;
import com.emr.service.CommomService;
import com.emr.service.approve.ApplyApproveService;
import com.emr.util.ListUtils;
import com.emr.util.Setters;
import com.emr.vo.*;
import com.emr.vo.commomSearch.CommomVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName BatchApplyServiceImpl
 * @Description 批量借阅实现类
 * @Author linjj
 * @Date 2023/8/28 17:10
 * @Version 1.0
 */
@Service
public class BatchApplyDetailsServiceImpl implements BatchApplyDetailsService {
    @Autowired
    BatchApplyMapper batchApplyMapper;
    @Autowired
    Emr_DictionaryMapper emrDictionaryMapper;
    @Autowired
    BatchApplyDetailsMapper batchApplyDetailsMapper;
    @Autowired
    EmrComomSetMapper emrComomSetMapper;
    @Autowired
    Archive_MasterMapper archiveMasterMapper;
    @Autowired
    Emr_Apply_ApproveMapper emrApplyApproveMapper;
    @Autowired
    private Emr_Apply_ApproveMapper applyApproveMapper;
    @Autowired
    private CommomMapper commomMapper;
    @Value("${applyApproveFlag}")
    private String applyApproveFlag;
    @Autowired
    private ApplyApproveService applyApproveService;

    @Autowired
    private CommomService commomService;

    @Override
    public List<CommomVo> addCommomVo(CommomVo commomVo) {
        List<CommomVo> list = batchApplyMapper.getCommomVo(commomVo);
        //转换科室
        convertDepartment(list);
        //当查询集合中只有一条记录时直接插入申请列表
        if (commomVo.getRefreshTable() == 1) {
            //判断是否已经存在并且未过期
            int i = batchApplyDetailsMapper.getNumInpNoAndAdmissTimes(list.get(0).getInpatientNo(), list.get(0).getAdmissTimes(), commomVo.getBatchId());
            if (i == 0) {
                addApplyDetails(list, commomVo.getBatchId(), commomVo.getApplyName());
            } else {
                list.get(0).setApplyStatc("住院号:" + list.get(0).getInpatientNo() + "住院次数:" + list.get(0).getAdmissTimes() + "已经申请且在有效期内,无需重复申请！！！");
            }
        }
        return list;
    }

    @Override
    public List<BatchApplyDetails> getBatchApplyDetails(int batchId, String inpatientNo) {
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String format = fmt.format(new Date());
        List<BatchApplyDetails> batchApplyDetails = batchApplyDetailsMapper.getBatchApplyDetails(batchId, inpatientNo);
        for (BatchApplyDetails list : batchApplyDetails) {
            //判断是否过期如果过期状态为4
            String effectiveTime = list.getEffectiveTime();
            if (StringUtils.isNotBlank(effectiveTime)) {
                if ((effectiveTime.compareTo(format)) == -1) {
                    list.setApplyStatc(4);
                }
            }
        }
        return batchApplyDetails;
    }

    @Override
    public Msg addBatchBorrowing(BatchBorrowingVo batchBorrowingVo) {
        //查询患者基础数据
        List<CommomVo> commomList = batchApplyMapper.getCommomVoByPatientIds(batchBorrowingVo.getPatientIds());
        //判断是否已经存在并且未过期
        for (CommomVo list : commomList) {
            int i = batchApplyDetailsMapper.getNumInpNoAndAdmissTimes(list.getInpatientNo(), list.getAdmissTimes(), batchBorrowingVo.getBatchId());
            if (i > 0) {
                return Msg.addBatchFail("住院号:" + list.getInpatientNo() + "住院次数:" + list.getAdmissTimes() + "已经申请且在有效期内,无需重复申请！！！");
            }
        }
        int i = addApplyDetailsList(commomList, batchBorrowingVo);
        if (i == 1) {
            return Msg.addBatchSuccess("批量申请保存成功");
        }
        return Msg.addBatchFail("批量申请保存失败");
    }

    @Override
    public int deleteById(int id, int batchId) {
        //更新批次数量
        delBorrowingNum(batchId);
        return batchApplyDetailsMapper.deleteById(id);
    }

    @Override
    public int rejectById(int id) {
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        BatchApplyDetailsVo batchApplyDetailsVo = new BatchApplyDetailsVo();
        batchApplyDetailsVo.setExamineName(user.getUserName());
        batchApplyDetailsVo.setExamineNameString(user.getUserPosition());
        batchApplyDetailsVo.setExamineTime(fmt.format(new Date()));
        batchApplyDetailsVo.setId(id);
        batchApplyDetailsVo.setApplyStatc(3);
        return batchApplyDetailsMapper.upadteApplyDetails(batchApplyDetailsVo);
    }

    @Override
    public int agreeById(String ids, Integer batchId) {
        int i = 0;
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        //声明一个BatchApplyDetailsVo集合
        List<BatchApplyDetailsVo> list = Collections.synchronizedList(new ArrayList<>());
        //将字符串转为List<String>在转成List<Integer>
        List<Integer> idsList = Arrays.asList(ids.split(",")).stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
        //遍历审核信息
        for (Integer id : idsList) {
            BatchApplyDetailsVo batchApplyDetailsVo = new BatchApplyDetailsVo();
            batchApplyDetailsVo.setExamineName(user.getUserName());
            batchApplyDetailsVo.setExamineNameString(user.getUserPosition());
            batchApplyDetailsVo.setExamineTime(fmt.format(new Date()));
            batchApplyDetailsVo.setApplyStatc(1);
            batchApplyDetailsVo.setId(id);
            list.add(batchApplyDetailsVo);
        }
        //给有效时间赋值
        upDateEffectiveTimeList(list);
        boolean b = upadteApplyDetais(list);
        if (b) {
            i = 1;
        }
        //更新审批数量
        upDateAgreeNum(batchId, list.size());
        return i;
    }

    @Override
    public int updateReturn(String barCode, String barCodeName) {
        int i = 0;
        Power_User user = new Power_User();
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //不为空直接用barCodeName为空拿当前登录userName
        if (StringUtils.isNotBlank(barCodeName)) {
            user.setUserName(barCodeName);
            String s = convertUserName(user.getUserName());
            if (StringUtils.isBlank(s)) {
                return i;
            }
            user.setUserPosition(s);
        } else {
            // 从session获取用户名
            user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        }
        List<UpdateReturnVo> inpNoAndVisitIdByBarCode = archiveMasterMapper.getInpNoAndVisitIdByBarCode(barCode);
        //集合为空则返回2无需要归还病历
        if (CollectionUtils.isEmpty(inpNoAndVisitIdByBarCode)) {
            i = 2;
            return i;
        }
        //查询是否有需要归还病历
        for (UpdateReturnVo list : inpNoAndVisitIdByBarCode) {
            List<ApplyStateVo> ids = emrApplyApproveMapper.getReturnState(list.getInpNo(), list.getVisitId(), user.getUserName());
            //集合为空则返回2无需要归还病历
            if (CollectionUtils.isEmpty(ids)) {
                i = 2;
                return i;
            }
            //需要归还病历id
            ApplyStateVo applyStateVo = ids.get(ids.size() - 1);
            //如果最后一条记录是已归还则不需要在次归还
            if (StringUtils.isNotBlank(applyStateVo.getReturnState())) {
                i = 2;
                return i;
            }
            i = emrApplyApproveMapper.UpdateReturnState(applyStateVo.getId(), user.getUserName(), fmt.format(new Date()), user.getUserPosition());
            return i;
        }
        return i;
    }

    @Override
    public Msg addFastBorrowing(FastBorrowingDto dto) {
        Power_User user = new Power_User();
        //保存实体
        Emr_Apply_Approve applyApprove = new Emr_Apply_Approve();
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (StringUtils.isNotBlank(dto.getUserName())) {
            user.setUserName(dto.getUserName());
        } else {
            // 从session获取用户名
            user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        }
        //查询是否未过期
        //根据条形码查询信息
        List<UpdateReturnVo> list = archiveMasterMapper.getInpNoAndVisitIdByBarCode(dto.getBarCode());
        //集合为空则返回无该条形码病历
        if (CollectionUtils.isEmpty(list)) {
            return Msg.BorrowingFail("无该条形码病历");
        }
        //查询patientId
        String patientId = commomMapper.getPatientIdByInpNoAndVisitId(list.get(0).getInpNo(), list.get(0).getVisitId());
        if (StringUtils.isBlank(patientId)) {
            return Msg.BorrowingFail("无该条形码已归档病历");
        }
        //判断是否有未过期切未归还病历
        List<Emr_Apply_Approve> approves = applyApproveMapper.getApproverState(list.get(0).getInpNo(), list.get(0).getVisitId(), user.getUserName());
        if (!CollectionUtils.isEmpty(approves)) {
            return Msg.BorrowingFail("该病案另一个申请还处于有效期，无须重复申请!");
        }
        //有效期是否过滤节假日
        if (applyApproveFlag.equals("1")) {
            SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-MM-dd");
            List<String> expireTimeList = applyApproveService.expireTime(fmt1.format(new Date()), applyApprove.getEffeDays());
            String s = expireTimeList.get(expireTimeList.size() - 1);
            applyApprove.setEffeTime(s);
        } else {
            Calendar calendar2 = Calendar.getInstance();
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            calendar2.add(Calendar.DATE, 3);
            applyApprove.setEffeTime(sdf2.format(calendar2.getTime()));
        }
        applyApprove.setApplyer(user.getUserName());
        applyApprove.setApplyTime(fmt.format(new Date()));
        applyApprove.setEffeDays(dto.getEffeDays());
        applyApprove.setApplyType(dto.getApplyType());
        applyApprove.setApplyReason(dto.getApplyReason());
        applyApprove.setApplyState("2");
        applyApprove.setPatientId(patientId);
        applyApprove.setAdmissId(list.get(0).getInpNo());
        applyApprove.setInpatientNo(list.get(0).getInpNo());
        applyApprove.setAdmissTimes(Short.parseShort(list.get(0).getVisitId()));
        applyApprove.setDisDate(list.get(0).getDischargeDateTime());
        int i = applyApproveMapper.insertSelective(applyApprove);
        if (i == 1) {
            return Msg.success("条形码" + dto.getBarCode() + "快速借阅成功");
        }
        return Msg.BorrowingFail("条形码" + dto.getBarCode() + "快速借阅失败");
    }

    @Override
    public Msg updateBatchApply(String barCode, String barCodeName, int batchId) {
        //转换用户名
        String userName = convertUserName(barCodeName);
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        if (StringUtils.isBlank(userName)){
            return Msg.BorrowingFail("该工号:" + barCodeName + "查询不到对应用户名");
        }
        //根据条形码查询病历
        List<UpdateReturnVo> list = archiveMasterMapper.getInpNoAndVisitIdByBarCode(barCode);
        //集合为空则未查询到该条形码病历
        if (CollectionUtils.isEmpty(list)) {
            return Msg.BorrowingFail("未查询到该条形码病历");
        }
        //根据条形码查询批量表需要归还病历
        List<String> ids = batchApplyDetailsMapper.getIdInpNoAndAdmissTimes(list.get(0).getInpNo(), list.get(0).getVisitId(), batchId);
        if (CollectionUtils.isEmpty(ids)) {
            return Msg.BorrowingFail("该条形码:" + barCode + "无需要归还的病历");
        }
        int i = batchApplyDetailsMapper.updateBatchApplyDetails(ids.get(ids.size() - 1), userName, barCodeName, fmt.format(new Date()));
        if (i==1){
            return Msg.success("该条形码:" + barCode + "快速归还成功");
        }
        return Msg.BorrowingFail("该条形码:" + barCode + "快速归还失败");
    }

    @Override
    public Msg fastBatchApply(FastBorrowingDto dto) {
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        List<BatchApplyDetailsVo> NewList = Collections.synchronizedList(new ArrayList<>());
        //根据条形码查询信息
        List<UpdateReturnVo> updateReturnVo = archiveMasterMapper.getInpNoAndVisitIdByBarCode(dto.getBarCode());
        //集合为空则返回无该条形码病历
        if (CollectionUtils.isEmpty(updateReturnVo)) {
            return Msg.BorrowingFail("无该条形码病历");
        }
        //判断是否已经借阅
        for (UpdateReturnVo list:updateReturnVo){
            int i = batchApplyDetailsMapper.getNumInpNoAndAdmissTimes(list.getInpNo(), Integer.parseInt(list.getVisitId()), dto.getBatchId());
            if (i > 0) {
                return Msg.addBatchFail("住院号:" + list.getInpNo() + "住院次数:" + list.getVisitId() + "已经申请且在有效期内,无需重复申请！！！");
            }
            //插入申请借阅详情集合
            BatchApplyDetailsVo batchApplyDetailsVo = new BatchApplyDetailsVo();
            batchApplyDetailsVo.setBatchId(dto.getBatchId());
            batchApplyDetailsVo.setApplyName(dto.getUserName());
            batchApplyDetailsVo.setApplyDate(user.getRemark());
            batchApplyDetailsVo.setApplyTime(fmt.format(new Date()));
            batchApplyDetailsVo.setEffectiveDay(dto.getEffeDays());
            batchApplyDetailsVo.setInpatientNo(list.getInpNo());
            batchApplyDetailsVo.setAdmissTimes(Short.parseShort(list.getVisitId()));
            batchApplyDetailsVo.setPatientName(list.getName());
            batchApplyDetailsVo.setDisTime(list.getDischargeDateTime().toString());
            batchApplyDetailsVo.setApplyReason(dto.getApplyReason());
            batchApplyDetailsVo.setApplyFlag(2);
            batchApplyDetailsVo.setApplyStatc(2);
            batchApplyDetailsVo.setHandlingName(user.getUserName());
            batchApplyDetailsVo.setHandlingNameString(user.getUserPosition());
            //将对象传入集合
            NewList.add(batchApplyDetailsVo);
        }
        //同步数量
        updateNum(dto.getBatchId(), NewList.size());
        int i = batchApplyDetailsMapper.addBatchApplyDetails(NewList);
        if (i==1){
            return Msg.success("条形码:"+dto.getBarCode()+"快速借阅成功");
        }
        return Msg.BorrowingFail("条形码:"+dto.getBarCode()+"快速借阅成功");
    }

    private void upDateAgreeNum(int batchId, int readNum) {
        BatchApplyVo batchApplyVo = batchApplyMapper.getreadNum(batchId);
        int i = readNum + batchApplyVo.getReadNum();
        batchApplyMapper.updateReadNumm(i, batchId);
//        //如果借阅数量跟审批数量一样则改为已审批
        if (batchApplyVo.getBorrowingNum() == i) {
            batchApplyMapper.updateStatc(2, batchId);
        } else {
            batchApplyMapper.updateStatc(3, batchId);
        }

    }

    private boolean upadteApplyDetais(List<BatchApplyDetailsVo> list) {
        boolean flag = false;
        for (BatchApplyDetailsVo batchApplyDetailsVo : list) {
            batchApplyDetailsMapper.upadteApplyDetails(batchApplyDetailsVo);
            flag = true;
        }
        return flag;
    }

    private void upDateEffectiveTimeList(List<BatchApplyDetailsVo> batchApplyDetailsVolist) {
        for (BatchApplyDetailsVo list : batchApplyDetailsVolist) {
            String effectiveTime = batchApplyDetailsMapper.getEffectiveTime(list.getId(), list.getExamineTime());
            list.setEffectiveTime(effectiveTime);
        }
    }

    private int addApplyDetailsList(List<CommomVo> commomList, BatchBorrowingVo batchBorrowingVo) {
        //获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        List<BatchApplyDetailsVo> list = Collections.synchronizedList(new ArrayList<>());
        for (CommomVo commomInfoList : commomList) {
            //插入申请借阅详情集合
            BatchApplyDetailsVo batchApplyDetailsVo = new BatchApplyDetailsVo();
            batchApplyDetailsVo.setBatchId(batchBorrowingVo.getBatchId());
            batchApplyDetailsVo.setApplyName(batchBorrowingVo.getApplyName());
            batchApplyDetailsVo.setApplyDate(user.getRemark());
            batchApplyDetailsVo.setApplyTime(fmt.format(new Date()));
            batchApplyDetailsVo.setEffectiveDay(batchBorrowingVo.getEffectiveDay());
            batchApplyDetailsVo.setInpatientNo(commomInfoList.getInpatientNo());
            batchApplyDetailsVo.setAdmissTimes(commomInfoList.getAdmissTimes());
            batchApplyDetailsVo.setPatientName(commomInfoList.getName());
            batchApplyDetailsVo.setDisTime(commomInfoList.getDisDate());
            batchApplyDetailsVo.setApplyReason(batchBorrowingVo.getApplyReason());
            batchApplyDetailsVo.setApplyFlag(batchBorrowingVo.getApplyFlag());
            batchApplyDetailsVo.setApplyStatc(2);
            batchApplyDetailsVo.setHandlingName(user.getUserName());
            batchApplyDetailsVo.setHandlingNameString(user.getUserPosition());
            //将对象传入集合
            list.add(batchApplyDetailsVo);
        }
        //同步数量
        updateNum(batchBorrowingVo.getBatchId(), list.size());
        return batchApplyDetailsMapper.addBatchApplyDetails(list);
    }

    /**
     * @description: 转换科室方法
     * @author linjj
     * @date: 2023/9/4 14:18
     */
    private void convertDepartment(List<CommomVo> list) {
        //获取科室列表
        List<Emr_Dictionary> dicList = emrDictionaryMapper.selectDictionaryByTypeCode("dept_code");
        //转换科室
        Map<String, Emr_Dictionary> codeMap = ListUtils.toMap(dicList, Emr_Dictionary::getCode);
        Setters.<CommomVo>instance().list(list).cycleSetProperties(p -> {
            String deptCode = p.getDisDept();
            if (StringUtils.isNotBlank(deptCode) && codeMap.containsKey(deptCode)) {
                String deptName = codeMap.get(deptCode).getName();
                p.setDisDept(deptName);
            }
        });
    }


    private void addApplyDetails(List<CommomVo> list, int batchId, String ApplyName) {
        //获取系统设置的默认参数
        EmrComomSet emrComomSet = emrComomSetMapper.selectByPrimaryKey(1);
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        if (list.size() == 1) {
            BatchApplyDetailsVo batchApplyDetailsVo = new BatchApplyDetailsVo();
            batchApplyDetailsVo.setBatchId(batchId);
            batchApplyDetailsVo.setApplyName(ApplyName);
            batchApplyDetailsVo.setApplyDate(user.getRemark());
            //获取系统当前时间
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            batchApplyDetailsVo.setApplyTime(fmt.format(new Date()));
            batchApplyDetailsVo.setEffectiveDay(emrComomSet.getDefaultDay());
            batchApplyDetailsVo.setInpatientNo(list.get(0).getInpatientNo());
            batchApplyDetailsVo.setAdmissTimes(list.get(0).getAdmissTimes());
            batchApplyDetailsVo.setPatientName(list.get(0).getName());
            batchApplyDetailsVo.setHandlingName(user.getUserName());
            batchApplyDetailsVo.setDisTime(list.get(0).getDisDate());
            batchApplyDetailsVo.setApplyReason("需要借阅这份病历");
            batchApplyDetailsVo.setApplyFlag(1);
            batchApplyDetailsVo.setApplyStatc(2);
            batchApplyDetailsVo.setHandlingNameString(user.getUserPosition());
            List<BatchApplyDetailsVo> batchApplyDetailsList = Arrays.asList(batchApplyDetailsVo);
            batchApplyDetailsMapper.addBatchApplyDetails(batchApplyDetailsList);
            //新增时同步批次数量先获取原先数量
            updateBorrowingNum(batchId);
        }
    }

    private void updateBorrowingNum(int batchId) {
        BatchApplyVo batchApplyVo = batchApplyMapper.getBorrowingNum(batchId);
        Integer borrowingNum = batchApplyVo.getBorrowingNum();
        int i = borrowingNum + 1;
        batchApplyMapper.updateBorrowingNum(i, batchId);
    }


    private void delBorrowingNum(int batchId) {
        BatchApplyVo batchApplyVo = batchApplyMapper.getBorrowingNum(batchId);
        Integer borrowingNum = batchApplyVo.getBorrowingNum();
        int i = borrowingNum - 1;
        batchApplyMapper.updateBorrowingNum(i, batchId);
    }


    private void updateNum(int batchId, int listNum) {
        BatchApplyVo batchApplyVo = batchApplyMapper.getBorrowingNum(batchId);
        Integer borrowingNum = batchApplyVo.getBorrowingNum();
        int i = borrowingNum + listNum;
        batchApplyMapper.updateBorrowingNum(i, batchId);
    }

    //转换用户名
    private String convertUserName(String userName) {
        String userNameString = null;
        //获取登录用户信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //获取用户名
        List<User> userList = null;
        try {
            userList = commomService.getUserList(userName, request);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        for (User user1 : userList) {
            String name = user1.getName();
            if (StringUtils.isNotBlank(userName) && user1.getUserName().equals(userName)) {
                userNameString = name;
            }
        }
        return userNameString;
    }

}
