package cn.com.ctop.toutiao.modules.batch.controller;

import cn.com.ctop.common.module.service.IFileInfoService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.toutiao.modules.batch.service.IBytedanceIndustryInfoService;
import cn.com.ctop.toutiao.modules.material.entity.ByteDanceVideoInfo;
import cn.com.ctop.toutiao.modules.material.entity.BytedanceImageInfo;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceAdvertisePlanService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceVideoInfoService;
import cn.com.ctop.toutiao.modules.material.service.IBytedanceImageInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/bytedance/batch")
public class ByteDanceBatchController {
    @Autowired
    IByteDanceAdvertisePlanService byteDanceAdvertisePlanService;
    @Autowired
    private IBytedanceIndustryInfoService bytedanceIndustryInfoService;
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private IByteDanceCreativeService creativeService;
    @Autowired
    private IByteDanceVideoInfoService videoInfoService;

    /**
     * 获取动态词包信息
     *
     * @param accountId
     * @return
     */
    @GetMapping("creative/words/get")
    public Map<String, Object> getCreativeWords(Long accountId) {
        return fileInfoService.getCreativeWords(accountId);
    }

    /**
     * 行动号召按钮
     * @param accountId
     * @param landType
     * @return
     */
    @GetMapping("action/text")
    public Map<String,Object>getActionText(Long accountId,String landType){
        return fileInfoService.getActionText(accountId,landType);
    }

    @GetMapping("industry/list")
    public Map<String,Object>getIndustryList(){
        return bytedanceIndustryInfoService.getIndustryList();
    }
    @GetMapping("load/industry")
    public Map<String,Object>loadIndustryList(Long accountId,Integer level){
        return bytedanceIndustryInfoService.getBytedanceIndustryList(accountId,level);
    }

    @GetMapping(value = "/getVideoList")
    public Result<IPage<ByteDanceVideoInfo>> getBytedqanceVideoList(
            @RequestParam(name = "type") Integer type,
            @RequestParam(name = "accountId") Long accountId,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "startDate", defaultValue = "") String startDate,
            @RequestParam(name = "endDate", defaultValue = "") String endDate) {
        Result<IPage<ByteDanceVideoInfo>> result = new Result<>();
        try {
            if (Check.isNull(accountId)) {
                throw new Exception("账户id不能为空");
            }
            QueryWrapper<ByteDanceVideoInfo> queryWrapper = new QueryWrapper<>();
            if (null!=startDate&&!"".equals(startDate.trim()) && null!=endDate&&!"".equals(endDate.trim())) {
                queryWrapper.between("create_time", startDate+" 00:00:00", endDate+" 23:59:59");
            }
            if(null!=type&&type!=0){
                queryWrapper.eq("type",type);
            }
            queryWrapper.eq("account_id",accountId);
            queryWrapper.orderByDesc("material_upload_time");
            queryWrapper.groupBy("signature");
            Page<ByteDanceVideoInfo> page = new Page<>(pageNo, pageSize);
            IPage<ByteDanceVideoInfo> pageList = videoInfoService.page(page, queryWrapper);
            result.setSuccess(true);
            result.setResult(pageList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
    @Autowired
    private IBytedanceImageInfoService imageInfoService;

    @GetMapping(value = "/getImageList")
    public Result<IPage<BytedanceImageInfo>> getBytedqanceVideoList(
            @RequestParam(name = "type") Integer type,
            @RequestParam(name = "accountId")Long accountId, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "startDate", defaultValue = "") String startDate,
            @RequestParam(name = "endDate", defaultValue = "") String endDate, HttpServletRequest req) {
        Result<IPage<BytedanceImageInfo>> result = new Result<>();
        try {
            if (Check.isNull(accountId)) {
                throw new Exception("账户id不能为空");
            }
            QueryWrapper<BytedanceImageInfo> queryWrapper = new QueryWrapper<>();
            if(null!=type&&type!=0){
                queryWrapper.eq("type",type);
            }
            if (!Check.isNull(startDate) && !Check.isNull(endDate)) {
                queryWrapper.between("image_create_time", startDate+" 00:00:00", endDate+" 23:59:59");
            }
            queryWrapper.eq("account_id",accountId);
            queryWrapper.orderByDesc("image_create_time");
            queryWrapper.groupBy("signature");
            Page<BytedanceImageInfo> page = new Page<>(pageNo, pageSize);
            IPage<BytedanceImageInfo> pageList = imageInfoService.page(page, queryWrapper);
            result.setSuccess(true);
            result.setResult(pageList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     *  批量创建创意
     */
    @PostMapping("/create/creative")
    public Map<String,Object>addCreative(@RequestBody JSONObject data){
        return creativeService.batchCreate(data);
    }
}

