package cn.com.ctop.kuaishou.modules.ai.controller;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.ai.entity.KuaiShouVideoLastTime;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaiShouVideoLastTimeService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouVideoRelateCreativesService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "快手-批量工具")
@RestController
@RequestMapping("/kuaishou/material")
public class KuaiShouMaterialControer {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService iKuaishouInterfaceService;
    @Autowired
    private IKuaiShouVideoLastTimeService lastTimeService;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;
    @Autowired
    private IKuaishouVideoRelateCreativesService relateCreativesService;


    @PostMapping(value = "/getKuaiShouVideoList")
    public JSONObject getKuaiShouVideoList(@RequestBody JSONObject requestJson) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参不能为空");
            }
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("账户id不能为空");
            }
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("未获取账号授权信息");
            }

            String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
            iKuaishouInterfaceService.getVideoList(token, nowDate, nowDate);
            returnJson.put("code", 0);
            returnJson.put("message", "素材更新成功");

        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }


    /**
     * 查询上新素材
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/getNewVideoList")
    public JSONObject getNewVideo(@RequestBody JSONObject requestJson) {
        // log.info("获取上新素材入参：{}", requestJson);
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参不能为空");
            }
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("账户id不能为空");
            }
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("未获取账号授权信息");
            }
            String appVersion = requestJson.getString("appVersion");
            Integer channelType = requestJson.getInteger("channelType");
            KuaiShouVideoLastTime videoLastTime = lastTimeService.getById(accountId);
            Date startTime;
            Date endDate = new Date();
            if (!Check.isNull(videoLastTime)) {
                startTime = videoLastTime.getLastTime();
            } else {
                startTime = DateUtils.addTime(endDate, 72);
            }
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("startTime", DateUtils.formatDate(startTime, "yyyy-MM-dd HH:mm:ss"));
            requestMap.put("endTime", DateUtils.formatDate(endDate, "yyyy-MM-dd HH:mm:ss"));
            requestMap.put("accountId", accountId);
            if (!Check.isNull(appVersion)) {
                requestMap.put("appVersion", appVersion);
            }
            if (!Check.isNull(channelType)) {
                requestMap.put("channelType", channelType);
            }
            List<JSONObject> materialList = videoGetService.getVideoListByMap(requestMap);
            if (!Check.isNull(materialList)) {
                for (int i = 0; i < materialList.size(); i++) {
                    JSONObject materialJson = materialList.get(i);
                    String signature = materialJson.getString("signature");
                    List<String> imageList = lastTimeService.getImageListByMd5(signature);
                    materialJson.put("imageList", imageList);
                }
                KuaiShouVideoLastTime lastTime = new KuaiShouVideoLastTime();
                lastTime.setId(accountId);
                lastTime.setLastTime(endDate);
                lastTimeService.saveOrUpdate(lastTime);
                returnJson.put("code", 0);
                returnJson.put("message", "SUCCESS");
                returnJson.put("data", materialList);
            } else {
                returnJson.put("code", -1);
                returnJson.put("message", "暂未新上素材");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }


    /**
     * 历史素材
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/getHistoryVideoList")
    public JSONObject getHistoryVideoList(@RequestBody JSONObject requestJson) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参不能为空");
            }
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("账户id不能为空");
            }
            String startDate = requestJson.getString("startDate");
            if (Check.isNull(startDate)) {
                throw new Exception("请输入开始查询时间");
            }

            String endDate = requestJson.getString("endDate");
            if (Check.isNull(endDate)) {
                throw new Exception("请输入结束查询时间");
            }
            Integer createCount = requestJson.getInteger("createCount");
            if (Check.isNull(createCount) || createCount == 0) {
                throw new Exception("请输入最小关联数");
            }
            Integer num = requestJson.getInteger("num");
            if (Check.isNull(num) || num == 0) {
                throw new Exception("请输入查询条数");
            }
            String appVersion = requestJson.getString("appVersion");
            Integer channelType = requestJson.getInteger("channelType");
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("startTime", startDate);
            requestMap.put("endTime", endDate);
            requestMap.put("accountId", accountId);
            requestMap.put("num", num);
            requestMap.put("createCount", createCount);
            if (!Check.isNull(appVersion)) {
                requestMap.put("appVersion", appVersion);
            }
            if (!Check.isNull(channelType)) {
                requestMap.put("channelType", channelType);
            }
            List<JSONObject> materialList = relateCreativesService.getVideoListByMap(requestMap);
            if (!Check.isNull(materialList)) {
                materialList = videoGetService.getVideoInfoAndImageList(materialList);
                returnJson.put("code", 0);
                returnJson.put("message", "SUCCESS");
                returnJson.put("data", materialList);
            } else {
                returnJson.put("code", -1);
                returnJson.put("message", "暂未新上素材");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }


    /**
     * 历史Top素材
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/getHistoryTopVideoList")
    public JSONObject getHistoryTopVideoList(@RequestBody JSONObject requestJson) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参不能为空");
            }
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("账户id不能为空");
            }
            String startDate = requestJson.getString("startTime");
            if (Check.isNull(startDate)) {
                throw new Exception("请输入开始查询时间");
            }
            String endDate = requestJson.getString("endTime");
            if (Check.isNull(endDate)) {
                throw new Exception("请输入结束查询时间");
            }
            Integer num = requestJson.getInteger("num");
            if (Check.isNull(num) || num == 0) {
                throw new Exception("请输入查询条数");
            }
            String appVersion = requestJson.getString("appVersion");
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("startDate", startDate);
            requestMap.put("endDate", endDate);
            requestMap.put("accountId", accountId);
            Integer channelType = requestJson.getInteger("channelType");
            requestMap.put("num", num);
            if (!Check.isNull(appVersion)) {
                requestMap.put("appVersion", appVersion);
            }
            if (!Check.isNull(channelType)) {
                requestMap.put("channelType", channelType);
            }
            List<JSONObject> materialList = relateCreativesService.getHistoryTopVideoList(requestMap);
            if (!Check.isNull(materialList)) {
                materialList = videoGetService.getVideoInfoAndImageList(materialList);
                returnJson.put("code", 0);
                returnJson.put("message", "SUCCESS");
                returnJson.put("data", materialList);
            } else {
                returnJson.put("code", -1);
                returnJson.put("message", "暂未历史爆量素材");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }


    /**
     * 关联创意数为0素材
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/getCreateZeroVideoList")
    public JSONObject getHistoryCreateVideoList(@RequestBody JSONObject requestJson) {
        //  log.info("获取关联创意数为0素材入参：{}", requestJson);
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参不能为空");
            }
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("账户id不能为空");
            }

            Integer videoCnt = requestJson.getInteger("videoCnt");
            if (Check.isNull(videoCnt)) {
                throw new Exception("请输入查询条数");
            }
            String startTime = requestJson.getString("startTime");
            if (Check.isNull(startTime)) {
                throw new Exception("请输入查询开始时间");
            }
            String endTime = requestJson.getString("endTime");
            if (Check.isNull(endTime)) {
                throw new Exception("请输入查询结束时间");
            }
            String appVersion = requestJson.getString("appVersion");
            Integer channelType = requestJson.getInteger("channelType");
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("startTime", startTime);
            requestMap.put("endTime", endTime);
            requestMap.put("accountId", accountId);
            requestMap.put("videoCnt", videoCnt);
            if (!Check.isNull(appVersion)) {
                requestMap.put("appVersion", appVersion);
            }
            if (!Check.isNull(channelType)) {
                requestMap.put("channelType", channelType);
            }
            List<JSONObject> materialList = relateCreativesService.getVideoZeroListByMap(requestMap);
            if (!Check.isNull(materialList)) {
                materialList = videoGetService.getVideoInfoAndImageList(materialList);
                returnJson.put("code", 0);
                returnJson.put("message", "SUCCESS");
                returnJson.put("data", materialList);
            } else {
                returnJson.put("code", -1);
                returnJson.put("message", "暂未关联创意数为0素材");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }


}
