package com.yida.data.system.configure;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.constant.QywxServiceProviderConstant;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.EduQywxServiceProvider;
import com.yida.data.common.core.entity.system.EduQywxServiceProviderSchool;
import com.yida.data.common.core.utils.WxServiceProviderUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.system.service.EduQywxServiceProviderSchoolService;
import com.yida.data.system.service.EduQywxServiceProviderService;
import com.yida.data.system.service.IDeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RequiredArgsConstructor
@Component
@Order(10)
@Slf4j
public class InitializeRunner implements ApplicationRunner {

    private final RedisService redisService;
    private final WxServiceProviderUtil wxServiceProviderUtil;

    private final IDeptService deptService;
    private final EduQywxServiceProviderService eduQywxServiceProviderService;
    private final EduQywxServiceProviderSchoolService eduQywxServiceProviderSchoolService;

    @Override
    public void run(ApplicationArguments args) {
        log.info("初始化部门缓存");
        List<Dept> allDept = deptService.list();
        Map<Object, Object> items = new HashMap<>();

        for (Dept dept : allDept) {
            items.put(dept.getDeptId().toString(), dept);
        }
        redisService.hmset(CachePrefixConstant.SYS_DEPT_DATA, items);
//        initProviderSchool();
    }

    /**
     * 初始化服务商对应学校加密corpId
     *
     * @author ZYJ
     * @date 2022/11/10 9:13
     */
//    public void initProviderSchool() {
//        // 查询所有服务商
//        List<EduQywxServiceProvider> providerList = eduQywxServiceProviderService.list();
//        // 查询所有企业微信学校
//        List<Dept> deptList = deptService.list(Wrappers.lambdaQuery(new Dept()).eq(Dept::getSchoolType, Dept.TYPE_QYWX));
//        // 加密key缓存map
//        Map<Object, Object> items = new HashMap<>(16);
//        // 原始key缓存map
//        Map<Object, Object> originalItems = new HashMap<>(16);
//
//        // 处理关联数据
//        for (EduQywxServiceProvider provider : providerList) {
//            for (Dept dept : deptList) {
//                // 查询是否有关联数据
//                EduQywxServiceProviderSchool providerSchool = eduQywxServiceProviderSchoolService
//                        .getOne(Wrappers.lambdaQuery(new EduQywxServiceProviderSchool())
//                                .eq(EduQywxServiceProviderSchool::getProviderCorpId, provider.getCorpId())
//                                .eq(EduQywxServiceProviderSchool::getDeptOriginalCorpId, dept.getCorpId()));
//                if (Objects.isNull(providerSchool)) {
//                    continue;
//                }
//                if (StringUtils.isNotBlank(providerSchool.getDeptEncryptionCorpId())) {
//                    providerSchool = new EduQywxServiceProviderSchool();
//                    providerSchool.setProviderCorpId(provider.getCorpId());
//                    providerSchool.setDeptOriginalCorpId(dept.getCorpId());
//                    // 转换corpId
//                    String providerToken = wxServiceProviderUtil
//                            .getProviderToken(provider.getCorpId(), provider.getServiceProviderSecret());
//                    String convertCorpId = wxServiceProviderUtil.convertCorpId(providerToken, dept.getCorpId());
//                    providerSchool.setDeptEncryptionCorpId(convertCorpId);
//                    eduQywxServiceProviderSchoolService.save(providerSchool);
//                }
//                items.put(provider.getCorpId() + "." + providerSchool.getDeptEncryptionCorpId(), providerSchool);
//                originalItems.put(provider.getCorpId() + "." + providerSchool.getDeptOriginalCorpId(), providerSchool);
//            }
//        }
//        if (CollUtil.isNotEmpty(items)) {
//            redisService.hmset(QywxServiceProviderConstant.PROVIDER_SCHOOL_CORP_DATA, items);
//            redisService.hmset(QywxServiceProviderConstant.PROVIDER_SCHOOL_CORP_ORIGINAL_DATA, originalItems);
//        }
//    }
}
