package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.common.module.entity.BindAccountLogin;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IBindAccountLoginService;
import cn.com.ctop.common.module.service.IFileInfoService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.toutiao.entity.*;
import cn.com.ctop.toutiao.service.*;
import com.alibaba.fastjson.JSONArray;
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.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@RestController
@RequestMapping("template")
public class ByteDanceTemplateController {
    @Autowired
    private IByteDanceCreativeService creativeService;
    @Autowired
    private IUserAllocationService userAllocationService;
    @Autowired
    private IBytedanceUrlInfoService urlInfoService;
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private IByteDanceAdvertisePlanService planService;
    @Autowired
    private IByteDanceCampaignService campaignService;
    @Autowired
    private IByteDanceUserOrientationTemplateService orientationTemplateService;
    @Autowired
    private IByteDanceCampaignTemplateService campaignTemplateService;
    @Autowired
    private IByteDanceBudgetTemplateService budgetTemplateService;
    @Autowired
    private IBytedanceDeliveryTargetTemplateService deliveryTargetTemplateService;
    @Autowired
    private IBytedanceAdvertisePlanTemplateService advertisePlanTemplateService;

    /**
     * 广告组模板
     *
     * @param template 广告组模板信息
     * @return map
     */
    @PostMapping("campaign/insert")
    public Map<String, Object> campaignTemplateInsert(@RequestBody ByteDanceCampaignTemplate template) {
        return campaignTemplateService.insertTemplate(template);
    }

    /**
     * 智能封面
     *
     * @param accountId 广告主id
     * @param videoId 视频id
     * @return map
     */
    @GetMapping("cover/suggest")
    public Map<String, Object> suggestCover(String accountId, String videoId) {
        return campaignTemplateService.getSuggestCover(accountId, videoId);
    }

    /**
     * 预算与出价模板
     *
     * @param template 预算与出价模板信息
     * @return map
     */
    @PostMapping("budget/insert")
    public Map<String, Object> budgetTemplateInsert(@RequestBody ByteDanceBudgetTemplate template) {
        return budgetTemplateService.insertTemplate(template);
    }

    /**
     * 投放目标
     *
     * @param template 投放目标信息
     * @return map
     */
    @PostMapping("deliverytarget/insert")
    public Map<String, Object> diliverytargetTemplateInsert(@RequestBody BytedanceDeliveryTargetTemplate template) {
        return deliveryTargetTemplateService.insertTemplate(template);
    }

    /**
     * 获取投放目标信息
     *
     * @param targetId 投放目标id
     * @return map
     */
    @GetMapping("deliveryTarget/get")
    public Map<String, Object> diliverytargetTemplateGet(Long targetId) {
        return deliveryTargetTemplateService.get(targetId);
    }

    /**
     * 广告创意创建
     * @param template 广告创意信息
     * @return map
     */
    @PostMapping("creative/insert")
    public Map<String, Object> creativeInsert(@RequestBody JSONObject template) {
        return creativeService.insertBatch(template);
    }

