package com.cnd3b.service;

import com.cnd3b.common.BaseClass;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DBConnect;
import com.cnd3b.common.data.db.SQLiteJDBC;
import com.cnd3b.utility.obs.BucketFile;
import com.cnd3b.utility.obs.mpc.MpcFactory;
import com.cnd3b.utility.obs.mpc.MpcFile;

import java.util.ArrayList;

/**
 * 华为云存储转码任务查询
 */
public class ObsMpcTaskQuery extends BaseClass implements Runnable {

    @Override
    public void run() {
        try {
            if (new SQLiteJDBC().checkServicesUsed("ObsMpcTaskQuery", true)) {
                mpcTask();
                createMinImage();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void mpcTask() {
        DBConnect dbConnect = new DBConnect();
        SQLFactory sqlFactory = new SQLFactory(this, "转码任务查询");
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        ArrayList<String> list = new ArrayList<>();
        if (!rows.isEmpty()) {
            System.err.println("转码任务查询：共" + rows.size() + "条");
        }
        MpcFactory mpcFactory = new MpcFactory();
        int rownum = 1;
        for (Row row : rows) {
            String siteid = row.getString("siteid");
            String tattachmentid = row.getString("tattachmentid");
            String mpctaskid = row.getString("mpctaskid");
            System.err.println("转码任务查询：开始执行第" + (rownum++) + "条,siteid:" + siteid + "tattachmentid:" + tattachmentid + "mpctaskid:" + mpctaskid);
            MpcFile file = mpcFactory.queryMpcTask(mpctaskid);
            if (file != null) {
                //转码成功的视频文件名称
                String objectname = file.getObject();
                System.err.println("转码任务查询：转码后名称" + objectname);
                String obsfilename = objectname + "/" + file.getOutput_file_name();

                BucketFile bucketFile = new BucketFile(siteid);
                if (!bucketFile.doesObjectExist(obsfilename)) {
                    System.err.println("转码任务查询：没有找到实体文件" + obsfilename);
                    continue;
                }
                String fileurl = bucketFile.getFileUrl(obsfilename);
                System.err.println("转码任务查询：获取转码文件访问地址" + fileurl);
                String sql = "update tattachment set serialnumber_hls='" + objectname + "', fobsurl_hls='" + fileurl + "' where siteid='" + siteid + "' and tattachmentid='" + tattachmentid + "'";
                list.add(sql);
                bucketFile.close();
            } else {
                System.err.println("转码任务查询：转码未完成");
            }
        }
        if (!list.isEmpty()) {
            System.err.println("更新附件表转码后访问路径");
            String status = dbConnect.runSqlUpdate(list);
            if (!status.equals("true")) {
                System.err.println("转码任务查询:" + status);
            }
        }
    }

    /**
     * 缩略图生成
     */
    public void createMinImage() {
        DBConnect dbConnect = new DBConnect();
        SQLFactory sqlFactory = new SQLFactory(this, "生成缩略图附件查询");
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            String siteid = row.getString("siteid");
            String serialnumber = row.getString("serialnumber");
            String tattachmentid = row.getString("tattachmentid");
            BucketFile bucketFile = new BucketFile(siteid);
            String obsurl_imageshort = bucketFile.getMinImageUrl(serialnumber);
            String obsurl_imagecompress = bucketFile.getCompressImageUrl(serialnumber);
            dbConnect.runSqlUpdate("update tattachment set fobsurl_minimage='" + obsurl_imageshort + "',fobsurl_compressimage='" + obsurl_imagecompress + "' where siteid='" + siteid + "' and tattachmentid='" + tattachmentid + "'");
        }
    }
}
