Search in sources :

Example 11 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class SakuraController method pushDiscs.

@Transactional
@PreAuthorize("hasRole('BASIC')")
@PostMapping(value = "/api/sakuras/{id}/discs/{discId}", produces = MEDIA_TYPE)
public String pushDiscs(@PathVariable("id") Long id, @PathVariable("discId") Long discId, @RequestParam(name = "discColumns", defaultValue = DISC_COLUMNS) String discColumns) {
    Sakura sakura = dao.get(Sakura.class, id);
    if (sakura == 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 (sakura.getDiscs().stream().anyMatch(d -> d.getId().equals(discId))) {
        if (LOGGER.isInfoEnabled()) {
            infoRequest("[添加碟片到列表失败][指定的碟片已存在于列表][ASIN={}][列表={}]", disc.getAsin(), sakura.getTitle());
        }
        return errorMessage("指定的碟片已存在于列表");
    }
    sakura.getDiscs().add(disc);
    if (LOGGER.isInfoEnabled()) {
        infoRequest("[添加碟片到列表成功][ASIN={}][列表={}]", disc.getAsin(), sakura.getTitle());
    }
    return objectResult(disc.toJSON(getColumns(discColumns)));
}
Also used : Disc(mingzuozhibi.persist.disc.Disc) Sakura(mingzuozhibi.persist.disc.Sakura) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class DiscController method findRecords.

@Transactional
@GetMapping(value = "/api/discs/{id}/records", produces = MEDIA_TYPE)
public String findRecords(@PathVariable Long id) {
    Disc disc = dao.get(Disc.class, id);
    if (disc == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[获取碟片排名失败][指定的碟片Id不存在][Id={}]", id);
        }
        return errorMessage("指定的碟片Id不存在");
    }
    JSONObject result = disc.toJSON();
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[获取碟片排名成功][碟片信息={}]", result);
    }
    result.put("records", buildRecords(dao, disc));
    return objectResult(result);
}
Also used : JSONObject(org.json.JSONObject) Disc(mingzuozhibi.persist.disc.Disc) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class DiscController method searchFromAmazon.

private void searchFromAmazon(String asin, AtomicReference<Disc> disc, StringBuffer error) {
    Instant instant = Instant.now();
    if (LOGGER.isInfoEnabled()) {
        infoRequest("[查找碟片][从Amazon查询开始][asin={}]", asin);
    }
    service.createDiscTask(asin, task -> {
        if (task.isDone()) {
            Node node = getNode(task.getDocument(), "Items", "Item", "ItemAttributes");
            String rankText = getText(task.getDocument(), "Items", "Item", "SalesRank");
            if (node != null) {
                Document itemAttributes = node.getOwnerDocument();
                String title = getText(itemAttributes, "Title");
                String group = getText(itemAttributes, "ProductGroup");
                String release = getText(itemAttributes, "ReleaseDate");
                Objects.requireNonNull(title);
                Objects.requireNonNull(group);
                DiscType type = getType(group, title);
                boolean amazon = title.startsWith("【Amazon.co.jp限定】");
                LocalDate releaseDate;
                if (release != null) {
                    releaseDate = LocalDate.parse(release, formatter);
                } else {
                    releaseDate = LocalDate.now();
                }
                Disc newDisc = new Disc(asin, title, type, UpdateType.Both, amazon, releaseDate);
                if (rankText != null) {
                    newDisc.setThisRank(new Integer(rankText));
                }
                dao.save(newDisc);
                disc.set(newDisc);
            } else {
                error.append(task.getErrorMessage());
            }
        }
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("[查找碟片][从Amazon查询成功][asin={}][耗时={}ms]", asin, Instant.now().toEpochMilli() - instant.toEpochMilli());
        }
        synchronized (disc) {
            disc.notify();
        }
    });
}
Also used : Disc(mingzuozhibi.persist.disc.Disc) Instant(java.time.Instant) Node(org.w3c.dom.Node) DocumentReader.getNode(mingzuozhibi.service.amazon.DocumentReader.getNode) DiscType(mingzuozhibi.persist.disc.Disc.DiscType) Document(org.w3c.dom.Document) LocalDate(java.time.LocalDate)

Example 14 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class DiscController method mergePts.

@Transactional
@PutMapping(value = "/api/discs/{id}/pts", produces = MEDIA_TYPE)
public String mergePts(@PathVariable Long id, @JsonArg String text) {
    Disc disc = dao.get(Disc.class, id);
    if (disc == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[提交PT失败][指定的碟片Id不存在][Id={}]", id);
        }
        return errorMessage("指定的碟片Id不存在");
    }
    int matchLine = mergePtText(dao, disc, text);
    computeAndUpdateSakuraPt(dao, disc);
    JSONObject result = disc.toJSON();
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[提交PT成功][提交记录数={}][碟片信息={}]", matchLine, result);
    }
    result.put("records", buildRecords(dao, disc));
    return objectResult(result);
}
Also used : JSONObject(org.json.JSONObject) Disc(mingzuozhibi.persist.disc.Disc) PutMapping(org.springframework.web.bind.annotation.PutMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with Disc

use of mingzuozhibi.persist.disc.Disc in project mzzb-server by mingzuozhibi.

the class AmazonScheduler method checkAmazonHotData.

private void checkAmazonHotData() {
    LOGGER.info("[开始检测Amazon(Hot)数据]");
    Set<Disc> discs = new LinkedHashSet<>();
    dao.execute(session -> {
        findActiveSakura(session).forEach(sakura -> {
            findAmazonDiscs(sakura).filter(needUpdate()).forEach(discs::add);
        });
    });
    Set<Disc> hotDiscs = discs.stream().unordered().limit(10).collect(Collectors.toSet());
    if (hotDiscs.size() > 0) {
        LOGGER.debug("[开始检测Amazon(Hot)数据][共{}个]", hotDiscs.size());
        AtomicInteger updateCount = new AtomicInteger(hotDiscs.size());
        hotDiscs.forEach(disc -> {
            service.createRankTask(disc.getAsin(), checkHotCB(updateCount, disc));
        });
    } else {
        LOGGER.info("[结束检测Amazon(Hot)数据][未找到可以检测的Amazon(Hot)数据]]");
        amazonFetchStatus = AmazonFetchStatus.waitingForUpdate;
    }
}
Also used : Disc(mingzuozhibi.persist.disc.Disc) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Disc (mingzuozhibi.persist.disc.Disc)16 Transactional (org.springframework.transaction.annotation.Transactional)9 JSONObject (org.json.JSONObject)7 Sakura (mingzuozhibi.persist.disc.Sakura)6 LocalDate (java.time.LocalDate)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 LocalDateTime (java.time.LocalDateTime)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 UpdateType (mingzuozhibi.persist.disc.Disc.UpdateType)3 ViewType (mingzuozhibi.persist.disc.Sakura.ViewType)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 PutMapping (org.springframework.web.bind.annotation.PutMapping)3 java.util (java.util)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 DiscType (mingzuozhibi.persist.disc.Disc.DiscType)2 Dao (mingzuozhibi.support.Dao)2 Restrictions (org.hibernate.criterion.Restrictions)2 JSONArray (org.json.JSONArray)2