package mingzuozhibi.action;

import mingzuozhibi.persist.Disc;
import mingzuozhibi.persist.Sakura;
import mingzuozhibi.support.Dao;
import mingzuozhibi.support.JsonArg;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static mingzuozhibi.persist.Sakura.ViewType.SakuraList;

@RestController
public class SakuraController extends BaseController {

    @Autowired
    private Dao dao;

    @Transactional
    @GetMapping(value = "/api/sakuras", produces = MEDIA_TYPE)
    public String listSakura(@RequestParam("discColumns") String discColumns) {
        JSONArray data = new JSONArray();
        
        @SuppressWarnings("unchecked")
        List<Sakura> sakuras = dao.query(session -> {
            return session.createCriteria(Sakura.class)
                    .add(Restrictions.eq("viewType", SakuraList))
                    .add(Restrictions.eq("enabled", true))
                    .list();
        });

        Set<String> columns = Arrays.stream(discColumns.split(",")).collect(Collectors.toSet());
        sakuras.forEach(sakura -> {
            data.put(sakura.toJSON().put("discs", buildDiscs(sakura, columns)));
        });
        if (LOGGER.isDebugEnabled()) {
            debugRequest("[size={}][discColumns={}]", sakuras.size(), discColumns);
        }
        return objectResult(data);
    }

    @Transactional
    @GetMapping(value = "/api/basic/sakuras", produces = MEDIA_TYPE)
    public String listBasicSakura() {
        JSONArray array = new JSONArray();
        dao.findAll(Sakura.class).forEach(sakura -> array.put(sakura.toJSON()));

        if (LOGGER.isDebugEnabled()) {
            debugRequest("[size={}]", array.length());
        }
        return objectResult(array);
    }

    @Transactional
    @PostMapping(value = "/api/basic/sakuras", produces = MEDIA_TYPE)
    public String saveBasicSakura(
            @JsonArg("$.key") String key,
            @JsonArg("$.title") String title,
            @JsonArg("$.viewType") String viewType) {
        if (dao.lookup(Sakura.class, "key", key) != null) {
            return errorMessage("该Sakura列表已存在");
        }
        Sakura sakura = new Sakura(key, title, Sakura.ViewType.valueOf(viewType));
        dao.save(sakura);

        if (LOGGER.isInfoEnabled()) {
            infoRequest("[json={}]", sakura.toJSON());
        }
        return objectResult(sakura.toJSON());
    }

    private JSONArray buildDiscs(Sakura sakura, Set<String> columns) {
        JSONArray discs = new JSONArray();
        sakura.getDiscs().stream()
                .filter(disc -> disc.getUpdateType() != Disc.UpdateType.None)
                .forEach(disc -> discs.put(disc.toJSON(columns)));
        return discs;
    }

}

@@ -414,202 +414,9 @@
 ion.
-GetMapping;%0Aimport org.springframework.web.bind.annotation.PostMapping;%0Aimport org.springframework.web.bind.annotation.RequestParam;%0Aimport org.springframework.web.bind.annotation.RestController
+*
 ;%0A%0Ai
@@ -898,24 +898,16 @@
 rray();%0A
-        
 %0A       
@@ -2988,10 +2988,849 @@
 %0A    %7D%0A%0A
+    @Transactional%0A    @PostMapping(value = %22/api/basic/sakuras/%7Bid%7D%22, produces = MEDIA_TYPE)%0A    public String editAdminUser(%0A            @PathVariable(%22id%22) Long id,%0A            @JsonArg(%22$.key%22) String key,%0A            @JsonArg(%22$.title%22) String title,%0A            @JsonArg(%22$.viewType%22) String viewType,%0A            @JsonArg(%22$.enabled%22) boolean enabled) %7B%0A        Sakura sakura = dao.get(Sakura.class, id);%0A%0A        if (LOGGER.isDebugEnabled()) %7B%0A            debugRequest(%22%5BBefore:%7B%7D%5D%22, sakura.toJSON());%0A        %7D%0A        sakura.setKey(key);%0A        sakura.setTitle(title);%0A        sakura.setViewType(Sakura.ViewType.valueOf(viewType));%0A        sakura.setEnabled(enabled);%0A        if (LOGGER.isDebugEnabled()) %7B%0A            debugRequest(%22%5BModify:%7B%7D%5D%22, sakura.toJSON());%0A        %7D%0A        return objectResult(sakura.toJSON());%0A    %7D%0A%0A
 %7D%0A

Add Update Api