package com.sqx.modules.course.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sqx.common.utils.*;
import com.sqx.modules.app.entity.InviteAchievement;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.RedisUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.service.InviteAchievementService;
import com.sqx.modules.app.utils.JwtUtils;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.course.dao.CourseCollectDao;
import com.sqx.modules.course.dao.CourseDao;
import com.sqx.modules.course.dao.CourseDetailsDao;
import com.sqx.modules.course.dao.CourseUserDao;
import com.sqx.modules.course.entity.Course;
import com.sqx.modules.course.entity.CourseDetails;
import com.sqx.modules.course.entity.CourseUser;
import com.sqx.modules.course.service.CourseService;
import com.sqx.modules.course.service.CourseUserService;
import com.sqx.modules.course.vo.CourseIn;
import com.sqx.modules.orders.service.OrdersService;
import com.sqx.modules.redisService.RedisService;
import com.sqx.modules.search.service.AppSearchService;
import com.sqx.modules.utils.EasyPoi.ExcelUtils;
import com.sqx.modules.utils.HttpClientUtil;
import com.sqx.modules.utils.SenInfoCheckUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements CourseService {
    @Autowired
    private CourseDetailsDao courseDetailsDao;
    @Autowired
    private CourseCollectDao courseCollectDao;
    @Autowired
    private CourseUserDao courseUserDao;
    @Autowired
    private AppSearchService appSearchService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private CourseUserService courseUserService;
    private final RedisService redisService;
    private final InviteAchievementService inviteAchievementService;


    @Resource
    private RedisUtils redisUtils;

    public CourseServiceImpl(RedisService redisService, InviteAchievementService inviteAchievementService) {
        this.redisService = redisService;
        this.inviteAchievementService = inviteAchievementService;
    }

    private static String getIndexKey(Object... objs) {
        final StrBuilder sb = new StrBuilder();
        for (Object obj : objs) {
            sb.append(obj == null ? "XXX" : Convert.toStr(obj));
            sb.append("_");
        }
        String key = SecureUtil.md5(sb.toString());
        return StrUtil.format("app:index:{}", key);
    }

    private void setCache(String key, String value) {
        key = getIndexKey(key);
        redisUtils.set(key, value, 60 * 30);
    }

    private String getCache(String key) {
        key = getIndexKey(key);
        return redisUtils.get(key);
    }

    /**
     * 创建线程池处理业务逻辑
     */
    private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().build();
    private ExecutorService singleThreadPool = new ThreadPoolExecutor(30, 100,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());


    private static boolean sys = false;

    @Override
    public Result insertCourse(Course course) {
        //设置删除标识
        course.setIsDelete(0);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //设置创建时间
        course.setCreateTime(df.format(new Date()));
        //设置更新时间
        course.setUpdateTime(df.format(new Date()));
        if (course.getCourseType().equals(2) || course.getCourseType().equals(3)) {
            baseMapper.insert(course);
            CourseDetails courseDetails = new CourseDetails();
            courseDetails.setCourseId(course.getCourseId());
            courseDetails.setVideoUrl(course.getRemark());
            courseDetails.setViewCount(0L);
            courseDetails.setPlayCompleteCount(0L);
            courseDetailsDao.insert(courseDetails);
        } else {
            baseMapper.insert(course);
        }
        return Result.success("操作成功！");
    }

    @Override
    public Result updateCourse(Course course) {
        baseMapper.updateById(course);
        return Result.success("操作成功！");
    }

    @Override
    public Result updateDelete(Long id) {
        baseMapper.updateDelete(id);
        return Result.success("操作成功！");
    }

    @Override
    public Result selectCourse(Integer page, Integer limit, Long classifyId, String title, Integer isRecommend, Integer status,
                               Long bannerId, Integer sort, String token, Integer isPrice, Integer admin, Integer over,
                               Integer wxCourse, Integer dyCourse, Integer wxShow, Integer dyShow) {
        Long userId = null;
        if (admin == null) {
            if (StringUtils.isNotEmpty(token)) {
                Claims claims = jwtUtils.getClaimByToken(token);
                if (claims != null && !jwtUtils.isTokenExpired(claims.getExpiration())) {
                    userId = Long.parseLong(claims.getSubject());
                }
            }
        }
        Page<Map<String, Object>> pages = new Page<>(page, limit);
        // 一周的第一天
        DateTime begin = DateUtil.beginOfWeek(new Date());
        String startTime = begin.toString();
        // 一周的最后一天
        DateTime end = DateUtil.endOfWeek(new Date());
        String endTime = end.toString();
        String cacheKey = getIndexKey(admin, userId, page, limit, classifyId, title, isRecommend, status, bannerId,
                sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
        String cache = getCache(cacheKey);
        if (StrUtil.isNotEmpty(cache)) {
            Map<String, Object> data = JSONUtil.toBean(cache, Map.class);
            return Result.success().put("data", data);
        }

        PageHelper.startPage(page, limit);
        if (admin == null) {
            List<Map<String, Object>> map = baseMapper.selectCourse(classifyId, title, isRecommend, status, bannerId,
                    sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(map);
            List<Map<String, Object>> records = pageInfo.getList();
            for (Map<String, Object> m : records) {
                m.put("courseDetailsId", null);
                m.put("courseDetailsName", "");
                m.put("dyEpisodeId", "");
                m.put("wxCourseDetailsId", "");
            }
            PageUtils pageUtils = PageUtils.page(pageInfo);
            setCache(cacheKey, JSONUtil.toJsonStr(pageUtils));
            return Result.success().put("data", pageUtils);
        }
        List<Map<String, Object>> list = baseMapper.selectCourseAdmin(classifyId, title, isRecommend, status, bannerId,
                sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        List<Map<String, Object>> records = pageInfo.getList();
        for (Map<String, Object> map : records) {
            map.put("courseDetailsId", null);
            map.put("courseDetailsName", "");
            map.put("dyEpisodeId", "");
            map.put("wxCourseDetailsId", "");
        }
        PageUtils pageUtils = PageUtils.page(pageInfo);
        setCache(cacheKey, JSONUtil.toJsonStr(pageUtils));
        return Result.success().put("data", pageUtils);
    }

    /*@Override
    public Result selectCourseById(Long id, Long userId) {
        //查询短剧信息
        Course bean = baseMapper.selectById(id);
        if (userId != null) {
            bean.setIsCollect(courseCollectDao.selectCount(new QueryWrapper<CourseCollect>().eq("user_id", userId).eq("course_id", id)));
        } else {
            bean.setIsCollect(0);
        }
        //查询用户是否购买了这本书
        CourseUser courseUser = courseUserDao.selectCourseUser(id, userId);
        Orders one = ordersService.selectOrdersByCourseIdAndUserId(userId,id);
        if (courseUser != null) {
            bean.setListsDetail(courseDetailsDao.findByCourseId(id));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            courseUser.setUpdateTime(df.format(new Date()));
            courseUserDao.updateCourseTime(courseUser);
            bean.setIsMyCourse(2);
            bean.setOrders(one);
        }else{
            if(bean.getCourseType()==null || bean.getCourseType().equals(1)){
                bean.setListsDetail(courseDetailsDao.findByCourseIdNotUrl(id));
            }
            bean.setIsMyCourse(1);
        }
        return Result.success().put("data", bean);
    }*/

    @Override
    public Result selectCourseById(Integer page, Integer limit, Long id, Integer good) {
        PageHelper.startPage(page, limit);
        List<CourseDetails> details = courseDetailsDao.selectCoursePageByCourseId(id, good);
        PageInfo<CourseDetails> pageInfo = new PageInfo<>(details);
        return Result.success().put("data", PageUtils.page(pageInfo));
    }

    @Override
    public Result selectCourseTitle(Integer page, Integer limit, String title, Long userId) {
        //分页
        Page<Map<String, Object>> pages = new Page<>(page, limit);
        if (userId != null) {
            //记录或更新搜索内容
            appSearchService.insetAppSearch(title, userId);
        }
        if (StringUtils.isEmpty(title)) {
            return Result.error("请输入要搜索的内容！");
        }

        PageHelper.startPage(page, limit);
        List<Map<String, Object>> list = baseMapper.selectCourseTitle(title);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return Result.success().put("data", PageUtils.page(pageInfo));
    }

    @Override
    public Result synCourse() {
        if (sys) {
            return Result.error("视频正在同步中，请稍等！");
        }
        sys = true;
        singleThreadPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    String value = commonInfoService.findOne(250).getValue();
                    String s = HttpClientUtil.doGet(value);
                    log.error("返回值：" + s);
                    JSONArray jsonArray = JSONArray.parseArray(s);
                    for (int i = 2; i < jsonArray.size(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String cname = jsonObject.getString("cname");
                        String name = jsonObject.getString("name");
                        String m3u8url = jsonObject.getString("m3u8url");
                        String picurl = jsonObject.getString("picurl");
                        String description = jsonObject.getString("description");
                        Course course = baseMapper.selectOne(new QueryWrapper<Course>().eq("title", cname));
                        if (course == null) {
                            course = new Course();
                            course.setTitle(cname);
                            course.setTitleImg(picurl);
                            course.setPrice(BigDecimal.ZERO);
                            course.setPayNum(0);
                            course.setImg(picurl);
                            course.setDetails(description);
                            course.setIsDelete(0);
                            course.setCreateTime(DateUtils.format(new Date()));
                            course.setUpdateTime(course.getCreateTime());
                            course.setIsRecommend(0);
                            course.setStatus(2);
                            course.setIsPrice(2);
                            course.setViewCounts(0);
                            baseMapper.insert(course);
                        } else {
                            course.setTitle(cname);
                            course.setTitleImg(picurl);
                            course.setImg(picurl);
                            course.setDetails(description);
                            baseMapper.updateById(course);
                        }
                        Integer count = courseDetailsDao.selectCount(new QueryWrapper<CourseDetails>().eq("course_details_name", name));
                        if (count == 0) {
                            CourseDetails courseDetails = new CourseDetails();
                            courseDetails.setCourseId(course.getCourseId());
                            courseDetails.setCourseDetailsName(name);
                            courseDetails.setVideoUrl(m3u8url);
                            courseDetails.setCreateTime(DateUtils.format(new Date()));
                            courseDetails.setTitleImg(picurl);
                            courseDetails.setContent(description);
                            courseDetails.setGoodNum(0);
                            courseDetails.setPrice(BigDecimal.ZERO);
                            courseDetails.setIsPrice(2);
                            courseDetails.setViewCount(0L);
                            courseDetails.setPlayCompleteCount(0L);
                            courseDetailsDao.insert(courseDetails);
                        } else {
                            CourseDetails courseDetails = courseDetailsDao.selectOne(new QueryWrapper<CourseDetails>().eq("course_details_name", name).last(" limit 1"));
                            courseDetails.setCourseDetailsName(name);
                            courseDetails.setVideoUrl(m3u8url);
                            courseDetails.setCreateTime(DateUtils.format(new Date()));
                            courseDetails.setTitleImg(picurl);
                            courseDetails.setContent(description);
                            courseDetailsDao.updateById(courseDetails);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("同步视频出错：" + e.getMessage(), e);
                } finally {
                    sys = false;
                }
            }
        });
        return Result.success();
    }

    @Override
    public Result updateCourseDetails(String ids, BigDecimal price, String content, String titleImg) {
        for (String id : ids.split(",")) {
            CourseDetails courseDetails = courseDetailsDao.selectById(Long.parseLong(id));
            courseDetails.setPrice(price);
            if (price.doubleValue() == 0) {
                courseDetails.setIsPrice(2);
            } else {
                courseDetails.setIsPrice(1);
            }
            courseDetails.setContent(content);
            courseDetails.setTitleImg(titleImg);
            courseDetailsDao.updateById(courseDetails);
        }
        return Result.success();
    }

    @Override
    public Result updateCourseStatus(String ids, Integer status) {
        for (String id : ids.split(",")) {
            Course course = baseMapper.selectById(Long.parseLong(id));
            course.setStatus(status);
            baseMapper.updateById(course);
        }
        return Result.success();
    }


    @Override
    public Result deleteCourseByIds(String ids) {
        for (String id : ids.split(",")) {
            baseMapper.deleteById(Long.parseLong(id));
            courseDetailsDao.delete(new QueryWrapper<CourseDetails>().eq("course_id", Long.parseLong(id)));
        }
        return Result.success();
    }

    @Override
    public Result deleteCourseDetailsByIds(String ids) {
        for (String id : ids.split(",")) {
            courseDetailsDao.deleteById(Long.parseLong(id));
        }
        return Result.success();
    }

    @Override
    public Result courseNotify(Long userId, Long courseId, Long courseDetailsId) {
        CourseUser courseUser = new CourseUser();
        //设置短剧id
        courseUser.setCourseId(courseId);
        courseUser.setCourseDetailsId(courseDetailsId);
        courseUser.setClassify(2);
        //设置用户id
        courseUser.setUserId(userId);

        //加入我的列表
        courseUserService.insertCourseUser(courseUser);
        return Result.success();
    }

    @Override
    public Result dyVideoUpload(Long courseId) {
        uploadVideo(courseId);
        return Result.success();
    }

    @Async
    public void uploadVideo(Long courseId) {
        try {
            String appid = commonInfoService.findOne(805).getValue();
            Course course = baseMapper.selectById(courseId);
            List<CourseDetails> courseDetailsList = courseDetailsDao.selectList(new QueryWrapper<CourseDetails>().eq("course_id", courseId));
            if (StringUtils.isEmpty(course.getDyImgId())) {
                //上传短剧封面图
                String imgUrl = "https://open.douyin.com/api/playlet/v2/resource/upload/";
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("resource_type", 2);
                jsonObject.put("ma_app_id", appid);
                JSONObject image_meta = new JSONObject();
                image_meta.put("url", course.getTitleImg());
                jsonObject.put("image_meta", image_meta);
                String s = HttpClientUtil.doPostJson(imgUrl, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
                JSONObject jsonObject1 = JSONObject.parseObject(s);
                String err_no = jsonObject1.getString("err_no");
                log.info("抖音短剧封面图上传：" + jsonObject1.toJSONString());
                if ("0".equals(err_no)) {
                    JSONObject data = jsonObject1.getJSONObject("data");
                    JSONObject image_result = data.getJSONObject("image_result");
                    String open_pic_id = image_result.getString("open_pic_id");
                    course.setDyImgId(open_pic_id);
                    baseMapper.updateById(course);
                }
            }
            if (StringUtils.isEmpty(course.getDyCourseId())) {
                //创建短剧
                String url = "https://open.douyin.com/api/playlet/v2/video/create/";
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("ma_app_id", appid);
                JSONObject album_info = new JSONObject();
                album_info.put("title", course.getTitle());
                album_info.put("seq_num", courseDetailsList.size());
                JSONArray cover_list = new JSONArray();
                cover_list.add(course.getDyImgId());
                album_info.put("cover_list", cover_list);
                album_info.put("year", DateUtils.format(new Date(), "yyyy"));
                album_info.put("album_status", "3");
                album_info.put("recommendation", course.getTitle());
                album_info.put("desp", course.getDetails());
                JSONArray tag_list = new JSONArray();
                if (course.getCourseLabelIds() != null) {
                    String[] split = course.getCourseLabelIds().split(",");
                    for (String tag : split) {
                        tag_list.add(tag);
                        if (tag_list.size() == 3) {
                            break;
                        }
                    }
                }
                album_info.put("tag_list", tag_list);
                /*if(StringUtils.isNotEmpty(course.getLicenseNum()) || StringUtils.isNotEmpty(course.getRegistrationNum()) ||
                        StringUtils.isNotEmpty(course.getKeyRecordNum()) || StringUtils.isNotEmpty(course.getOrdinaryRecordNum())){
                    album_info.put("qualification","1");
                    JSONObject record_info=new JSONObject();
                    if(StringUtils.isNotEmpty(course.getLicenseNum())){
                        record_info.put("license_num",course.getLicenseNum());
                    }else if(StringUtils.isNotEmpty(course.getRegistrationNum())){
                        record_info.put("registration_num",course.getRegistrationNum());
                    }else if(StringUtils.isNotEmpty(course.getOrdinaryRecordNum())){
                        record_info.put("ordinary_record_num",course.getOrdinaryRecordNum());
                    }else{
                        record_info.put("key_record_num",course.getKeyRecordNum());
                    }
                    album_info.put("record_info",record_info);
                }else{

                }*/
                album_info.put("qualification", "1");
                JSONObject record_audit_info = new JSONObject();
                JSONObject record_material = new JSONObject();
                record_material.put("name", course.getTitle());
                record_material.put("duration", course.getDuration());
                record_material.put("seqs_count", courseDetailsList.size());
                record_material.put("production_organisation", course.getProductionOrganisation());
                String director = course.getDirector();
                String[] directors = director.split(",");
                record_material.put("director", directors);
                String producer = course.getProducer();
                String[] producers = producer.split(",");
                record_material.put("producer", producers);
                String actor = course.getActor();
                String[] actors = actor.split(",");
                record_material.put("actor", actors);
                record_material.put("summary", course.getSummary());

                String imgUrl = "https://open.douyin.com/api/playlet/v2/resource/upload/";
                JSONObject imageJSON = new JSONObject();
                imageJSON.put("resource_type", 2);
                imageJSON.put("ma_app_id", appid);
                JSONObject image_meta = new JSONObject();
                image_meta.put("url", course.getCostDistributionUri());
                imageJSON.put("image_meta", image_meta);
                String s = HttpClientUtil.doPostJson(imgUrl, imageJSON.toJSONString(), SenInfoCheckUtil.getDyToken());
                JSONObject jsonObject1 = JSONObject.parseObject(s);
                String err_no = jsonObject1.getString("err_no");
                log.info("抖音短剧封面图上传：" + jsonObject1.toJSONString());
                if ("0".equals(err_no)) {
                    JSONObject data = jsonObject1.getJSONObject("data");
                    JSONObject image_result = data.getJSONObject("image_result");
                    String open_pic_id = image_result.getString("open_pic_id");
                    record_material.put("cost_distribution_uri", open_pic_id);
                }

                imgUrl = "https://open.douyin.com/api/playlet/v2/resource/upload/";
                imageJSON = new JSONObject();
                imageJSON.put("resource_type", 2);
                imageJSON.put("ma_app_id", appid);
                image_meta = new JSONObject();
                image_meta.put("url", course.getAssuranceUri());
                imageJSON.put("image_meta", image_meta);
                s = HttpClientUtil.doPostJson(imgUrl, imageJSON.toJSONString(), SenInfoCheckUtil.getDyToken());
                jsonObject1 = JSONObject.parseObject(s);
                err_no = jsonObject1.getString("err_no");
                log.info("抖音短剧封面图上传：" + jsonObject1.toJSONString());
                if ("0".equals(err_no)) {
                    JSONObject data = jsonObject1.getJSONObject("data");
                    JSONObject image_result = data.getJSONObject("image_result");
                    String open_pic_id = image_result.getString("open_pic_id");
                    record_material.put("assurance_uri", open_pic_id);
                }

                record_material.put("playlet_production_cost", course.getPlayletProductionCost());
                record_audit_info.put("record_material", record_material);
                album_info.put("record_audit_info", record_audit_info);
                jsonObject.put("album_info", album_info);
                s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
                jsonObject1 = JSONObject.parseObject(s);
                err_no = jsonObject1.getString("err_no");
                log.info("抖音创建短剧：" + jsonObject1.toJSONString());
                if ("0".equals(err_no)) {
                    JSONObject data = jsonObject1.getJSONObject("data");
                    String album_id = data.getString("album_id");
                    course.setDyCourseId(album_id);
                    baseMapper.updateById(course);
                }
            }
            //上传视频
            for (CourseDetails courseDetails : courseDetailsList) {

                if (StringUtils.isEmpty(courseDetails.getDyCourseDetailsId()) || courseDetails.getDyUrlStatus() == 3) {
                    String imgUrl = "https://open.douyin.com/api/playlet/v2/resource/upload/";
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("resource_type", 1);
                    jsonObject.put("ma_app_id", appid);
                    JSONObject video_meta = new JSONObject();
                    video_meta.put("url", courseDetails.getVideoUrl());
                    video_meta.put("title", courseDetails.getCourseDetailsName());
                    video_meta.put("description", courseDetails.getContent());
                    if (courseDetails.getVideoUrl().contains(".mp4")) {
                        video_meta.put("format", "mp4");
                    } else {
                        video_meta.put("format", "m3u8");
                    }
                    jsonObject.put("video_meta", video_meta);
                    String s = HttpClientUtil.doPostJson(imgUrl, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
                    JSONObject jsonObject1 = JSONObject.parseObject(s);
                    log.info("抖音上传短剧视频：" + jsonObject1.toJSONString());
                    String err_no = jsonObject1.getString("err_no");
                    if (!"0".equals(err_no)) {
                        continue;
                    }
                    JSONObject data = jsonObject1.getJSONObject("data");
                    JSONObject video_result = data.getJSONObject("video_result");
                    String open_video_id = video_result.getString("open_video_id");
                    courseDetails.setDyCourseDetailsId(open_video_id);
                    courseDetails.setDyUrlStatus(1);
                    courseDetailsDao.updateById(courseDetails);
                }
                if (StringUtils.isEmpty(courseDetails.getDyImgId())) {
                    //上传短剧封面图
                    String imgUrl = "https://open.douyin.com/api/playlet/v2/resource/upload/";
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("resource_type", 2);
                    jsonObject.put("ma_app_id", appid);
                    JSONObject image_meta = new JSONObject();
                    image_meta.put("url", courseDetails.getTitleImg());
                    jsonObject.put("image_meta", image_meta);
                    String s = HttpClientUtil.doPostJson(imgUrl, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
                    JSONObject jsonObject1 = JSONObject.parseObject(s);
                    log.info("抖音上传短剧视频封面：" + jsonObject1.toJSONString());
                    String err_no = jsonObject1.getString("err_no");
                    if (!"0".equals(err_no)) {
                        continue;
                    }
                    JSONObject data = jsonObject1.getJSONObject("data");
                    JSONObject image_result = data.getJSONObject("image_result");
                    String open_pic_id = image_result.getString("open_pic_id");
                    courseDetails.setDyImgId(open_pic_id);
                    courseDetailsDao.updateById(courseDetails);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("抖音上传视频出错：" + e.getMessage(), e);
        }

    }

    @Override
    public Result dyVideoAudit(Long courseId) {
        String appid = commonInfoService.findOne(805).getValue();
        Course course = baseMapper.selectById(courseId);
        String url = "https://open.douyin.com/api/playlet/v2/video/review/";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("album_id", course.getDyCourseId());
        jsonObject.put("ma_app_id", appid);
        String s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
        JSONObject jsonObject1 = JSONObject.parseObject(s);
        String err_no = jsonObject1.getString("err_no");
        if (!"0".equals(err_no)) {
            return Result.error(jsonObject1.getString("err_msg"));
        }
        JSONObject data = jsonObject1.getJSONObject("data");
        String version = data.getString("version");
        course.setDyVersion(version);
        course.setDyStatus(1);
        baseMapper.updateById(course);
        return Result.success();
    }

    @Override
    public Result dyVideoUp(Long courseId) {
        Course course = baseMapper.selectById(courseId);
        String url = "https://open.douyin.com/api/playlet/v2/album/online/";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("album_id", course.getDyCourseId());
        jsonObject.put("operate", "2");
        jsonObject.put("version", course.getDyVersion());
        String s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
        JSONObject jsonObject1 = JSONObject.parseObject(s);
        String err_no = jsonObject1.getString("err_no");
        if (!"0".equals(err_no)) {
            return Result.error(jsonObject1.getString("err_msg"));
        }
        course.setDyStatus(4);
        baseMapper.updateById(course);
        return Result.success();
    }

    @Override
    public Result setDyNotifyUrl(String notifyUrl) {
        notifyUrl += "/sqx_fast/app/course/notifyUrl";
        String url = "https://open.douyin.com/api/industry/v1/solution/set_impl";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("operator", "管理员");
        jsonObject.put("release_reason", "配置短剧回调地址");
        JSONArray industry_impl_list = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("template_id", 20001);
        JSONArray open_ability_impl_list = new JSONArray();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("ability_identity", "/msg/playlet/review/notify");
        jsonObject2.put("is_delete", false);
        jsonObject2.put("test_url", notifyUrl);
        jsonObject2.put("prod_url", notifyUrl);
        jsonObject2.put("ability_type", "2");
        jsonObject2.put("impl_name", "短剧回调消息实现配置");
        open_ability_impl_list.add(jsonObject2);
        jsonObject1.put("open_ability_impl_list", open_ability_impl_list);
        industry_impl_list.add(jsonObject1);
        jsonObject.put("industry_impl_list", industry_impl_list);
        jsonObject.put("app_config_item_list", new JSONArray());
        String s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
        log.error("抖音设置回调域名返回值：" + s);
        JSONObject jsonObject3 = JSONObject.parseObject(s);
        JSONObject data = jsonObject3.getJSONObject("data");
        String err_no = data.getString("error_code");
        if (!"0".equals(err_no)) {
            return Result.error(data.getString("description"));
        }
        return Result.success();
    }

    @Override
    public JSONObject notifyUrl(JSONObject jsonObject) {
        log.info("抖音视频回调：" + jsonObject.toJSONString());
        String type = jsonObject.getString("type");
        JSONObject msg = jsonObject.getJSONObject("msg");
        if ("upload_video".equals(type)) {
            String open_video_id = msg.getString("open_video_id");
            String success = msg.getString("success");
            CourseDetails courseDetails = courseDetailsDao.selectOne(new QueryWrapper<CourseDetails>().eq("dy_course_details_id", open_video_id));
            if (courseDetails != null) {
                if ("true".equals(success)) {
                    courseDetails.setDyUrlStatus(2);
                } else {
                    courseDetails.setDyUrlStatus(3);
                }
                courseDetailsDao.updateById(courseDetails);
                //获取是否所有的集都上传成功 如果成功 则添加集
                Integer count = courseDetailsDao.selectCount(new QueryWrapper<CourseDetails>()
                        .eq("course_id", courseDetails.getCourseId()).in("dy_url_status", 1, 3));
                log.info("count" + count);
                if (count == 0) {
                    videoEdit(courseDetails);
                }
            }
        } else if ("album_audit".equals(type)) {
            String album_id = msg.getString("album_id");
            Course course = baseMapper.selectOne(new QueryWrapper<Course>().eq("dy_course_id", album_id));
            if (course != null) {
                String audit_status = msg.getString("audit_status");
                if ("1".equals(audit_status)) {
                    course.setDyStatus(3);
                    course.setDyStatusContent(msg.getString("audit_msg"));
                } else {
                    course.setDyStatus(2);
                    course.setDyVersion(msg.getString("version"));
                }
                baseMapper.updateById(course);
            }
        }
        if ("episode_audit".equals(type)) {
            String episode_id = msg.getString("episode_id");
            CourseDetails courseDetails = courseDetailsDao.selectOne(new QueryWrapper<CourseDetails>().eq("dy_episode_id", episode_id));
            if (courseDetails != null) {
                String audit_status = msg.getString("audit_status");
                if ("1".equals(audit_status)) {
                    courseDetails.setDyStatus(3);
                    courseDetails.setDyStatusContent(msg.getString("audit_msg"));
                } else {
                    courseDetails.setDyStatus(2);
                    courseDetails.setDyVersion(msg.getString("version"));
                }
                courseDetailsDao.updateById(courseDetails);
            }
        }
        JSONObject result = new JSONObject();
        result.put("err_no", 0);
        result.put("err_tips", "success");
        return result;
    }

    @Async
    public void videoEdit(CourseDetails courseDetails) {
        //这里进行延迟操作 抖音不允许并发操作
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("延迟结束");
        //没有待审核和审核失败的 吧所有的集进行上传
        List<CourseDetails> courseDetailsList = courseDetailsDao.selectList(new QueryWrapper<CourseDetails>()
                .eq("course_id", courseDetails.getCourseId()).orderByAsc("create_time"));
        String appid = commonInfoService.findOne(805).getValue();
        Course course = baseMapper.selectById(courseDetails.getCourseId());
        String url = "https://open.douyin.com/api/playlet/v2/video/edit/";
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("album_id", course.getDyCourseId());
        jsonObject1.put("ma_app_id", appid);
        int num = 1;
        JSONArray episode_info_list = new JSONArray();
        while (true) {

            CourseDetails courseDetails1 = courseDetailsList.get(num - 1);
            JSONObject episode = new JSONObject();
            episode.put("title", courseDetails1.getCourseDetailsName());
            episode.put("seq", num);
            JSONArray cover_list = new JSONArray();
            cover_list.add(courseDetails1.getDyImgId());
            episode.put("cover_list", cover_list);
            episode.put("open_video_id", courseDetails1.getDyCourseDetailsId());
            episode_info_list.add(episode);
            if (courseDetailsList.size() == num) {
                jsonObject1.put("episode_info_list", episode_info_list);
                String s = HttpClientUtil.doPostJson(url, jsonObject1.toJSONString(), SenInfoCheckUtil.getDyToken());
                log.info("绑定集：" + s);
                JSONObject jsonObject2 = JSONObject.parseObject(s);
                JSONObject data = jsonObject2.getJSONObject("data");
                JSONObject episode_id_map = data.getJSONObject("episode_id_map");
                Map<String, Object> map = new HashMap<>(episode_id_map);
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    CourseDetails courseDetails2 = courseDetailsList.get(Integer.parseInt(key) - 1);
                    courseDetails2.setDyEpisodeId(String.valueOf(value));
                    courseDetailsDao.updateById(courseDetails2);
                }
                break;
            }
            if (episode_info_list.size() % 100 == 0) {
                jsonObject1.put("episode_info_list", episode_info_list);
                String s = HttpClientUtil.doPostJson(url, jsonObject1.toJSONString(), SenInfoCheckUtil.getDyToken());
                JSONObject jsonObject2 = JSONObject.parseObject(s);
                log.info("绑定集100：" + s);
                JSONObject data = jsonObject2.getJSONObject("data");
                JSONObject episode_id_map = data.getJSONObject("episode_id_map");
                Map<String, Object> map = new HashMap<>(episode_id_map);
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    CourseDetails courseDetails2 = courseDetailsList.get(Integer.parseInt(key) - 1);
                    courseDetails2.setDyEpisodeId(String.valueOf(value));
                    courseDetailsDao.updateById(courseDetails2);
                }
                episode_info_list = new JSONArray();
            }
            num++;
        }
        //对页面进行绑定 这里同样进行延迟操作
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        url = "https://open.douyin.com/api/playlet/v2/album/bind/";
        jsonObject1 = new JSONObject();
        jsonObject1.put("schema_bind_type", "1");

        for (CourseDetails courseDetails1 : courseDetailsList) {
            JSONObject single_schema_bind = new JSONObject();
            single_schema_bind.put("album_id", course.getDyCourseId());
            single_schema_bind.put("episode_id", courseDetails1.getDyEpisodeId());
            single_schema_bind.put("path", "me/detail/detail");
            JSONArray params = new JSONArray();
            JSONObject param1 = new JSONObject();
            param1.put("key", "id");
            param1.put("value", String.valueOf(course.getCourseId()));
            JSONObject param2 = new JSONObject();
            param2.put("key", "courseDetailsId");
            param2.put("value", String.valueOf(courseDetails1.getCourseDetailsId()));
            params.add(param1);
            params.add(param2);
            single_schema_bind.put("params", params);
            jsonObject1.put("single_schema_bind", single_schema_bind);
            String s = HttpClientUtil.doPostJson(url, jsonObject1.toJSONString(), SenInfoCheckUtil.getDyToken());
            log.info("绑定页面返回值：" + s);
            //每次绑定完页面后 延迟后进行操作
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public Result uploadCourseDetails(Long courseDetailsId) {
        String appid = commonInfoService.findOne(805).getValue();
        CourseDetails courseDetails = courseDetailsDao.selectById(courseDetailsId);
        String imgUrl = "https://open.douyin.com/api/playlet/v2/resource/upload/";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resource_type", 1);
        jsonObject.put("ma_app_id", appid);
        JSONObject video_meta = new JSONObject();
        video_meta.put("url", courseDetails.getVideoUrl());
        video_meta.put("title", courseDetails.getCourseDetailsName());
        video_meta.put("description", courseDetails.getContent());
        if (courseDetails.getVideoUrl().contains("mp4")) {
            video_meta.put("format", "mp4");
        } else {
            video_meta.put("format", "m3u8");
        }
        jsonObject.put("video_meta", video_meta);
        String s = HttpClientUtil.doPostJson(imgUrl, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
        JSONObject jsonObject1 = JSONObject.parseObject(s);
        String err_no = jsonObject1.getString("err_no");
        if (!"0".equals(err_no)) {
            return Result.error(jsonObject1.getString("err_msg"));
        }
        JSONObject data = jsonObject1.getJSONObject("data");
        JSONObject video_result = data.getJSONObject("video_result");
        String open_video_id = video_result.getString("open_video_id");
        courseDetails.setDyCourseDetailsId(open_video_id);
        courseDetails.setDyUrlStatus(1);
        courseDetailsDao.updateById(courseDetails);
        //上传短剧封面图
        imgUrl = "https://open.douyin.com/api/playlet/v2/resource/upload/";
        jsonObject = new JSONObject();
        jsonObject.put("resource_type", 2);
        jsonObject.put("ma_app_id", appid);
        JSONObject image_meta = new JSONObject();
        image_meta.put("url", courseDetails.getTitleImg());
        jsonObject.put("image_meta", image_meta);
        s = HttpClientUtil.doPostJson(imgUrl, jsonObject.toJSONString(), SenInfoCheckUtil.getDyToken());
        jsonObject1 = JSONObject.parseObject(s);
        err_no = jsonObject1.getString("err_no");
        if (!"0".equals(err_no)) {
            return Result.error(jsonObject1.getString("err_msg"));
        }
        data = jsonObject1.getJSONObject("data");
        JSONObject image_result = data.getJSONObject("image_result");
        String open_pic_id = image_result.getString("open_pic_id");
        courseDetails.setDyImgId(open_pic_id);
        courseDetailsDao.updateById(courseDetails);
        return Result.success();
    }

    @Override
    public Result updateDyCourse(Course course) {
        baseMapper.updateById(course);
        if (course.getDyCourseId() != null) {
            //已经提交抖音 同步抖音
            String appid = commonInfoService.findOne(805).getValue();
            String url = "https://open.douyin.com/api/playlet/v2/video/edit/";
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("album_id", course.getDyCourseId());
            jsonObject1.put("ma_app_id", appid);
            List<CourseDetails> courseDetailsList = courseDetailsDao.selectList(new QueryWrapper<CourseDetails>().eq("course_id", course.getCourseId()));
            JSONObject album_info = new JSONObject();
            album_info.put("title", course.getTitle());
            album_info.put("seq_num", courseDetailsList.size());
            JSONArray cover_list = new JSONArray();
            cover_list.add(course.getDyImgId());
            album_info.put("cover_list", cover_list);
            album_info.put("year", DateUtils.format(new Date(), "yyyy"));
            album_info.put("album_status", "3");
            album_info.put("recommendation", course.getTitle());
            album_info.put("desp", course.getDetails());
            JSONArray tag_list = new JSONArray();
            if (course.getCourseLabelIds() != null) {
                String[] split = course.getCourseLabelIds().split(",");
                for (String tag : split) {
                    tag_list.add(tag);
                    if (tag_list.size() == 3) {
                        break;
                    }
                }
            }
            album_info.put("tag_list", tag_list);
            if (StringUtils.isNotEmpty(course.getLicenseNum()) || StringUtils.isNotEmpty(course.getRegistrationNum()) ||
                    StringUtils.isNotEmpty(course.getKeyRecordNum()) || StringUtils.isNotEmpty(course.getOrdinaryRecordNum())) {
                album_info.put("qualification", "2");
                JSONObject record_info = new JSONObject();
                if (StringUtils.isNotEmpty(course.getLicenseNum())) {
                    record_info.put("license_num", course.getLicenseNum());
                } else if (StringUtils.isNotEmpty(course.getRegistrationNum())) {
                    record_info.put("registration_num", course.getRegistrationNum());
                } else if (StringUtils.isNotEmpty(course.getOrdinaryRecordNum())) {
                    record_info.put("ordinary_record_num", course.getOrdinaryRecordNum());
                } else {
                    record_info.put("key_record_num", course.getKeyRecordNum());
                }
                album_info.put("record_info", record_info);
            } else {
                album_info.put("qualification", "1");
            }

            jsonObject1.put("album_info", album_info);
            jsonObject1.put("episode_info_list", new JSONArray());
            String s = HttpClientUtil.doPostJson(url, jsonObject1.toJSONString(), SenInfoCheckUtil.getDyToken());
            JSONObject jsonObject2 = JSONObject.parseObject(s);
            String err_no = jsonObject2.getString("err_no");
            if (!"0".equals(err_no)) {
                return Result.error(jsonObject2.getString("err_msg"));
            }
        }
        return Result.success();
    }

    @Override
    public Result sysWxCourse(Integer freeNum, BigDecimal coursePrice, Integer maxGood, Integer minGood) {
        singleThreadPool.submit(() -> sysWxCourses(freeNum, coursePrice, maxGood, minGood));

        return Result.success();
    }


    public void sysWxCourses(Integer freeNum, BigDecimal coursePrice, Integer maxGood, Integer minGood) {
        try {
            int offset = 0;
            String value = commonInfoService.findOne(887).getValue();
            String http = commonInfoService.findOne(19).getValue();
            Random rand = new Random();
            while (true) {
                String url = "https://api.weixin.qq.com/wxa/sec/vod/listdramas?access_token=" + SenInfoCheckUtil.getMpToken();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("offset", offset);
                jsonObject.put("limit", 100);
                String s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString());
                JSONObject jsonObject1 = JSONObject.parseObject(s);
                log.error("微信同步短剧返回值：" + jsonObject1.toJSONString());
                String errcode = jsonObject1.getString("errcode");
                if (!"0".equals(errcode)) {
                    log.error("同步微信短剧失败：" + jsonObject1.getString("errmsg"));
                    break;
                }
                JSONArray drama_info_list = jsonObject1.getJSONArray("drama_info_list");
                if (drama_info_list == null || drama_info_list.size() == 0) {
                    break;
                }

                for (int i = 0; i < drama_info_list.size(); i++) {
                    JSONObject jsonObject2 = drama_info_list.getJSONObject(i);
                    //剧目状态。0为正常可播；1为审核中；2为审核失败；3为平台下架
                    String status = jsonObject2.getString("status");
                    if (!"0".equals(status)) {
                        continue;
                    }
                    String name = jsonObject2.getString("name");
                    name = name.replace(" ", "");
                    Course course = baseMapper.selectOne(new QueryWrapper<Course>().eq("title", name).eq("is_delete", 0));
                    if (course != null) {
                        course.setWxCourseId(jsonObject2.getString("drama_id"));
                        baseMapper.updateById(course);
                    } else {

                        String[] split = value.split(",");
                        int min = 0; // 指定范围的最小值（包含）
                        int max = split.length - 1; // 指定范围的最大值（包含）
                        int randomNum1 = min + rand.nextInt(max - min + 1);
                        int randomNum2 = min + rand.nextInt(max - min + 1);
                        int randomNum3 = min + rand.nextInt(max - min + 1);
                        String courseLabel = split[randomNum1] + "," + split[randomNum2] + "," + split[randomNum3];
                        String cover_url = jsonObject2.getString("cover_url");

                        //读取微信图片 保存到服务器本地
                        String[] splits = http.split("://");
                        // 上传文件路径
                        String filePath = "/www/wwwroot/" + splits[1] + "/file/uploadPath/duanju/" + name + "/" + name + ".jpg";
                        File file = new File(filePath);
                        if (!file.getParentFile().exists()) {
                            file.getParentFile().mkdirs();
                        }

                        HttpClientUtil.downloadImage(cover_url, filePath);
                        String img = http + "/file/uploadPath/duanju/" + name + "/" + name + ".jpg";
                        ;

                        course = new Course();
                        course.setTitle(name);
                        course.setTitleImg(img);
                        course.setPrice(coursePrice);
                        course.setCourseLabel(courseLabel);
                        course.setPayNum(0);
                        course.setImg(img);
                        course.setDetails(name);
                        course.setIsDelete(0);
                        course.setCreateTime(DateUtils.format(new Date()));
                        course.setUpdateTime(course.getCreateTime());
                        course.setIsRecommend(0);
                        course.setStatus(1);
                        course.setIsPrice(1);
                        course.setViewCounts(0);
                        course.setIsOver(1);
                        course.setWxCourseId(jsonObject2.getString("drama_id"));
                        course.setWxShow(1);
                        baseMapper.insert(course);
                    }
                }
                offset = offset + 100;
            }
            //同步集
            List<Course> courseList = baseMapper.selectList(new QueryWrapper<Course>().isNotNull("wx_course_id"));
            for (Course course : courseList) {
                offset = 0;
                while (true) {
                    String url = "https://api.weixin.qq.com/wxa/sec/vod/listmedia?access_token=" + SenInfoCheckUtil.getMpToken();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("drama_id", Integer.parseInt(course.getWxCourseId()));
                    jsonObject.put("limit", 100);
                    jsonObject.put("offset", offset);
                    String s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString());
                    JSONObject jsonObject1 = JSONObject.parseObject(s);
                    String errcode = jsonObject1.getString("errcode");
                    if (!"0".equals(errcode)) {
                        log.error("同步微信短剧集失败：" + jsonObject1.getString("errmsg"));
                        break;
                    }
                    JSONArray media_info_list = jsonObject1.getJSONArray("media_info_list");
                    if (media_info_list == null || media_info_list.size() == 0) {
                        break;
                    }

                    int priceNum = media_info_list.size() - freeNum;
                    BigDecimal courseDetailsPrice = BigDecimal.ZERO;
                    if (priceNum > 0) {
                        courseDetailsPrice = coursePrice.divide(BigDecimal.valueOf(priceNum), 2, BigDecimal.ROUND_UP);
                    }
                    for (int i = 0; i < media_info_list.size(); i++) {
                        JSONObject jsonObject2 = media_info_list.getJSONObject(i);
                        String name = jsonObject2.getString("name");
                        name = name.replace(".mp4", "");
                        name = name.replace(".avi", "");
                        name = name.replace(".wmv", "");
                        name = name.replace(".flv", "");
                        name = name.replace(".m3u8", "");
                        name = name.replace(".mov", "");
                        name = name.replace(" ", "");
                        CourseDetails courseDetails = courseDetailsDao.selectOne(new QueryWrapper<CourseDetails>()
                                .eq("course_id", course.getCourseId()).like("course_details_name", name));
                        if (courseDetails != null) {
                            courseDetails.setWxCourseDetailsId(jsonObject2.getString("media_id"));
                            courseDetailsDao.updateById(courseDetails);
                        } else {
                            String cover_url = jsonObject2.getString("cover_url");

                            //读取微信图片 保存到服务器本地
                            String[] splits = http.split("://");
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                            // 上传文件路径
                            String filePath = "/www/wwwroot/" + splits[1] + "/file/uploadPath/duanju/" + course.getTitle() + "/" + name + ".jpg";
                            ;
                            File file = new File(filePath);
                            if (!file.getParentFile().exists()) {
                                file.getParentFile().mkdirs();
                            }

                            HttpClientUtil.downloadImage(cover_url, filePath);
                            String img = http + "/file/uploadPath/duanju/" + course.getTitle() + "/" + name + ".jpg";
                            ;

                            int goodNum = minGood + rand.nextInt(maxGood - minGood + 1);
                            courseDetails = new CourseDetails();
                            courseDetails.setWxCourseDetailsId(jsonObject2.getString("media_id"));
                            courseDetails.setCourseId(course.getCourseId());
                            courseDetails.setCourseDetailsName(name);
                            courseDetails.setCreateTime(DateUtils.format(new Date()));
                            courseDetails.setTitleImg(img);
                            courseDetails.setContent(name);
                            courseDetails.setGoodNum(goodNum);
                            courseDetails.setViewCount(0L);
                            courseDetails.setPlayCompleteCount(0L);
                            if (i <= freeNum) {
                                courseDetails.setPrice(BigDecimal.ZERO);
                                courseDetails.setIsPrice(2);
                            } else {
                                courseDetails.setPrice(courseDetailsPrice);
                                courseDetails.setIsPrice(1);
                            }
                            courseDetails.setSort(i);
                            courseDetailsDao.insert(courseDetails);
                        }
                    }
                    offset = offset + 100;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("同步微信短剧失败：" + e.getMessage(), e);
        }

    }


    @Override
    public Result selectWxVideoUrl(String wxCourseDetailsIds) {
        List<Map<String, String>> list = new ArrayList<>();
        for (String wxCourseDetailsIdStr : wxCourseDetailsIds.split(",")) {
            Long wxCourseDetailsId = Long.parseLong(wxCourseDetailsIdStr);
            String url = "https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token=" + SenInfoCheckUtil.getMpToken();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("media_id", wxCourseDetailsId);
            jsonObject.put("t", (new Date().getTime() / 1000) + 7200);
            String s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString());
            JSONObject jsonObject1 = JSONObject.parseObject(s);
            String errcode = jsonObject1.getString("errcode");
            if (!"0".equals(errcode)) {
                log.error(wxCourseDetailsIdStr + "   获取微信播放链接失败：" + jsonObject1.getString("errmsg"));
//                return Result.error("获取微信播放链接失败："+jsonObject1.getString("errmsg"));
                continue;
            }
            JSONObject media_info = jsonObject1.getJSONObject("media_info");
            String duration = media_info.getString("duration");
            String cover_url = media_info.getString("cover_url");
            String mp4_url = media_info.getString("mp4_url");
            Map<String, String> result = new HashMap<>();
            result.put("duration", duration);
            result.put("cover_url", cover_url);
            result.put("mp4_url", mp4_url);
            result.put("wxCourseDetailsId", String.valueOf(wxCourseDetailsId));
            list.add(result);
        }
        return Result.success().put("data", list);
    }

    @Override
    public Result uploadWxCourse(Long courseId, Integer qualificationType, String registrationNumber,
                                 String qualificationCertificateMaterialId, String costOfProduction, String costCommitmentLetterMaterialId) {
        String url = "https://api.weixin.qq.com/wxa/sec/vod/modifydramabasicinfo?access_token=" + SenInfoCheckUtil.getMpToken();

        //储存信息
        Course course = baseMapper.selectById(courseId);
        course.setQualificationType(qualificationType);
        course.setRegistrationNumber(registrationNumber);
        course.setQualificationCertificateMaterialId(qualificationCertificateMaterialId);
        course.setCostOfProduction(costOfProduction);
        course.setCostCommitmentLetterMaterialId(costCommitmentLetterMaterialId);
        course.setWxCourseStatus(1);
        baseMapper.updateById(course);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("drama_id", course.getWxCourseId());
        jsonObject.put("qualification_type", qualificationType);
        jsonObject.put("registration_number", registrationNumber);
        jsonObject.put("qualification_certificate_material_id", qualificationCertificateMaterialId);
        jsonObject.put("cost_of_production", costOfProduction);
        jsonObject.put("cost_commitment_letter_material_id", costCommitmentLetterMaterialId);
        String s = HttpClientUtil.doPostJson(url, jsonObject.toJSONString());
        log.error("提交微信备案审核：" + s);
        JSONObject jsonObject1 = JSONObject.parseObject(s);
        String errcode = jsonObject1.getString("errcode");
        if (!"0".equals(errcode)) {
            return Result.error(jsonObject1.getString("errmsg"));
        }
        return Result.success();
    }


    @Override
    public Result courseListExcelIn(MultipartFile file) throws IOException {
        List<CourseIn> courseList = ExcelUtils.importExcel(file, 2, 1, CourseIn.class);
        if (CollectionUtils.isEmpty(courseList)) {
            return Result.error("Excel数据为空，excel转化失败！");
        }
        //当前行索引（Excel的数据从第几行开始,就填写几）
        int index = 4;
        //失败条数
        int repeat = 0;
        //成功条数
        int successIndex = 0;
        //空数据
        int emptyCount = 0;
        for (CourseIn courseIn : courseList) {
            if (courseIn.getTitle() == null) {
                emptyCount++;
                continue;
            }
            if (courseIn.getIsPrice() == 1) {
                if (courseIn.getPrice() == null || courseIn.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
                    return Result.error("第【" + index + "】行数据为收费短剧,但并没有设置价格");
                }
            }
            index++;
            Integer count = baseMapper.selectCount(new QueryWrapper<Course>().eq("title", courseIn.getTitle()));
            if (count <= 0) {
                Course course = new Course();
                BeanUtils.copyProperties(courseIn, course);
                course.setCreateTime(DateUtils.format(new Date()));
                course.setUpdateTime(DateUtils.format(new Date()));
                int result = baseMapper.insert(course);
                if (result > 0) {
                    successIndex++;
                }
            } else {
                repeat++;
            }

        }
        return Result.success("导入成功,共新增【" + successIndex + "】条,失败【" + (courseList.size() - successIndex - emptyCount) + "】条,其中过滤重复数据【" + repeat + "】条");

    }

    @Override
    public Result viewCourse(Long courseId, Long courseDetailsId, String type) {
        Course course = baseMapper.selectById(courseId);
        if (course == null) {
            return Result.error("短剧不存在");
        }

        CourseDetails details = courseDetailsDao.selectById(courseDetailsId);
        if (details == null) {
            return Result.error("短剧集不存在");
        }

        if ("start".equals(type)) {
            //开始播放
            details.setViewCount(details.getViewCount() + 1);
            courseDetailsDao.updateById(details);
            baseMapper.updateById(course);
        } else if ("end".equals(type)) {
            //播放完成
            details.setPlayCompleteCount(details.getPlayCompleteCount() + 1);
            courseDetailsDao.updateById(details);
        }

        return Result.success();
    }

    @Override
    public Result getRedEnvelopeTips(Long userId) {
        Integer count = ordersService.countOrderNum(userId, DateUtil.formatDateTime(DateUtil.beginOfDay(new Date())));

        String totalCount = commonInfoService.findOne(901).getValue();

        String data = String.format("每日前%s次付款均可获取抽奖机会，抽奖保底抽中付款金额等额红包，红包可直接提现。当前为第%d次付款", totalCount, count + 1);

        return Result.success().put("data", data);
    }

    @Override
    public void incrWeekPayCount(Long courseId) {
        if (ApiAccessLimitUtil.isAccessAllowed("setWeekPayCount", courseId.toString(), 1, 600)) {
            int count = redisService.getCourseWeekPayCount(courseId);
            update(null, new LambdaUpdateWrapper<Course>()
                    .eq(Course::getCourseId, courseId)
                    .set(Course::getWeekPay, count));
        }
    }
}
