package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.QueryGenerator;
import cn.com.ctop.crawler.modules.account.entity.KuaishouAdAccount;
import cn.com.ctop.crawler.modules.account.service.IKuaishouAdAccountService;
import cn.com.ctop.kuaishou.modules.app.entity.AppAd;
import cn.com.ctop.kuaishou.modules.app.entity.AppVideo;
import cn.com.ctop.kuaishou.modules.app.service.IAppAdService;
import cn.com.ctop.kuaishou.modules.app.service.IAppVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jeecg-boot
 */
@RestController
@RequestMapping("/ctop/adcover")
public class AdCoverController {
    @Autowired
    private IAppAdService appAdService;
    @Autowired
    private IAppVideoService appVideoService;
    @Autowired
    private IKuaishouAdAccountService kuaishouAdAccountService;
    @Autowired
    private ISysCategoryService sysCategoryService;

    @ResponseBody
    @GetMapping("/summary/today")
    public Result<Map<String, Object>> getTodaySummary() {
        Result<Map<String, Object>> result = new Result<>();
        QueryWrapper<AppAd> appAdWrapper = new QueryWrapper<>();
        QueryWrapper<AppVideo> appVideoWrapper = new QueryWrapper<>();
        QueryWrapper<KuaishouAdAccount> advertiserWrapper = new QueryWrapper<>();
        QueryWrapper<KuaishouAdAccount> productWrapper = new QueryWrapper<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(new Date());
        String startDate = now + " 00:00:00";
        String endDate = now + " 23:59:59";
        appAdWrapper.between("time", startDate, endDate);
        appVideoWrapper.between("time", startDate, endDate);
        advertiserWrapper.between("create_time", startDate, endDate);
        productWrapper.between("create_time", startDate, endDate);
        productWrapper.groupBy("product");
        int appAdCount = appAdService.count(appAdWrapper);
        int appVideoCount = appVideoService.count(appVideoWrapper);
        int advertiserCount = kuaishouAdAccountService.count(advertiserWrapper);
        int productCount = kuaishouAdAccountService.count(productWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("adCount", appAdCount);
        map.put("videoCount", appVideoCount);
        map.put("advertiserCount", advertiserCount);
        map.put("productCount", productCount);
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }

    @ResponseBody
    @GetMapping("/summary/all")
    public Result<Map<String, Object>> getAllSummary() {
        Result<Map<String, Object>> result = new Result<>();
        QueryWrapper<KuaishouAdAccount> productWrapper = new QueryWrapper<>();
        productWrapper.groupBy("product");
        int appAdCount = appAdService.count();
        int appVideoCount = appVideoService.count();
        int advertiserCount = kuaishouAdAccountService.count();
        int productCount = kuaishouAdAccountService.count(productWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("adCount", appAdCount);
        map.put("videoCount", appVideoCount);
        map.put("advertiserCount", advertiserCount);
        map.put("productCount", productCount);
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }

    @ResponseBody
    @GetMapping("/feeds")
    public Result<List<AppAd>> getVideoList(HttpServletRequest req){
        Result<List<AppAd>> result = new Result<>();
        Map<String,String[]> paramMap = req.getParameterMap();
        String industry = paramMap.get("industry")!=null?sysCategoryService.getById(paramMap.get("industry")[0]).getName():null;
        String company = paramMap.get("company")!=null?paramMap.get("company")[0]:null;
        String product = paramMap.get("product")!=null?paramMap.get("product")[0]:null;
        String userName = paramMap.get("userName") != null ? paramMap.get("userName")[0] : null;
        String start = paramMap.get("start")!=null?paramMap.get("start")[0]:null;
        String end = paramMap.get("end")!=null?paramMap.get("end")[0]:null;
        String orderByColumn = paramMap.get("orderByColumn")!=null?paramMap.get("orderByColumn")[0]:null;
        String orderByType = paramMap.get("orderByType")!=null?paramMap.get("orderByType")[0]:null;
        Integer pageSize = paramMap.get("pageSize") != null ? Integer.parseInt(paramMap.get("pageSize")[0]) : 10;
        Integer pageNo = paramMap.get("pageNo") != null ? Integer.parseInt(paramMap.get("pageNo")[0]) : 1;
        int pageStart = (pageNo-1)*pageSize;
        List<AppAd> list = appAdService.queryAdFeeds(userName, industry, company, product, start, end, orderByColumn, orderByType, pageStart, pageSize);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    @ResponseBody
    @GetMapping("/detail")
    public Result<Map<String,Object>> getDetail(@RequestParam(name="photoId") Long photoId, @RequestParam(name="userId") Long userId){
        Result<Map<String, Object>> result = new Result<>();
        AppAd ad = new AppAd();
        ad.setPhotoId(photoId);
        QueryWrapper<AppAd> queryWrapper = QueryGenerator.initQueryWrapper(ad,null);
        List<AppAd> list = appAdService.list(queryWrapper);
        AppVideo video = appVideoService.getById(photoId);
        KuaishouAdAccount kuaishouAdAccount = kuaishouAdAccountService.getById(userId);
        if(kuaishouAdAccount == null){
            kuaishouAdAccount = new KuaishouAdAccount();
        }
        ad = list.get(0);
        if(video == null){
            video = new AppVideo();
            video.setMvUrl(ad.getMvUrl());
        }

        kuaishouAdAccount.setUrl("https://live.kuaishou.com/profile/" + ad.getShareInfo().substring(ad.getShareInfo().indexOf('=') + 1, ad.getShareInfo().indexOf('&')));
        video.setShareInfo("https://live.kuaishou.com/u/" + ad.getShareInfo().replace("userId=", "").replace("photoId=", "").replace("&", "/"));
        Map<String, Object> map = new HashMap<>();
        map.put("adlist",list);
        map.put("user",kuaishouAdAccount);
        map.put("video",video);
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }

    /**
     *
     * @param photoId
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/adlist")
    public Result<List<AppAd>> getAdList(@RequestParam(name = "photoId") String photoId, @RequestParam(name = "userId") Long userId) {
        Result<List<AppAd>> result = new Result<>();
        AppAd ad = new AppAd();
        ad.setPhotoId(Long.valueOf(photoId));
        QueryWrapper<AppAd> queryWrapper = QueryGenerator.initQueryWrapper(ad,null);
        List<AppAd> list = appAdService.list(queryWrapper);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }
}
