package cn.common.api;

import cn.common.HealthWebApplication;
import cn.common.common.config.message.MsgCodeConstants;
import cn.common.common.constants.BaseConstant;
import cn.common.common.constants.PlatformConstant;
import cn.common.common.enums.audit.AuditTypeEnum;
import cn.common.common.redis.RedisRepository;
import cn.common.common.utils.alibaba.AliMessageSendComponent;
import cn.common.common.utils.auth.EncryptUtils;
import cn.common.common.utils.spring.SnowflakeIdWorker;
import cn.common.repository.entity.AuthUser;
import cn.common.repository.entity.PlatformDict;
import cn.common.repository.repository.AuthDepartmentRepository;
import cn.common.repository.repository.AuthUserRepository;
import cn.common.repository.repository.PlatformDictRepository;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author Singer create by singer email:singer-coder@qq.com
 * @projectName health-platform-api
 * @Description:
 * @date 2020-06-11
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = HealthWebApplication.class)
@ActiveProfiles("dev")
@Slf4j
//@Ignore
public class ApiTests {

    @Resource
    private RedisRepository redisRepository;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private BaseConstant baseConstant;

    @Resource
    private AuthUserRepository authUserRepository;

    @Resource
    private PlatformDictRepository platformDictRepository;

    @Resource
    private AliMessageSendComponent aliMessageSendComponent;

    @Resource
    private PlatformConstant platformConstant;

    @Resource
    private AuthDepartmentRepository authDepartmentRepository;

    @Resource
    private MsgCodeConstants msgCodeConstants;

    @Resource
    private MapperFacade mapperFacade;

    /**
      * 测试redis的位置功能
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/7/22
      */
    @Test
    public void testRedisGeo(){
        //redisRepository.addGeo(PlatformConstant.MERCHANT_POS,104.185574,30.827591,"XXX");
        /*Double distinct = redisRepository.getDistinct(PlatformConstant.MERCHANT_POS, "XXX",
                "XXX");
        log.info("相对距离:"+distinct);*/
        Circle circle = new Circle(104.123488, 30.855101, Metrics.MILES.getMultiplier());
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
                .newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);
        GeoResults<RedisGeoCommands.GeoLocation<String>> pointRadiusList =
                redisRepository.getPointRadius(PlatformConstant.MERCHANT_POS, circle, args);
        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> contentList = pointRadiusList.getContent();
        contentList.stream().forEach(item -> {
            RedisGeoCommands.GeoLocation<String> content = item.getContent();
            String name = content.getName();
            log.info("地址:"+name);
        });
        System.out.println("附近经纬度附近的人：" + JSON.toJSONString(pointRadiusList));
        System.out.println("附近经纬度附近的人：" + pointRadiusList);
    }

    /**
     * 发送阿里云短信
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/3/25
     */
    @Test
    public void sentAliMessage(){
        String messageSignName = msgCodeConstants.getMessageSignName();
        //模板Code
        String messageTemplateCode = StrUtil.EMPTY;
        String phone = "";
        HashMap<String, String> variableCodeHashMap = Maps.newHashMap();
        variableCodeHashMap.put("name","Singer");
        variableCodeHashMap.put("auditType",AuditTypeEnum.CORP_DATA.getDescription());
        try {
            log.info(">>>>>>>>>>>>>>>>>>>>>执行发送阿里云,当前线程ID:{}<<<<<<<<<<<<<<<<<<<",Thread.currentThread().getId());
            aliMessageSendComponent.sendAliMessage(phone,messageTemplateCode,messageSignName,variableCodeHashMap);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>发送短信出现异常:{}<<<<<<<<<<<<<<<<<<<<<<<<<<<",e);
        }
    }

    /**
     * 清洗数据字典
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/3/25
     */
    @Test
    public void initBusinessDictionary(){
        String contentString = "资金类,土地要素类";
        List<String> regionList = Arrays.asList(contentString.split(","));
        regionList.stream().forEach(item -> {
            PlatformDict dictionary = new PlatformDict();

            dictionary.setPlatformDictId(SnowflakeIdWorker.uniqueMainId());
            dictionary.setDictType("problemType");
            dictionary.setDictKey(item);
            dictionary.setDictValue(item);
            dictionary.setDictRemark(item);

            platformDictRepository.insert(dictionary);
        });
    }

    /**
     * 清洗后台系统API参与者数据信息
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/3/25
     */
    @Test
    public void flushUserPwd(){
        List<AuthUser> authUserList = authUserRepository.selectList(new LambdaQueryWrapper<>());
        authUserList.stream().forEach(item -> {
            String authUserId = item.getAuthUserId();
            String userName = item.getUserName();
            String authUserSalt = SnowflakeIdWorker.uniqueMainId();
            String password = "123456";
            String encryptPassword = EncryptUtils.encrypt(password, authUserSalt);
            authUserRepository.resetPassword(userName,encryptPassword,authUserSalt);
        });

    }
}
