package com.rf.psychological;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rf.psychological.config.SystemExcludeFilter;
import com.rf.psychological.utils.CPUSerial;
import com.rf.psychological.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.persistence.EntityManager;
import java.io.IOException;

/**
 * @author zzf
 */
//@EnableCaching
@EnableJpaRepositories(basePackages = {"com.rf.psychological"})
@SpringBootApplication(scanBasePackages = {"com.rf.psychological"})
//@ComponentScan(basePackages = {"com.rf.psychological"},excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM,classes ={ SystemExcludeFilter.class})})
//@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,pattern = "com.rf.psychological.wxpay.*")})

@EnableJpaAuditing
@EnableTransactionManagement
@EnableScheduling
@Slf4j
//@EnableSwagger2
public class PsychologicalApplication {

    static Logger logger = LoggerFactory.getLogger(PsychologicalApplication.class);
    @Autowired
    static Environment environment;
    @Value("${spring.profiles.active}")
    static String profile;


    public static void main(String[] args) throws Exception {
        //检查环境
        //System.out.println("profile--"+profile);
        //System.out.println("env--"+environment.getActiveProfiles()[0]);

        /*if (PsychologicalApplication.checkDog()){
        }else {
            return;
        }*/

            SpringApplication.run(PsychologicalApplication.class, args);
		/*if (CPUSerial.getDiskdriveAndCsproductUUID().equals(Constant.DEFAULT_PZ_UUID)){
		}else {
			System.out.println("无权限启动");
		}*/
    }

    private static boolean checkDog() {
        System.out.println(System.getProperty("java.library.path"));
        int ret;
        SoftKey ytsoftkey = new SoftKey();
        String DevicePath;
        DevicePath = ytsoftkey.FindPort(0);
        if(ytsoftkey.get_LastError()!=0)
        {
            System.out.println("未找到加密锁,请插入加密锁后，再进行操作。");
            logger.error("未找到加密锁,请插入加密锁后，再进行操作。");
            return false;
        }
        if (ytsoftkey.CheckKeyByFindort_2() == 0)
        {
            System.out.println("使用普通算法一来检查:找到指定的加密锁");
            logger.info("使用普通算法一来检查:找到指定的加密锁");
        }
        else
        {
            logger.error("使用普通算法一来检查:未能找到指定的加密锁");
            System.out.println("使用普通算法一来检查:未能找到指定的加密锁");
            return false;
        }
        ret = ytsoftkey.CheckKeyByEncstring();
        if (ret == 1)
        {
            logger.error("你生成加密代码时没有设置该函数");
            System.out.println( "你生成加密代码时没有设置该函数");
            return false;
        }
        if (ret == 0)
        {
            logger.info("使用增强算法一来检查:找到指定的加密锁");
            System.out.println("使用增强算法一来检查:找到指定的加密锁");
        }
        else
        {
            logger.error("使用增强算法一来检查:未能找到指定的加密锁");
            System.out.println("使用增强算法一来检查:未能找到指定的加密锁");
            return false;
        }
        ret = ytsoftkey.CheckKeyByEncstring_New();
        if (ret == 0)
        {
            logger.info("使用增强算法二来检查:找到指定的加密锁");
            System.out.println("使用增强算法二来检查:找到指定的加密锁");
        }
        else
        {
            if (ret < 0)
            {
                log.error("使用增强算法二来检查:未能找到指定的加密锁");
                System.out.println("使用增强算法二来检查:未能找到指定的加密锁");
                return false;
            }
            else
            {
                if (ret == 2)
                {
                    log.error("当前锁不支持这个功能。");
                    System.out.println("当前锁不支持这个功能。");
                    return false;
                }
            }
        }
        ret = ytsoftkey.CheckKeyByReadEprom();
        if (ret == 1)
        {
            logger.error("你生成加密代码时没有设置该函数");
            System.out.println("你生成加密代码时没有设置该函数");
            return false;
        }
        if (ret == 0)
        {
            logger.info("使用读写储存器来检查:找到指定的加密锁");
            System.out.println("使用读写储存器来检查:找到指定的加密锁");
        }
        else
        {
            logger.error("使用读写储存器来检查:未能找到指定的加密锁");
            System.out.println("使用读写储存器来检查:未能找到指定的加密锁");
            return false;
        }
        return  true;
    }

    /**
     * 让Spring管理JPAQueryFactory
     *
     * @param entityManager
     * @return
     */
    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }


}
