package com.willalp.addressbook.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.willalp.addressbook.domain.AddressBook;
import com.willalp.addressbook.service.IHsAddressBookService;
import com.willalp.common.core.domain.entity.SysDept;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.ChineseCharacterUtil;
import com.willalp.common.utils.StringUtils;
import com.willalp.files.domain.HsOrgPersonnelFiles;
import com.willalp.files.service.IHsOrgPersonnelFilesService;
import com.willalp.system.domain.resp.OrganizationListResp;
import com.willalp.system.service.ISysDeptService;
import com.willalp.system.service.ISysUserOrganizationService;
import com.willalp.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HsAddressBookServiceImpl implements IHsAddressBookService {

    @Resource
    private ISysUserService userService;

    @Resource
    private IHsOrgPersonnelFilesService orgPersonnelFilesService;

    @Resource
    private ISysDeptService sysDeptService;

    @Resource
    private ISysUserOrganizationService userOrganizationService;

    @Override
    public List<AddressBook> abList(String department) {
        List<HsOrgPersonnelFiles> department1;
        if (StringUtils.isBlank(department)) {
            throw new BaseException("部门编号为空");
        } else if (StringUtils.equals("all", department)) {
            department1 = orgPersonnelFilesService.getBaseMapper().selectList(null);
        } else {
            department1 = orgPersonnelFilesService.getBaseMapper().selectList(
                    new QueryWrapper<HsOrgPersonnelFiles>()
                            .eq("department", department));
        }
        return this.toAb(department1);
    }

    @Override
    public List<SysDept> institutionList(String un, String oc) {
        if (StringUtils.isBlank(un)) {
            throw new BaseException("用户编号为空无法验证");
        }
        if (StringUtils.isBlank(oc)) {
            throw new BaseException("机构编号为空无法验证");
        }
        List<OrganizationListResp> oByUList = userOrganizationService.getOrganizationByUserName(un);
        SysDept sysDept = sysDeptService.queryAbOrganizationInfoByDeptCode(oc);
        long count = oByUList.stream().filter(item -> item.getOrganizationCode().equals(sysDept.getDeptCode())).count();
        List<SysDept> sysDepts;
        List<SysDept> sysDeptsCopy = new ArrayList<>();
        if (count > 0) {
            sysDepts = sysDeptService.selectChildrenDeptById(sysDept.getDeptId());
            if (0 != sysDepts.size()) {
                sysDepts.forEach(item -> {
                    if (sysDept.getDeptId().equals(item.getParentId())) {
                        sysDeptsCopy.add(item);
                    }
                });
            }
        }
        return sysDeptsCopy;
    }

    @Override
    public List<AddressBook> groupAbList(String data) {
        String[] split = data.split(",");
        if (0 == split.length) {
            throw new BaseException("该群组暂无数据");
        }
        List<HsOrgPersonnelFiles> orgPersonnelFiles = orgPersonnelFilesService.getBaseMapper().selectList(
                new QueryWrapper<HsOrgPersonnelFiles>().in("user_code", (Object[]) split));
        return this.toAb(orgPersonnelFiles);
    }

    private List<AddressBook> toAb(List<HsOrgPersonnelFiles> orgPersonnelFiles) {
        List<AddressBook> list = new ArrayList<>();
        //使用一个map<字母，子集合>当作中转站减少逻辑判断
        Map<String, List<AddressBook.AddressBookItem>> map = new HashMap<>();
        orgPersonnelFiles.forEach(u -> {
            String str = u.getUserName();
            if (StringUtils.isNotBlank(str)) {
                str = String.valueOf(str.charAt(0));
                //查看首字母
                String szm0 = ChineseCharacterUtil.convertHanzi2Pinyin(str, false).toUpperCase();
                //创建子对象
                AddressBook.AddressBookItem aa0 = new AddressBook.AddressBookItem();
                aa0.setName(u.getUserName());
                aa0.setPhone(u.getPhone());
                //aa0.setImgSrc(u.getAvatar());
                //写入到Map中
                List<AddressBook.AddressBookItem> addressBookItems = map.get(szm0);
                if (ObjectUtil.isEmpty(addressBookItems)) {
                    map.put(szm0, new ArrayList<>());
                    addressBookItems = map.get(szm0);
                }
                addressBookItems.add(aa0);
            }
        });
        //循环取出放入List
        map.forEach((k, v) -> list.add(new AddressBook(k, v)));
        return list.size() != 0 ? list : null;
    }
}
