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

import cn.com.ctop.common.module.entity.MaterialAscription;
import cn.com.ctop.common.module.entity.SysUser;
import cn.com.ctop.common.module.entity.UserCompany;
import cn.com.ctop.common.module.service.IMaterialAscriptionService;
import cn.com.ctop.common.module.service.ISysUserService;
import cn.com.ctop.common.module.service.IUserCompanyService;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
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 org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.util.Date;
import java.util.List;

/**
 * 快手视频报表信息
 * @author jeecg-boot
 * @date   2021-02-20
 * @version V1.0
 */
@Service
public class EtlKuaishouVideoInfoServiceImpl extends ServiceImpl<EtlKuaishouVideoInfoMapper, EtlKuaishouVideoInfo> implements IEtlKuaishouVideoInfoService {
    @Resource
    private EtlKuaishouVideoInfoMapper etlKuaishouVideoInfoMapper;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;
    @Autowired
    private IMaterialAscriptionService ascriptionService;
    @Autowired
    private IUserCompanyService userCompanyService;
    @Autowired
    private ISysUserService sysUserService;
    @Override
    public void etlKuaishouVideoInfo(String date) {
        //1:查询当前日期内的所有存在md5码信息的视频数据
        List<KuaiShouVideoGet> videoGets = videoGetService.getVideoInfoGroupBySignature(date);
        if(null!=videoGets&&!videoGets.isEmpty()){
            videoGets.forEach(videoGet -> {
                EtlKuaishouVideoInfo etlKuaishouVideoInfo = new EtlKuaishouVideoInfo();
                etlKuaishouVideoInfo.setVideoCode(videoGet.getSignature());
                etlKuaishouVideoInfo.setUrl(videoGet.getUrl());
                etlKuaishouVideoInfo.setCoverUrl(videoGet.getCoverUrl());
                etlKuaishouVideoInfo.setChannelType(videoGet.getChannelType().toString());
                if(videoGet.getChannelType() == 0){
                    etlKuaishouVideoInfo.setChannelName("自产");
                }else {
                    etlKuaishouVideoInfo.setChannelName("素造");
                }
                //查询相关设计人员信息
                JSONObject ascription = ascriptionService.getDetailByCode(videoGet.getSignature());
                if(null!=ascription){
                    String clipId = ascription.getString("clipId");
                    if(null!=clipId&&!clipId.trim().equals("")){
                        etlKuaishouVideoInfo.setClipId(clipId);
                        UserCompany userCompany = userCompanyService.getByUserId(clipId);
                        if(null!=userCompany){
                            etlKuaishouVideoInfo.setCompanyName(userCompany.getCompanyName());
                        }
                        //根据id查询剪辑所属的设计负责人
                        SysUser user = sysUserService.getById(clipId);
                        SysUser designTeamLeader = sysUserService.getDesignerTeamLeaderBy(user.getOrgCode());
                        if(null!=designTeamLeader){
                            etlKuaishouVideoInfo.setDesignTeamLeaderId(designTeamLeader.getId());
                            etlKuaishouVideoInfo.setDesignTeamLeaderName(designTeamLeader.getRealname());
                        }
                    }
                    String clipName = ascription.getString("clipName");
                    if(null!=clipName&&!clipName.trim().equals("")){
                        etlKuaishouVideoInfo.setClipName(clipName);
                    }
                    String planId = ascription.getString("planId");
                    if(null!=planId&&!planId.trim().equals("")){
                        etlKuaishouVideoInfo.setPlanId(planId);
                    }
                    String planName = ascription.getString("planName");
                    if(null!=planName&&!planName.trim().equals("")){
                        etlKuaishouVideoInfo.setPlanName(planName);
                    }
                    String shotId = ascription.getString("shotId");
                    if(null!=shotId&&!shotId.trim().equals("")){
                        etlKuaishouVideoInfo.setShotId(shotId);
                    }
                    String shotName = ascription.getString("shotName");
                    if(null!=shotName&&!shotName.trim().equals("")){
                        etlKuaishouVideoInfo.setShotName(shotName);
                    }
                }
                //查询视频初次消耗时间
                String minDate = etlKuaishouVideoInfoMapper.getVideoMinDate(videoGet.getSignature());
                if(null!=minDate&&!"".equals(minDate)){
                    Date getDate = DateUtils.parseDate(minDate,"yyyy-MM-dd");
                    etlKuaishouVideoInfo.setStateDate(getDate);
                }
                etlKuaishouVideoInfoMapper.replaceVideoInfo(etlKuaishouVideoInfo);
            });
        }
    }
    @Override
    public EtlKuaishouVideoInfo getOneByVideoCode(String signature) {
        QueryWrapper<EtlKuaishouVideoInfo>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_code",signature);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
