/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.modules.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.RuleConfigure;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.repository.RuleConfigureRepository;
import com.youchain.basicdata.service.StockService;
import com.youchain.businessdata.domain.PickDetail;
import com.youchain.businessdata.domain.Task;
import com.youchain.businessdata.service.TaskService;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.BizStatus;
import com.youchain.utils.Email.EmailUtils;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;



/**
 * 测试用
 * @author Liu Xue
 * @date 2019-01-08
 */
@Slf4j

@Component("testTask")
@Service
public class TestTask {
    @Autowired
    private EntityManager entityManager;
    @Autowired
    private RuleConfigureRepository ruleConfigureRepository;
    @Autowired
    private StockService stockService;
    private static int MAX_TASK_COUNT = 4;
    public static int MAX_HS_COUNT = 4;
    //限制时间
    private static long TIMEOUT_MS = 600000; // 10分钟，以毫秒为单位
    private long lastTaskTime = System.currentTimeMillis();


//    @Bean(autowire = Autowire.BY_NAME,value = "testTask")
    @AnonymousAccess
    public synchronized void reminderRules() {
        System.out.println("执行一次定时任务");
        List<RuleConfigure> ruleConfigures= ruleConfigureRepository.findAll();
        System.out.println("执行一次定时任务2");
        for(RuleConfigure ruleConfigure:ruleConfigures) {
            String sql=ruleConfigure.getSqlContent();
            List taskList= entityManager.createNativeQuery(sql)
                    .unwrap(SQLQuery.class)
                    .setResultTransformer(
                            AliasToEntityMapResultTransformer.INSTANCE
                    )
                    .list();
            if(taskList.size()>0){
                stockService.stockMsg(ruleConfigure.getTcMsg());
                EmailUtils.sendNodecrypPwd("预警提醒",ruleConfigure.getYjDes(),ruleConfigure.getYjSjr().split(";"));
            }
       }
    }




    public static void main(String[] args) throws JsonProcessingException {

    }
}
