package org.jeecg.modules.adweb.seo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.vo.DictPropertyModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.adweb.seo.service.ISeoMonthPdfService;
import org.jeecg.modules.adweb.seo.vo.MonthPdfKeywordsRankVO;
import org.jeecg.modules.adweb.system.service.SysAdwebApi;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Description: seo_month_pdf
 * @Author: jeecg-boot
 * @Date: 2023-07-06
 * @Version: V1.0
 */
@RestController
@RequestMapping("/seo/seoMonthPdf")
@Slf4j
public class SeoMonthPdfController {

    @Autowired private SysAdwebApi sysAdwebApi;

    @Autowired private ISysDictItemService sysDictItemService;

    @Autowired private ISeoMonthPdfService seoMonthPdfService;

    /**
     * 获取seo月报当月以及下月优化计划能容（字典）
     *
     * @return
     */
    @GetMapping("/getSeoMonthPlanContent")
    public Result<?> getSeoMonthPlanContent() {
        List<DictPropertyModel> dictPropertyModels =
                sysAdwebApi.queryDictInfoByDictCode("seo_month_report_template");
        String currentItemText = null;
        String nextItemText = null;
        for (DictPropertyModel item : dictPropertyModels) {
            if ("seo_current_month_report".equals(item.getLabel())) {
                currentItemText = item.getValue();
            } else {
                nextItemText = item.getValue();
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("currentMonthPlan", currentItemText);
        map.put("nextMonthPlan", nextItemText);

        return Result.OK(map);
    }

    /**
     * 编辑月报优化计划
     *
     * @param sysDictItem
     * @return
     */
    @PutMapping("/editSeoMonthPlan")
    @CacheEvict(value = CacheConstant.SYS_DICT_CACHE, allEntries = true)
    public Result<?> editSeoMonthPlan(@RequestBody SysDictItem sysDictItem) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        sysDictItem.setUpdateBy(user.getUsername());
        LambdaQueryWrapper<SysDictItem> currentQueryWrapper = new LambdaQueryWrapper();
        currentQueryWrapper.eq(SysDictItem::getItemText, sysDictItem.getItemText());
        SysDictItem sysDictItem1 = sysDictItemService.getOne(currentQueryWrapper);
        sysDictItem.setId(sysDictItem1.getId());
        boolean update = sysDictItemService.updateById(sysDictItem);
        if (update) {
            return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败！");
    }

    @GetMapping("/getSeoKeywordsRank")
    public Result<?> getSeoKeywordsRank(
            @RequestParam String siteCode, @RequestParam String exportMonth) {
        log.info("siteCode:{},exportMonth:{}", siteCode, exportMonth);
        MonthPdfKeywordsRankVO seoKeywordsRank =
                seoMonthPdfService.getSeoKeywordsRank(siteCode, exportMonth);
        log.info("seoKeywordsRank:{}", seoKeywordsRank);
        return Result.OK(seoKeywordsRank);
    }
}
