package cn.com.ctop.kuaishou.modules.batch.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.AesEncryptUtil;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideo;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouVideoMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 快手-本地图片保存
 * @Author: jeecg-boot
 * @Date: 2019-08-14
 * @Version: V1.0
 */
@Slf4j
@Service
public class KuaiShouVideoServiceImpl extends ServiceImpl<KuaiShouVideoMapper, KuaiShouVideo> implements IKuaiShouVideoService {
    @Autowired
    private KuaiShouVideoMapper kuaiShouVideoMapper;
    @Autowired
    private IKuaishouInterfaceService interfaceService;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;

    @Override
    public void insert(JSONObject requestJson) throws Exception {
        if (Check.isNull(requestJson)) {
            throw new Exception("传递参数不能为空");
        }
        Long accountId = requestJson.getLong("accountId");
        CtopOauthToken cTopOauthToken = oauthTokenService.getOauthTokenByAccountId(String.valueOf(accountId));
        if (Check.isNull(cTopOauthToken)) {
            throw new Exception("未获取到token信息");
        }
        String accessToken = cTopOauthToken.getAccessToken();
        JSONArray localArr = requestJson.getJSONArray("localUrl");
        if (!Check.isNull(localArr)) {
            for (int i = 0; i < localArr.size(); i++) {
                KuaiShouVideo video = new KuaiShouVideo();
                video.setVideoDesc(requestJson.getString("videoDesc"));
                String url = localArr.get(i).toString();
                String fileName = url.substring(url.lastIndexOf("/") + 1);
                String fileNameStr = null;
                if (!Check.isNull(fileName)) {
                    String[] split = fileName.split("-");
                    fileNameStr = split[0];
                }
                video.setLoginId(requestJson.getString("loginId"));
                video.setAccountId(accountId);
                video.setVideoName(AesEncryptUtil.getUrlDecoderString(fileNameStr));
                String urlStr;
                if (!url.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                    urlStr = KuaishouInterfaceConstant.HTTPS_PREFIX + url;
                } else {
                    urlStr = url;
                }
                video.setLocalUrl(urlStr);
                video.setMaterialType(requestJson.getString("materialType"));
                video.setPositionType(requestJson.getString("positionType"));
                kuaiShouVideoMapper.insert(video);
                // 多线程上传文件
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        log.info("快手开始上传视频，accountId：{}", accountId);
                        interfaceService.videoUpload(accountId, accessToken, urlStr);
                    }
                };
                thread.start();
            }
        }

    }

    @Override
    public String localInsert(JSONObject requestJson) throws Exception {
        String loginId = requestJson.getString("login_id");
        String videoUrl = requestJson.getString("videoUrl");
        Long accountId = requestJson.getLong("accountId");
        QueryWrapper<KuaiShouVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_id", loginId);
        queryWrapper.eq("local_url", videoUrl);
        queryWrapper.eq("account_id", accountId);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        KuaiShouVideo localVideo = kuaiShouVideoMapper.selectOne(queryWrapper);
        if (Check.isNull(localVideo)) {
            if (!Check.isNull(videoUrl)) {
                KuaiShouVideo video = new KuaiShouVideo();
                video.setVideoDesc(requestJson.getString("description"));
                String fileName = videoUrl.substring(videoUrl.lastIndexOf("/") + 1);
                String fileNameStr = null;
                if (!Check.isNull(fileName)) {
                    String[] split = fileName.split("-");
                    fileNameStr = split[0];
                }

                if (!videoUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                    String url = KuaishouInterfaceConstant.HTTPS_PREFIX + videoUrl;
                    video.setLocalUrl(url);
                } else {
                    video.setLocalUrl(videoUrl);
                }

                video.setLoginId(loginId);
                video.setMaterialType(requestJson.getString("videoType"));
                video.setAccountId(accountId);
                video.setPositionType(requestJson.getString("positionType"));
                video.setVideoName(AesEncryptUtil.getUrlDecoderString(fileNameStr));
                kuaiShouVideoMapper.insert(video);
                return video.getLocalUrl().toString();
            }
        }

        return videoUrl;
    }

}
