package com.mingzuozhibi.modules.admin;

import com.mingzuozhibi.commons.base.BaseController;
import com.mingzuozhibi.commons.mylog.JmsService;
import com.mingzuozhibi.modules.disc.DiscRepository;
import com.mingzuozhibi.modules.group.DiscGroupService;
import com.mingzuozhibi.modules.record.RecordCompute;
import com.mingzuozhibi.utils.ThreadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Set;

import static com.mingzuozhibi.utils.FormatUtils.DATE_FORMATTER;

@RestController
public class AdminController extends BaseController {

    @Autowired
    private JmsService jmsService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private AdminService adminService;

    @Autowired
    private RecordCompute recordCompute;

    @Autowired
    private DiscRepository discRepository;

    @Autowired
    private DiscGroupService discGroupService;

    @Transactional
    @Scheduled(cron = "0 59 * * * ?")
    @GetMapping(value = "/admin/sendNeedUpdateAsins", produces = MEDIA_TYPE)
    public void sendNeedUpdateAsins() {
        Set<String> asins = discGroupService.findNeedUpdateAsins();
        jmsService.sendJson("need.update.asins", gson.toJson(asins));
        jmsMessage.notify("JMS -> need.update.asins size=" + asins.size());
    }

    @Transactional
    @Scheduled(cron = "0 0 * * * ?")
    @GetMapping(value = "/admin/runAutomaticTasks", produces = MEDIA_TYPE)
    public void runAutomaticTasks() {
        jmsMessage.notify("运行每小时自动任务");
        ThreadUtils.startThread(() -> {
            adminService.deleteExpiredRemembers();
            adminService.moveExpiredHourRecords();
            adminService.recordRankAndComputePt();
        });
    }

    @Transactional
    @GetMapping(value = "/admin/sendAllDiscTrack", produces = MEDIA_TYPE)
    public void sendAllDiscTrack() {
        jdbcTemplate.query("SELECT asin, title FROM disc", rs -> {
            while (rs.next()) {
                jmsService.sendDiscTrack(rs.getString("asin"), rs.getString("title"));
            }
        });
    }

    @Transactional
    @GetMapping(value = "/admin/computeDate/{date}", produces = MEDIA_TYPE)
    public void computeDate(@PathVariable String date) {
        recordCompute.computeDate(LocalDate.parse(date, DATE_FORMATTER));
    }

    @Transactional
    @GetMapping(value = "/admin/computeDisc/{id}", produces = MEDIA_TYPE)
    public void computeDisc(@PathVariable Long id) {
        discRepository.findById(id).ifPresent(disc -> {
            recordCompute.computeDisc(disc);
        });
    }

}

@@ -129,32 +129,75 @@
 log.JmsService;%0A
+import com.mingzuozhibi.modules.disc.Disc;%0A
 import com.mingz
@@ -745,16 +745,76 @@
 riable;%0A
+import org.springframework.web.bind.annotation.PostMapping;%0A
 import o
@@ -918,13 +918,105 @@
 til.
-Set;%0A
+Optional;%0Aimport java.util.Set;%0A%0Aimport static com.mingzuozhibi.utils.ChecksUtils.paramNotExists;
 %0Aimp
@@ -1076,16 +1076,76 @@
 RMATTER;
+%0Aimport static com.mingzuozhibi.utils.ModifyUtils.logUpdate;
 %0A%0A@RestC
@@ -2761,25 +2761,27 @@
 e = %22/admin/
-c
+reC
 omputeDate/%7B
@@ -2823,25 +2823,27 @@
 public void 
-c
+reC
 omputeDate(@
@@ -3002,17 +3002,19 @@
 %22/admin/
-c
+reC
 omputeDi
@@ -3062,17 +3062,19 @@
 ic void 
-c
+reC
 omputeDi
@@ -3221,10 +3221,591 @@
 %0A    %7D%0A%0A
+    @Transactional%0A    @PostMapping(value = %22/api/admin/reComputeDisc2/%7Bid%7D%22, produces = MEDIA_TYPE)%0A    public String reComputeDisc2(@PathVariable Long id) %7B%0A        Optional%3CDisc%3E byId = discRepository.findById(id);%0A        if (!byId.isPresent()) %7B%0A            return paramNotExists(%22%E7%A2%9F%E7%89%87ID%22);%0A        %7D%0A        Disc disc = byId.get();%0A        Integer pt1 = disc.getTotalPt();%0A        recordCompute.computeDisc(disc);%0A        Integer pt2 = disc.getTotalPt();%0A        jmsMessage.notify(logUpdate(%22%E7%A2%9F%E7%89%87PT%22, pt1, pt2));%0A        return dataResult(%22compute: %22 + pt1 + %22-%3E%22 + pt2);%0A    %7D%0A%0A
 %7D%0A

add reComputeDisc2 api