package com.scpyun.platform.jilinsscgsdp.task.impl;

import com.scpyun.base.core.utils.SpringContextHolder;
import com.scpyun.base.core.utils.UnifyLogger;
import com.scpyun.base.db.service.CommonService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class FocusAreaNewTask {

    @Autowired
    CommonService commonService;
    RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
    private final String nameSpace = "com.scpyun.platform.standard.jilinsscgsdp.FocusAreaNewTaskDao.";


    @Scheduled(cron = "0 */10 * * * ?")
    //@Scheduled(cron = "*/5 * * * * ?")
    public void initFocusArea() {
        RLock rLock = redissonClient.getLock("jilinexchange:initFocusArea");
//        rLock.lock(NumberConstant.R_LOCK, TimeUnit.SECONDS);
        try {
            if (rLock.tryLock(60, TimeUnit.SECONDS)) {
                System.out.println("==========================执行定时任务");
                List<Map<String, Object>> areaList = commonService.findList(nameSpace + "getAreaList", null);

                // 获取所有的纠纷大类
                List<Map<String, Object>> eventCategoryList = commonService.findList(nameSpace + "getEventCategoryList", null);

                LocalDate today = LocalDate.now();
                String current_month = today.format(DateTimeFormatter.ofPattern("MM"));

                String current_year = today.format(DateTimeFormatter.ofPattern("yyyy"));

                Map<String, Object> delParam = new HashMap<>();
                delParam.put("year", current_year);
                delParam.put("month", current_month);
                delParam.put("data_type", "0");
                commonService.delete(nameSpace +"deleteFocusAreaNew",delParam);
                if (areaList != null && areaList.size() > 0) {
                    for (Map<String, Object> parent_item : areaList) {
                        for (Map<String, Object> item : eventCategoryList) {
                            item.put("area_id", parent_item.get("area_id"));
                            // 获取上一月各纠纷大类的 矛盾数量
                            int lastMonthCount = commonService.getObject(nameSpace + "getLastMonthCount", item);
                            // 获取当前月各纠纷大类的 矛盾数量
                            int currentMonthCount = commonService.getObject(nameSpace + "getCurrentMonthCount", item);
                            // 计算环比增长率
                            double res;
                            if (lastMonthCount == 0) {
                                res = currentMonthCount == 0 ? 0 : 100;
                            } else {
                                res = ((double) (currentMonthCount - lastMonthCount) / lastMonthCount) * 100;
                            }
                            int resInt = (int) Math.round(res);
                            if (resInt > 30 && currentMonthCount > 15) {
                                Map<String, Object> param = new HashMap<>();
                                param.put("year", current_year);
                                param.put("month", current_month);
                                param.put("event_category_id", item.get("value"));
                                param.put("growth_rate", resInt);
                                param.put("data_type", "0");
                                param.put("event_count", currentMonthCount);
                                param.put("area_code", parent_item.get("area_id"));
                                commonService.insert(nameSpace + "insertFocusAreaNew", param);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            UnifyLogger.parent(UnifyLogger.getTrace(e), UnifyLogger.Level.INFO);
        } finally {
            rLock.unlock();
        }
    }

    @Scheduled(cron = "0 */10 * * * ?")
    public void insertFocusAreaWithYear() {
        RLock rLock = redissonClient.getLock("jilinexchange:insertFocusAreaWithYear");
//        rLock.lock(NumberConstant.R_LOCK, TimeUnit.SECONDS);
        try {
            if (rLock.tryLock(60, TimeUnit.SECONDS)) {
                List<Map<String, Object>> areaList = commonService.findList(nameSpace + "getAreaList", null);

                // 获取所有的纠纷大类
                List<Map<String, Object>> eventCategoryList = commonService.findList(nameSpace + "getEventCategoryList", null);

                LocalDate today = LocalDate.now();
                String current_month = today.format(DateTimeFormatter.ofPattern("MM"));

                String current_year = today.format(DateTimeFormatter.ofPattern("yyyy"));

                Map<String, Object> delParam = new HashMap<>();
                delParam.put("year", current_year);
                delParam.put("month", current_month);
                delParam.put("data_type", "1");
                commonService.delete(nameSpace +"deleteFocusAreaNew",delParam);
                if (areaList != null && areaList.size() > 0) {
                    for (Map<String, Object> parent_item : areaList) {
                        for (Map<String, Object> item : eventCategoryList) {
                            item.put("area_id", parent_item.get("area_id"));
                            // 获取上一年各纠纷大类的 矛盾数量
                            int lastYearCount = commonService.getObject(nameSpace + "getLastYearCount", item);
                            // 获取当前年各纠纷大类的 矛盾数量
                            int currentYearCount = commonService.getObject(nameSpace + "getCurrentYearCount", item);
                            // 计算环比增长率
                            double res;
                            if (lastYearCount == 0) {
                                res = currentYearCount == 0 ? 0 : 100;
                            } else {
                                res = ((double) (currentYearCount - lastYearCount) / lastYearCount) * 100;
                            }
                            int resInt = (int) Math.round(res);
                            if (resInt > 50 && currentYearCount > 15) {
                                Map<String, Object> param = new HashMap<>();
                                param.put("year", current_year);
                                param.put("month", current_month);
                                param.put("event_category_id", item.get("value"));
                                param.put("growth_rate", resInt);
                                param.put("data_type", "1");
                                param.put("event_count", currentYearCount);
                                param.put("area_code", parent_item.get("area_id"));
                                commonService.insert(nameSpace + "insertFocusAreaNew", param);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            UnifyLogger.parent(UnifyLogger.getTrace(e), UnifyLogger.Level.INFO);
        } finally {
            rLock.unlock();
        }
    }
}
