package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IFileInfoService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.toutiao.modules.material.entity.ByteDanceAdvertisePlan;
import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCampaign;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceAdvertisePlanService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCampaignService;
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 IUserAllocationService userAllocationService;
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private IByteDanceAdvertisePlanService planService;
    @Autowired
    private IByteDanceCampaignService campaignService;

    /**
     * 获取快手授权用户信息列表
     * @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;
    }

    /**
     * @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("account_id", campaign.getId()).orderByAsc("id");
        Page<ByteDanceCampaign> page = new Page<>(pageNo, pageSize);
        IPage<ByteDanceCampaign> pageList = campaignService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @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("account_id", plan.getAccountId()).orderByDesc("id");
        Page<ByteDanceAdvertisePlan> page = new Page<>(pageNo, pageSize);
        IPage<ByteDanceAdvertisePlan> pageList = planService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @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);
    }

}
