package com.willalp.files.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.core.domain.entity.SysDept;
import com.willalp.common.core.domain.entity.SysDictData;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.files.domain.HsOrgPersonnelFiles;
import com.willalp.files.mapper.HsOrgPersonnelFilesMapper;
import com.willalp.files.service.IHsOrgPersonnelFilesService;
import com.willalp.system.domain.SysDeptPost;
import com.willalp.system.service.ISysDeptPostService;
import com.willalp.system.service.ISysDeptService;
import com.willalp.system.service.ISysDictDataService;
import com.willalp.system.service.ISysDictTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 机构人员档案Service业务层处理
 *
 * @author willalp
 * @date 2022-08-01
 */
@Service
public class HsOrgPersonnelFilesServiceImpl extends ServiceImpl<HsOrgPersonnelFilesMapper, HsOrgPersonnelFiles> implements IHsOrgPersonnelFilesService {
    @Resource
    private HsOrgPersonnelFilesMapper hsOrgPersonnelFilesMapper;

    @Resource
    private ISysDictDataService dictDataService;
    @Resource
    private ISysDictTypeService dictTypeService;

    @Resource
    private ISysDeptService deptService;

    /**
     * 查询机构人员档案列表
     *
     * @param hsOrgPersonnelFiles 机构人员档案
     * @return 机构人员档案
     */
    @Override
    public List<HsOrgPersonnelFiles> selectHsOrgPersonnelFilesList(HsOrgPersonnelFiles hsOrgPersonnelFiles) {
        return hsOrgPersonnelFilesMapper.selectHsOrgPersonnelFilesList(hsOrgPersonnelFiles);
    }

    @Override
    public List<HsOrgPersonnelFiles> importHsOrgPersonnelFilesList(HsOrgPersonnelFiles hsOrgPersonnelFiles) {
        List<HsOrgPersonnelFiles> dbList = hsOrgPersonnelFilesMapper.selectHsOrgPersonnelFilesList(hsOrgPersonnelFiles);
        List<SysDictData> jobTitleData = dictTypeService.selectDictDataByType("job_title");
        dbList.forEach(dbItem -> {
            for (SysDictData item : jobTitleData) {
                if (StringUtils.isNotBlank(dbItem.getJobTitle())
                        && dbItem.getJobTitle().equals(item.getDictValue())) {
                    dbItem.setJobTitle(item.getDictLabel());
                    break;
                }
            }
        });
        return dbList;
    }

    @Override
    public String importData(List<HsOrgPersonnelFiles> list) {
        List<HsOrgPersonnelFiles> dbList = this.selectHsOrgPersonnelFilesList(null);
        List<SysDictData> jobTitleData = dictTypeService.selectDictDataByType("job_title");
        List<HsOrgPersonnelFiles> savaList = new ArrayList<>();
        List<HsOrgPersonnelFiles> updateList = new ArrayList<>();
        if (0 != list.size()) {
            if (0 != dbList.size()) {
                dbList.forEach(dbItem -> {
                    int i = 0;
                    for (HsOrgPersonnelFiles hsOrgPersonnelFiles : list) {
                        String jobTitle = hsOrgPersonnelFiles.getJobTitle();
                        if (StringUtils.isNotBlank(jobTitle)) {
                            if (0 != jobTitleData.size()) {
                                List<SysDictData> collect = jobTitleData.stream()
                                        .filter(item -> null != item.getDictLabel() && item.getDictLabel().equals(jobTitle))
                                        .collect(Collectors.toList());
                                if (0 != collect.size()) {
                                    hsOrgPersonnelFiles.setJobTitle(collect.get(0).getDictValue());
                                }
                            }
                        }
                        /*String department = hsOrgPersonnelFiles.getDepartment();
                          if (StringUtils.isNotBlank(department)) {
                              SysDept sysDept = deptService.selectDeptById(Long.valueOf(department));
                              if (null != sysDept) {
                                  hsOrgPersonnelFiles.setDepartment(sysDept.getDeptName());
                              }
                          }*/
                        if (StringUtils.isNotBlank(hsOrgPersonnelFiles.getUserCode())
                                && hsOrgPersonnelFiles.getUserCode().equals(dbItem.getUserCode())) {
                            hsOrgPersonnelFiles.setId(dbItem.getId());
                            hsOrgPersonnelFiles.setDepartment(dbItem.getDepartment());
                            updateList.add(hsOrgPersonnelFiles);
                            break;
                        } else if (i == list.size()) {
                            savaList.add(hsOrgPersonnelFiles);
                        }
                        i++;
                    }
                });
            }
            if (0 != savaList.size()) {
                if (!saveBatch(savaList)) {
                    throw new BaseException("数据导入失败");
                }
            }
            if (0 != updateList.size()) {
                if (!updateBatchById(updateList)) {
                    throw new BaseException("数据更新失败");
                }
            }
        }
        return "共导入【" + savaList.size() + "】条新数据，更新【" + updateList.size() + "】条数据";
    }


    @Resource
    private ISysDeptService sysDeptService;

    @Resource
    private ISysDeptPostService sysDeptPostService;

    @Override
    public HsOrgPersonnelFiles appGetHsOrgPersonnelFilesByPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            throw new BaseException("手机号为空，请添加档案信息");
        }
        HsOrgPersonnelFiles orgPersonnelFiles = this.getBaseMapper().selectOne(new QueryWrapper<HsOrgPersonnelFiles>()
                .eq("phone", phone));
        if (orgPersonnelFiles == null) {
            throw new BaseException("暂未查询到该用户名片信息");
        }
        //补充部门信息
        String department = orgPersonnelFiles.getDepartment();
        if (StringUtils.isNotBlank(department)) {
            SysDept sysDept = sysDeptService.selectDeptById(Long.valueOf(department));
            orgPersonnelFiles.setDepartment(sysDept.getDeptName());
        } else {
            orgPersonnelFiles.setDepartment("暂无");
        }
        //补充岗位信息
        String post = orgPersonnelFiles.getPost();
        if (StringUtils.isNotBlank(post)) {
            SysDeptPost sysDeptPost = sysDeptPostService.selectById(Long.valueOf(post));
            orgPersonnelFiles.setPost(sysDeptPost.getPostName());
        } else {
            orgPersonnelFiles.setPost("暂无");
        }
        String seatNo = orgPersonnelFiles.getSeatNo();
        if (StringUtils.isBlank(seatNo)) {
            orgPersonnelFiles.setSeatNo("暂无");
        }
        String email = orgPersonnelFiles.getEmail();
        if (StringUtils.isBlank(email)) {
            orgPersonnelFiles.setEmail("暂无");
        }
        return orgPersonnelFiles;
    }

    @Override
    public Map<String, Object> flowGetFilesById(String id) {
        return baseMapper.flowGetFilesById(id);
    }
}
