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

import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.common.module.service.IMaterialAscriptionService;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.service.ISysUserService;
import cn.com.ctop.common.module.service.IUserCompanyService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import cn.com.ctop.kuaishou.modules.material.entity.EtlKuaishouVideoInfo;
import cn.com.ctop.kuaishou.modules.material.mapper.EtlKuaishouVideoInfoMapper;
import cn.com.ctop.kuaishou.modules.material.service.IEtlKuaishouVideoInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.entity.SysUser;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 快手视频报表信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-02-20
 */
@Service
@Slf4j
public class EtlKuaishouVideoInfoServiceImpl extends ServiceImpl<EtlKuaishouVideoInfoMapper, EtlKuaishouVideoInfo> implements IEtlKuaishouVideoInfoService {
    @Resource
    private EtlKuaishouVideoInfoMapper etlKuaishouVideoInfoMapper;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;
    @Autowired
    private IMaterialAscriptionService ascriptionService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMaterialInfoService materialInfoService;
    @Autowired
    private IUserCompanyService companyService;
    ExecutorService executorService = Executors.newFixedThreadPool(5);

    @Override
    public void etlKuaishouVideoInfo(String date) {
        Date startDate = DateUtils.getStartDate(date);
        Date endDate = DateUtils.getEndDate(date);
        List<JSONObject> videoList = videoGetService.getVideoListByDate(startDate, endDate);
        if (Check.isNull(videoList)) {
            return;
        }
        log.info("日期：{}，视频数量：{}", date, videoList.size());
        List<List<JSONObject>> partition = Lists.partition(videoList, 500);
        for (int i = 0; i < partition.size(); i++) {
            List<JSONObject> videos = partition.get(i);
            executorService.submit(() -> {
                List<EtlKuaishouVideoInfo> addList = new ArrayList<>();
                for (int j = 0; j < videos.size(); j++) {
                    JSONObject videoJson = videos.get(j);
                    if (Check.isNull(videoJson)) {
                        continue;
                    }
                    String signature = videoJson.getString("signature");
                    if (Check.isNull(signature)) {
                        continue;
                    }
                    try {
                        //1:查询素材名称
                        EtlKuaishouVideoInfo etlKuaishouVideoInfo = new EtlKuaishouVideoInfo();
                        MaterialInfo materialInfo = materialInfoService.getMaterialInfoByCode(signature);
                        if (null != materialInfo) {
                            etlKuaishouVideoInfo.setVideoName(materialInfo.getMaterialName());
                        } else {
                            etlKuaishouVideoInfo.setVideoName(videoJson.getString("photoName"));
                        }
                        etlKuaishouVideoInfo.setVideoCode(signature);
                        etlKuaishouVideoInfo.setUrl(videoJson.getString("url"));
                        etlKuaishouVideoInfo.setCoverUrl(videoJson.getString("coverUrl"));
                        String channelType = videoJson.getString("channelType");
                        etlKuaishouVideoInfo.setChannelType(channelType);
                        if (channelType.equals("0")) {
                            etlKuaishouVideoInfo.setChannelName("内部");
                        } else {
                            etlKuaishouVideoInfo.setChannelName("素造");
                        }
                        //查询相关设计人员信息
                        String clipId = null;
                        JSONObject ascription = ascriptionService.getDetailByCode(signature);
                        if (null != ascription) {
                            clipId = ascription.getString("clipId");
                            if (null != clipId && !"".equals(clipId.trim())) {
                                etlKuaishouVideoInfo.setClipId(clipId);
                                //根据id查询剪辑所属的设计负责人
                                SysUser user = sysUserService.getById(clipId);
                                if (null != user) {
                                    String roleCode = sysUserService.getRoleCodeByUserId(user.getId());
                                    if (null != roleCode && "designTeamLeader".equals(roleCode.trim())) {
                                        etlKuaishouVideoInfo.setDesignTeamLeaderId(user.getId());
                                        etlKuaishouVideoInfo.setDesignTeamLeaderName(user.getRealname());
                                    } else {
                                        etlKuaishouVideoInfo.setDesignTeamLeaderId(user.getLeaderId());
                                        etlKuaishouVideoInfo.setDesignTeamLeaderName(user.getLeaderName());
                                    }
                                }
                            }
                            String clipName = ascription.getString("clipName");
                            if (null != clipName && !"".equals(clipName.trim())) {
                                etlKuaishouVideoInfo.setClipName(clipName);
                            }
                            String planId = ascription.getString("planId");
                            if (null != planId && !"".equals(planId.trim())) {
                                etlKuaishouVideoInfo.setPlanId(planId);
                            }
                            String planName = ascription.getString("planName");
                            if (null != planName && !"".equals(planName.trim())) {
                                etlKuaishouVideoInfo.setPlanName(planName);
                            }
                            String shotId = ascription.getString("shotId");
                            if (null != shotId && !"".equals(shotId.trim())) {
                                etlKuaishouVideoInfo.setShotId(shotId);
                            }
                            String shotName = ascription.getString("shotName");
                            if (null != shotName && !"".equals(shotName.trim())) {
                                etlKuaishouVideoInfo.setShotName(shotName);
                            }
                        }
                        if (Check.isNull(clipId)) {
                            Long accountId = videoJson.getLong("accountId");
                            if (!Check.isNull(accountId)) {
                                JSONObject companyJson = companyService.getCompanyInfoByAccountId(accountId);
                                if (!Check.isNull(companyJson)) {
                                    etlKuaishouVideoInfo.setCompanyId(companyJson.getString("companyId"));
                                    etlKuaishouVideoInfo.setCompanyName(companyJson.getString("companyName"));
                                }
                            }
                        } else {
                            JSONObject companyJson = companyService.getCompanyInfoByUserId(clipId);
                            etlKuaishouVideoInfo.setCompanyId(companyJson.getString("companyId"));
                            etlKuaishouVideoInfo.setCompanyName(companyJson.getString("companyName"));
                        }
                        //查询视频初次消耗时间
                        String minDate = etlKuaishouVideoInfoMapper.getVideoMinDate(signature);
                        if (null != minDate && !"".equals(minDate)) {
                            Date getDate = DateUtils.parseDate(minDate, "yyyy-MM-dd");
                            etlKuaishouVideoInfo.setStateDate(getDate);
                        }
                        addList.add(etlKuaishouVideoInfo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (!Check.isNull(addList)) {
                    etlKuaishouVideoInfoMapper.addBatch(addList);

                }
            });
        }
        log.info("{}数据获取完成", date);
    }


    @Override
    public EtlKuaishouVideoInfo getOneByVideoCode(String signature) {
        QueryWrapper<EtlKuaishouVideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_code", signature);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

}
