package mingzuozhibi.action;

import mingzuozhibi.persist.disc.Disc;
import mingzuozhibi.persist.disc.DiscGroup;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
public class DiscGroupItemsController extends BaseController {

    @Transactional
    @GetMapping(value = "/api/discGroups/key/{key}/discs", produces = MEDIA_TYPE)
    public String findDiscs(
        @PathVariable String key,
        @RequestParam(required = false) String discColumns) {
        DiscGroup discGroup = dao.lookup(DiscGroup.class, "key", key);
        if (discGroup == null) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[获取列表碟片失败][指定的列表索引不存在][Key={}]", key);
            }
            return errorMessage("指定的列表索引不存在");
        }

        JSONObject result = discGroup.toJSON();
        result.put("discs", buildDiscs(discGroup, discColumns));

        if (LOGGER.isDebugEnabled()) {
            debugRequest("[获取列表碟片成功][列表标题={}][碟片数量={}]",
                discGroup.getTitle(), discGroup.getDiscs().size());
        }
        return objectResult(result);
    }

    private JSONArray buildDiscs(DiscGroup discGroup, String columns) {
        JSONArray discs = new JSONArray();
        if (columns == null) {
            discGroup.getDiscs().forEach(disc -> {
                discs.put(disc.toJSON());
            });
        } else {
            Set<String> columnSet = Stream.of(columns.split(","))
                .collect(Collectors.toSet());
            discGroup.getDiscs().forEach(disc -> {
                discs.put(disc.toJSON(columnSet));
            });
        }
        return discs;
    }

    @Transactional
    @PreAuthorize("hasRole('BASIC')")
    @PostMapping(value = "/api/discGroups/{id}/discs/{discId}", produces = MEDIA_TYPE)
    public synchronized String pushDiscs(
        @PathVariable Long id,
        @PathVariable Long discId) {

        DiscGroup discGroup = dao.get(DiscGroup.class, id);
        if (discGroup == null) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[添加碟片到列表失败][指定的列表Id不存在][Id={}]", id);
            }
            return errorMessage("指定的列表Id不存在");
        }

        Disc disc = dao.get(Disc.class, discId);
        if (disc == null) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[添加碟片到列表失败][指定的碟片Id不存在][Id={}]", discId);
            }
            return errorMessage("指定的碟片Id不存在");
        }

        if (discGroup.getDiscs().stream().anyMatch(d -> d.getId().equals(discId))) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[添加碟片到列表失败][指定的碟片已存在于列表][列表={}][碟片={}]",
                    discGroup.getTitle(), disc.getLogName());
            }
            return errorMessage("指定的碟片已存在于列表");
        }

        discGroup.getDiscs().add(disc);

        if (LOGGER.isInfoEnabled()) {
            infoRequest("[添加碟片到列表成功][列表={}][碟片={}]", discGroup.getTitle(), disc.getLogName());
        }
        return objectResult(disc.toJSON());
    }

    @Transactional
    @PreAuthorize("hasRole('BASIC')")
    @DeleteMapping(value = "/api/discGroups/{id}/discs/{discId}", produces = MEDIA_TYPE)
    public synchronized String dropDiscs(
        @PathVariable("id") Long id,
        @PathVariable("discId") Long discId) {

        DiscGroup discGroup = dao.get(DiscGroup.class, id);
        if (discGroup == null) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[从列表移除碟片失败][指定的列表Id不存在][Id={}]", id);
            }
            return errorMessage("指定的列表Id不存在");
        }

        Disc disc = discGroup.getDiscs().stream()
            .filter(t -> t.getId().equals(discId))
            .findFirst().orElse(null);
        if (disc == null) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[从列表移除碟片失败][指定的碟片Id不存在于列表][Id={}]", discId);
            }
            return errorMessage("指定的碟片Id不存在于列表");
        }

        discGroup.getDiscs().remove(disc);

        if (LOGGER.isInfoEnabled()) {
            infoRequest("[从列表移除碟片成功][列表={}][碟片={}]", discGroup.getTitle(), disc.getLogName());
        }
        return objectResult(disc.toJSON());
    }

}

@@ -23,16 +23,52 @@
 ction;%0A%0A
+import mingzuozhibi.jms.JmsMessage;%0A
 import m
@@ -197,16 +197,79 @@
 Object;%0A
+import org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -608,24 +608,75 @@
 ntroller %7B%0A%0A
+    @Autowired%0A    private JmsMessage jmsMessage;%0A%0A
     @Transac
@@ -3207,33 +3207,32 @@
 .add(disc);%0A
-%0A
         
 if (LOGGER.i
@@ -3223,61 +3223,25 @@
     
-if (LOGGER.isInfoEnabled()) %7B%0A            infoRequest
+jmsMessage.notify
 (%22%5B%E6%B7%BB
@@ -3249,33 +3249,40 @@
 %E7%A2%9F%E7%89%87%E5%88%B0%E5%88%97%E8%A1%A8%E6%88%90%E5%8A%9F%5D%5B%E5%88%97%E8%A1%A8=
-%7B%7D
+%25s
 %5D%5B%E7%A2%9F%E7%89%87=
-%7B%7D
+%25s%5D%5B%E7%94%A8%E6%88%B7=%25s
 %5D%22, discGrou
@@ -3308,36 +3308,41 @@
 getLogName()
-);%0A        %7D
+, getUserName());
 %0A        ret
@@ -4330,25 +4330,24 @@
 (disc);%0A
-%0A
         
 if (LOGG
@@ -4342,61 +4342,25 @@
     
-if (LOGGER.isInfoEnabled()) %7B%0A            infoRequest
+jmsMessage.notify
 (%22%5B%E4%BB%8E
@@ -4372,25 +4372,32 @@
 %E7%89%87%E6%88%90%E5%8A%9F%5D%5B%E5%88%97%E8%A1%A8=
-%7B%7D
+%25s
 %5D%5B%E7%A2%9F%E7%89%87=
-%7B%7D
+%25s%5D%5B%E7%94%A8%E6%88%B7=%25s
 %5D%22, disc
@@ -4431,28 +4431,33 @@
 ogName()
-);%0A        %7D
+, getUserName());
 %0A       

Add disc group items logger