/**
 * 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.emr.dao.Sign_ListMapper;
import com.emr.entity.*;
import com.emr.service.Emr_DictionaryService;
import com.emr.service.PowerUserService;
import com.emr.service.Sign_ListService;
import com.emr.util.ListUtil;
import com.emr.util.Setters;
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.util.CollectionUtils;

import java.util.*;

@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) {
            //获取科室列表
            Emr_Dictionary dic = new Emr_Dictionary();
            dic.setEffective(1);
            dic.setTypecode("dept_code");
            List<Emr_Dictionary> dicList = emrDictionaryService.dicByTypeCode(dic);
            //转换科室
            Map<String, Emr_Dictionary> codeMap = ListUtil.toMap(dicList, Emr_Dictionary::getCode);
            Setters.<Sign_List_Vo>instance().list(list).cycleSetProperties(p -> {
                String deptCode = p.getDeptName();
                String deptAdmissionCode = p.getDeptAdmissionTo();
                if(StringUtils.isNotBlank(deptCode) && codeMap.containsKey(deptCode)){
                    String deptName = codeMap.get(deptCode).getName();
                    p.setDeptName(deptName);
                }
                if(StringUtils.isNotBlank(deptAdmissionCode) && codeMap.containsKey(deptAdmissionCode)){
                    String deptAdmissionTo = codeMap.get(deptAdmissionCode).getName();
                    p.setDeptAdmissionTo(deptAdmissionTo);
                }
            });
            //查询用户集合
            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);
                            break;
                        }
                    }
                }
            }
            //查询签收次数
            //masterIds集合
            List<String> masterIds = ListUtil.distinctSelect(list, Sign_List_Vo::getMasterID);
            //转成逗号拼接
            String ids = String.join(",", masterIds);
            List<Sign_List_Vo> signNum = signListMapper.getSignNum(ids);
            //遍历签收次数
            Map<String, Sign_List_Vo> signNumMap = ListUtil.toMap(signNum, Sign_List_Vo::getMasterID);
            Setters.<Sign_List_Vo>instance().list(list).cycleSetProperties(p -> {
                String masterID = p.getMasterID();
                if (signNumMap.containsKey(masterID)){
                    String num = signNumMap.get(masterID).getNum();
                    p.setNum(num);
                }
            });
        }
        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(String 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(SignList signList, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<SignList> list = selSignByColm1(signList);
        return new OffsetLimitPage((Page) list);
    }


    @Override
    public List<SignList> selSignByColm1(SignList signList) {
        List<SignList> list = signListMapper.selSignByColm1(signList);
        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 (SignList 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);
                        }
                    }
                }
                String id = signObj.getId();
                signObj.setIdtile(id);
            }
        }
        return list;
    }


}
 

