package org.jeecg.modules.orderplatform.util;

import cn.com.ctop.common.module.entity.MaterialAscription;
import cn.com.ctop.common.module.entity.MaterialCutFrame;
import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.common.module.entity.MaterialParameter;
import cn.com.ctop.common.module.material.mapper.MaterialVideoInfoMapper;
import cn.com.ctop.common.module.service.IMaterialAscriptionService;
import cn.com.ctop.common.module.service.IMaterialCutFrameService;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.service.IMaterialParameterService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class NewAndOldInteract {

    @Autowired
    private MaterialVideoInfoMapper materialVideoInfoMapper;

    @Autowired
    private IMaterialInfoService materialInfoService;

    @Autowired
    private IMaterialAscriptionService materialAscriptionService;

    @Autowired
    private IMaterialCutFrameService materialCutFrameService;

    @Autowired
    private IMaterialParameterService materialParameterService;

    /**
     *  根据视频Md5从新版素材库中查询老版需要的数据做insert
     */
    public boolean NEW_2_OLD(String md5){

        try{
            JSONObject materialInfoNew = materialVideoInfoMapper.joinMaterialInfo(md5);

            if(null!=materialInfoNew&&!materialInfoNew.isEmpty()){
                MaterialInfo materialInfoOld = JSONObject.toJavaObject(materialInfoNew,MaterialInfo.class);
                materialInfoService.save(materialInfoOld);
            }
            JSONObject materialAscriptionNew = materialVideoInfoMapper.joinMaterialAscription(md5);

            if(null!=materialAscriptionNew&&!materialAscriptionNew.isEmpty()){
                MaterialAscription materialAscriptionOld = JSONObject.toJavaObject(materialAscriptionNew,MaterialAscription.class);
                materialAscriptionService.save(materialAscriptionOld);
            }

            JSONObject materialParameterNew = materialVideoInfoMapper.joinMaterialParameter(md5);

            if(null!=materialParameterNew&&!materialParameterNew.isEmpty()){
                MaterialParameter materialParameterOld = JSONObject.toJavaObject(materialParameterNew,MaterialParameter.class);
                materialParameterService.save(materialParameterOld);
            }

            List<JSONObject> materialCutFrameNew = materialVideoInfoMapper.joinMaterialCutFrame(md5);

            if(null!=materialCutFrameNew&&!materialCutFrameNew.isEmpty()){
                materialCutFrameNew.forEach(it->{
                    MaterialCutFrame materialCutFrameOld = JSONObject.toJavaObject(it,MaterialCutFrame.class);
                    materialCutFrameService.save(materialCutFrameOld);
                });
            }
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
        return true;
    }
}