package com.zhiqiyun.open.router.apis;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dliyun.oap.framework.annotation.ServiceMethod;
import com.dliyun.oap.framework.annotation.ServiceMethodBean;
import com.dliyun.oap.framework.response.OapResponse;
import com.zhiqiyun.open.core.models.sentiment.SentimentSpiderEvent;
import com.zhiqiyun.open.core.models.sentiment.SentimentSpiderResult;
import com.zhiqiyun.open.core.models.sentiment.SentimentSpiderSiteRule;
import com.zhiqiyun.open.core.models.sentiment.UrlPatterns;
import com.zhiqiyun.open.core.service.SentimentSpiderEventService;
import com.zhiqiyun.open.core.service.SentimentSpiderResultService;
import com.zhiqiyun.open.core.service.SentimentSpiderRuleService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.router.request.sentiment.*;
import com.zhiqiyun.open.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Slf4j
@ServiceMethodBean
public class SentimentSpiderApi {

    @Autowired
    private SentimentSpiderEventService sentimentSpiderEventService;

    @Autowired
    private SentimentSpiderRuleService sentimentSpiderRuleService;

    @Autowired
    private SentimentSpiderResultService sentimentSpiderResultService;

    @Autowired
    private SequenceService sequenceService;

    @ServiceMethod(method = "sentiment.spider.rule.findByKeyword", title = "通过关键词查询采集网站规则")
    public OapResponse findByKeyword(QuerySpiderSiteRuleRequest request) {
        QueryWrapper<SentimentSpiderSiteRule> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(request.getKeyword())) {
            wrapper.like("site_name", request.getKeyword());
            wrapper.or();
            wrapper.like("id", request.getKeyword());
        }
        wrapper.orderByDesc("id");

