package com.docus.server.archive.service.impl;

import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.archive.config.DocusProperties;
import com.docus.server.archive.constans.SyncConstant;
import com.docus.server.archive.converter.YdZyyUserInfoConverter;
import com.docus.server.archive.entity.PowerUser;
import com.docus.server.archive.mapper.PowerUserMapper;
import com.docus.server.archive.service.UserInfoSyncService;
import com.docus.server.ydzyy.entity.YdZyyUserInfoView;
import com.docus.server.ydzyy.mapper.YdZyyUserInfoViewMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 梅州三院用户数据同步
 *
 * @author YongBin Wen
 * @date 2024/3/27 16:25
 */
@Service("YdZyy" + SyncConstant.USER_INFO_SYNC_SERVICE_SUFFIX)
@Slf4j
public class YdZyyUserInfoSyncServiceImpl implements UserInfoSyncService {
    @Resource
    private PowerUserMapper powerUserMapper;
    @Resource
    private YdZyyUserInfoViewMapper userInfoViewMapper;

    @Resource
    private IdService idService;
    @Resource
    private DocusProperties docusProperties;

    @Override
    public void fullSync() {
        int size = 2000;
        int pageNumber = 1;

        while (true) {
            int startRow = ((pageNumber - 1) * size) + 1;
            int endRow = pageNumber * size;
            List<YdZyyUserInfoView> userInfoViews = userInfoViewMapper.page(startRow, endRow);
            if (Func.isNotEmpty(userInfoViews)) {
                // 转换保存
                List<PowerUser> powerUsers = userInfoViews.stream()
                        .map(YdZyyUserInfoConverter::convertUserInfo)
                        .collect(Collectors.toList());
                saveOrUpdate(powerUsers);
                // 英德中医院可能存第三方的数据 暂时不处理
            }
            if (Func.isEmpty(userInfoViews) || userInfoViews.size() < size) {
                break;
            }
            pageNumber++;
        }
    }

    /**
     * 根据修改的用户信息，根据工号判断是否存在，如果存在则更新名字，科室 状态等信息，如果不存在则插入用户信息
     *
     * @param iuPowerUserList 修改的用户信息
     */
    private void saveOrUpdate(List<PowerUser> iuPowerUserList) {
        Date nowDate = new Date();
        List<String> iuUserNames = iuPowerUserList.stream()
                .map(PowerUser::getUserName)
                .collect(Collectors.toList());

        List<PowerUser> existsPowerUserList = powerUserMapper.findByUserNames(iuUserNames);
        Map<String, PowerUser> existsPowerUserMap = existsPowerUserList.stream().collect(Collectors.toMap(PowerUser::getUserName, Function.identity()));
        final String delimiter = ",";

        List<PowerUser> insertUserList = new ArrayList<>();
        List<PowerUser> updateUserList = new ArrayList<>();
        for (PowerUser iuPowerUser : iuPowerUserList) {
            if (existsPowerUserMap.containsKey(iuPowerUser.getUserName())) {
                PowerUser powerUser = existsPowerUserMap.get(iuPowerUser.getUserName());
                powerUser.setName(iuPowerUser.getName());
                powerUser.setEnabled(iuPowerUser.getEnabled());
                powerUser.setEffective(iuPowerUser.getEffective());
                powerUser.setUpdateDate(nowDate);
                powerUser.setUpdater("java-sync");
                updateUserList.add(powerUser);
                continue;
            }
            iuPowerUser.setUserId(idService.getDateSeq());
            iuPowerUser.setUpdater("java-sync");
            iuPowerUser.setUpdateDate(nowDate);
            iuPowerUser.setCreater("java-sync");
            iuPowerUser.setCreateDate(nowDate);
            iuPowerUser.setAccountState(0);
            iuPowerUser.setIsThird(0);
            iuPowerUser.setLoginFlag(0);
            iuPowerUser.setPwdChange(0);
            iuPowerUser.setUserPwd(docusProperties.getDefaultUserPwd());
            insertUserList.add(iuPowerUser);
        }

        if (Func.isNotEmpty(insertUserList)) {
            powerUserMapper.insertBatch(insertUserList);
        }

        if (Func.isNotEmpty(updateUserList)) {
            powerUserMapper.updateBatch(updateUserList);
        }
    }
}