    /**
     * 获取快手授权用户信息列表
     * @param userAllocation 用户信息
     * @param pageNo 页码数
     * @param pageSize 每页数据条数
     * @param req 请求信息
     * @return map
     */
    @PostMapping(value = "allocation/list")
    public Result<IPage<UserAllocation>> queryPageList(@RequestBody UserAllocation userAllocation,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize,
                                                       HttpServletRequest req) {
        Result<IPage<UserAllocation>> result = new Result<>();
        QueryWrapper<UserAllocation> queryWrapper = QueryGenerator.initQueryWrapper(userAllocation, req.getParameterMap());
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.eq("user_id", user.getId());
        Page<UserAllocation> page = new Page<>(pageNo, pageSize);
        IPage<UserAllocation> pageList = userAllocationService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @Autowired
    private IBindAccountLoginService bindAccountLoginService;

    /**
     * 获取头条授权用户信息列表
     *
     * @param accountLogin
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @PostMapping(value = "bind/login/list")
    public Result<IPage<BindAccountLogin>> queryBindLoginList(@RequestBody BindAccountLogin accountLogin,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize,
                                                              HttpServletRequest req) {
        Result<IPage<BindAccountLogin>> result = new Result<>();
        QueryWrapper<BindAccountLogin> queryWrapper = QueryGenerator.initQueryWrapper(accountLogin, req.getParameterMap());
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.eq("user_id", user.getId()).eq("login_type", CtopAdConstant.PLATFORM_TYPE_BYTEDANCE_PY);
        Page<BindAccountLogin> page = new Page<>(pageNo, pageSize);
        IPage<BindAccountLogin> pageList = bindAccountLoginService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 用户定向
     *
     * @param template
     * @param req
     * @return
     */
    @PostMapping("userorentation/insert")
    public Map<String, Object> userOrentationTemplateInsert(@RequestBody JSONObject template, HttpServletRequest req) {
        return orientationTemplateService.insertTemplate(template);
    }

    /**
     * 链接信息
     *
     * @param urlInfo
     * @param req
     * @return
     */
    @PostMapping("link/insert")
    public Map<String, Object> linkInsert(@RequestBody BytedanceUrlInfo urlInfo, HttpServletRequest req) {
        return urlInfoService.insertLink(urlInfo);
    }

    /**
     * 广告计划模板
     * 方法废弃
     *
     * @param template
     * @param req
     * @return
     */
    @PostMapping("advertiserplan/insert")
    public Map<String, Object> advertiserplanTemplateInsert(@RequestBody BytedanceAdvertisePlanTemplate template, String accountId, HttpServletRequest req) {
        return advertisePlanTemplateService.insertTemplate(template, accountId);
    }

    /**
     * 用户定向模板列表
     *
     * @param template
     * @param req
     * @return
     */
    @PostMapping("userorentation/list")
    public Result<IPage<ByteDanceUserOrientationTemplate>> userOrentationTemplateList(ByteDanceUserOrientationTemplate template, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        return orientationTemplateService.getList(template, pageNo, pageSize, req);
    }

    /**
     * 预算与出价模板
     *
     * @param template
     * @param req
     * @return
     */
    @PostMapping("budget/list")
    public Result<IPage<ByteDanceBudgetTemplate>> budgetTemplateList(ByteDanceBudgetTemplate template, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        return budgetTemplateService.getList(template, pageNo, pageSize, req);
    }

    /**
     * 广告组模板
     *
     * @return
     */
    @PostMapping("campaign/list")
    public Result<IPage<ByteDanceCampaignTemplate>> campaignTemplateList(ByteDanceCampaignTemplate template, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        return campaignTemplateService.getList(template, pageNo, pageSize, req);
    }

    /**
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     * @功能：获取广告组列表信息
     */
    @PostMapping(value = "campaign/data/list")
    public Result<IPage<ByteDanceCampaign>> campaignPageList(@RequestBody ByteDanceCampaign campaign, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "1000") Integer pageSize, HttpServletRequest req) {
        Result<IPage<ByteDanceCampaign>> result = new Result<>();
        QueryWrapper<ByteDanceCampaign> queryWrapper = QueryGenerator.initQueryWrapper(campaign, req.getParameterMap());
        queryWrapper.eq("toutiao_id", campaign.getToutiaoId()).orderByAsc("id");
        Page<ByteDanceCampaign> page = new Page<>(pageNo, pageSize);
        IPage<ByteDanceCampaign> pageList = campaignService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * @param accountId
     * @return 查询计划可用转化id
     */
    @GetMapping(value = "convert/list")
    public Map<String, Object> convertPageList(Long accountId, Long targetId) {
        return planService.getAdConvertList(accountId, targetId);
    }

    /**
     * @param accountId
     * @param urlId     链接id
     * @return 查询计划可用转化id
     */
    @GetMapping(value = "convert/list/url")
    public Map<String, Object> convertListByUrlId(Long accountId, Long urlId) {
        return planService.getAdConvertListByUrlId(accountId, urlId);
    }

    /**
     * @param bindAccountId 绑定账号id
     * @param urlId         链接id
     * @return 查询计划可用转化id
     */
    @GetMapping(value = "convert/list/url/ng")
    public Map<String, Object> convertListByBindAccountId(Long bindAccountId, Long urlId) {
        return planService.getAdConvertListByUrlIdAndBindId(bindAccountId, urlId);
    }

    @Autowired
    private ISysCategoryService sysCategoryService;

    /**
     * @return
     * @功能：获取头条行业信息列表
     */
    @GetMapping(value = "/bytedance/industry/list")
    public JSONArray campaignPageList() {
        return sysCategoryService.getByteDanceIndustryList();
    }

    /**
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     * @功能：获取广告计划列表信息
     */
    @PostMapping(value = "plan/data/list")
    public Result<IPage<ByteDanceAdvertisePlan>> planPageList(@RequestBody ByteDanceAdvertisePlan plan, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "1000") Integer pageSize, HttpServletRequest req) {
        Result<IPage<ByteDanceAdvertisePlan>> result = new Result<>();
        QueryWrapper<ByteDanceAdvertisePlan> queryWrapper = QueryGenerator.initQueryWrapper(plan, req.getParameterMap());
        queryWrapper.eq("toutiao_id", plan.getToutiaoId()).orderByDesc("id");
        Page<ByteDanceAdvertisePlan> page = new Page<>(pageNo, pageSize);
        IPage<ByteDanceAdvertisePlan> pageList = planService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 投放目标
     *
     * @param template
     * @param req
     * @return
     */
    @PostMapping("deliverytarget/list")
    public Result<IPage<BytedanceDeliveryTargetTemplate>> diliverytargetTemplateList(BytedanceDeliveryTargetTemplate template, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        return deliveryTargetTemplateService.getList(template, pageNo, pageSize, req);
    }

    @PostMapping("video/upload")
    public Map<String, Object> videoUpload(MultipartFile file) {
        return new HashMap<>();
    }

    /**
     * 获取建站列表信息
     */
    @GetMapping("site/list")
    public JSONObject getSiteList(Long accountId) {
        return fileInfoService.getSiteList(accountId);
    }

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

}