        List<SentimentSpiderSiteRule> listData = this.sentimentSpiderRuleService.list(wrapper);
        return OapResponse.success().setBody(listData);
    }

    @ServiceMethod(method = "sentiment.spider.rule.save", title = "新增或者修改采集网站规则")
    public OapResponse saveOrUpdate(QuerySpiderSiteRuleSaveRequest request) {
        SentimentSpiderSiteRule siteRule = new SentimentSpiderSiteRule();
        siteRule.setUrlPatterns(JSONArray.parseArray(request.getUrlPatterns(), UrlPatterns.class));
        siteRule.setCollectorName(request.getCollectorName());
        siteRule.setStartUrls(request.getStartUrls());
        siteRule.setSiteName(request.getSiteName());
        siteRule.setId(request.getId());
        if (siteRule.getId() != null) {
            siteRule.setUpdatedTime(DateUtil.current());
            siteRule.setUpdatedBy(1000L);
            sentimentSpiderRuleService.updateById(siteRule);
        } else {
            siteRule.setId(this.sequenceService.nextId());
            siteRule.setCreatedTime(DateUtil.current());
            siteRule.setCreatedBy(1000L);
            siteRule.setUpdatedTime(DateUtil.current());
            siteRule.setUpdatedBy(1000L);
            sentimentSpiderRuleService.save(siteRule);
        }
        return OapResponse.success().setBody(siteRule);
    }

    @ServiceMethod(method = "sentiment.spider.rule.delete", title = "删除采集网站规则")
    public OapResponse delete(IdsRequest request) {
        sentimentSpiderRuleService.removeByIds(request.getIds());
        return OapResponse.success();
    }


    @ServiceMethod(method = "sentiment.spider.event.findPage", title = "查询舆情事件")
    public OapResponse findEventPage(QuerySpiderEventRequest request) {
        QueryWrapper<SentimentSpiderEvent> wrapper = new QueryWrapper<>();
        if (request.getSiteRuleId() != null) {
            wrapper.eq("site_rule_id", request.getSiteRuleId());
        }
        if (StringUtils.isNotBlank(request.getEventTitle())) {
            wrapper.like("event_title", request.getEventTitle());
        }
        if (StringUtils.isNotBlank(request.getKeywords())) {
            wrapper.like("keywords", request.getKeywords());
        }

        Page<SentimentSpiderEvent> page = new Page<>(request.getCurrent(), request.getPageSize());
        page.addOrder(OrderItem.desc("id"));

        Page<SentimentSpiderEvent> resultData = this.sentimentSpiderEventService.page(page, wrapper);
        for (SentimentSpiderEvent event : resultData.getRecords()) {
            SentimentSpiderSiteRule rule = this.sentimentSpiderRuleService.getById(event.getSiteRuleId());
            event.setRule(rule);
            event.setStatus(this.sentimentSpiderEventService.getStatus(event.getId()));
        }

        return OapResponse.success().setBody(resultData);
    }

    @ServiceMethod(method = "sentiment.spider.event.add", title = "新建舆情事件")
    public OapResponse save(SaveSpiderEventRequest request) {

        SentimentSpiderEvent entity = new SentimentSpiderEvent();
        BeanUtils.copyProperties(request, entity);

        entity.setCreatedTime(DateUtil.current());
        entity.setCreatedBy(1000L);

        entity.setUpdatedTime(DateUtil.current());
        entity.setUpdatedBy(1000L);
        entity.setId(this.sequenceService.nextId());

        this.sentimentSpiderEventService.save(entity);

        return OapResponse.success().setBody(entity);
    }

    @ServiceMethod(method = "sentiment.spider.event.updateById", title = "更新舆情事件")
    public OapResponse updateById(UpdateSpiderEventRequest request) {
        SentimentSpiderEvent entity = new SentimentSpiderEvent();
        BeanUtils.copyProperties(request, entity);

        entity.setUpdatedBy(1000L);
        entity.setUpdatedTime(DateUtil.current());

        this.sentimentSpiderEventService.updateById(entity);

        return OapResponse.success().setBody(entity);
    }

    @ServiceMethod(method = "sentiment.spider.event.deleteByIds", title = "删除舆情事件")
    public OapResponse deleteByIds(IdsRequest request) {
        this.sentimentSpiderEventService.removeByIds(request.getIds());
        return OapResponse.success();
    }

    @ServiceMethod(method = "sentiment.spider.event.startSpider", title = "启动舆情事件采集")
    public OapResponse startSpider(IdsRequest request) {
        List<SentimentSpiderEvent> listData = this.sentimentSpiderEventService.listByIds(request.getIds());
        for (SentimentSpiderEvent event : listData) {
            SentimentSpiderSiteRule rule = this.sentimentSpiderRuleService.getById(event.getSiteRuleId());
            event.setRule(rule);
            this.sentimentSpiderEventService.start(event);
        }
        return OapResponse.success();
    }

    @ServiceMethod(method = "sentiment.spider.event.stopSpider", title = "停止舆情事件采集")
    public OapResponse stopSpider(IdsRequest request) {
        List<SentimentSpiderEvent> listData = this.sentimentSpiderEventService.listByIds(request.getIds());
        for (SentimentSpiderEvent event : listData) {
            this.sentimentSpiderEventService.stop(event);
        }
        return OapResponse.success();
    }

    @ServiceMethod(method = "sentiment.spider.event.findResultPage", title = "分页查询采集结果")
    public OapResponse findResultPage(QuerySpiderResultRequest request) {

        QueryWrapper<SentimentSpiderResult> wrapper = new QueryWrapper<>();
        if (request.getEventId() != null) {
            wrapper.eq("event_id", request.getEventId());
        }
        if (request.getSiteRuleId() != null) {
            wrapper.eq("site_rule_id", request.getSiteRuleId());
        }
        if (StringUtils.isNotBlank(request.getTitle())) {
            wrapper.like("title", request.getTitle());
        }

        wrapper.select("id", "event_id", "site_rule_id", "url", "title", "keywords", "description", "fragments", "spider_time", "update_time");


        Page<SentimentSpiderResult> page = new Page<>(request.getCurrent(), request.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));

        Page<SentimentSpiderResult> resultData = this.sentimentSpiderResultService.page(page, wrapper);

        return OapResponse.success().setBody(resultData);
    }

    @ServiceMethod(method = "sentiment.spider.event.findResultInfo", title = "通过ID查询采集结果详情")
    public OapResponse findResultInfo(QuerySpiderResultDetailRequest request) {

        SentimentSpiderResult result = this.sentimentSpiderResultService.getById(request.getId());

        return OapResponse.success().setBody(result);
    }
}
