package p2.application.tobsmag;

import baseclass.PaoCust;
import baseclass.tools.P2Math;
import com.cnd3b.utility.obs.Bucket;
import p2.pao.PaoSet;
import p2.util.P2AppException;
import p2.util.P2Exception;

public class tobsmag extends PaoCust {
    public tobsmag(PaoSet arg0) {
        super(arg0);
    }

    /**
     * 创建桶验证
     *
     * @throws P2Exception
     */
    public void createobs_validate() throws P2Exception {
        Bucket bucket = new Bucket(getString("siteid"));
        if (bucket.isAllReadyUsed()) {
            throw new P2AppException("", bucket.getBucketName() + "已存在，不可重复创建！");
        }
    }

    /**
     * 创建桶
     *
     * @throws P2AppException
     */
    public void createobs() throws P2Exception {
        Bucket bucket = new Bucket(getString("siteid"));
        if (bucket.create()) {
            setValue("fbucketname", bucket.getBucketName(), 11L);
            setValue("fstatus", "正常", 11L);
            ((PaoSet) getThisPaoSet()).addWarning(new P2AppException("", "创建成功！"));
        } else {
            ((PaoSet) getThisPaoSet()).addWarning(new P2AppException("", "创建失败！"));
        }
    }


    /**
     * 查询桶剩余容量验证
     *
     * @throws P2Exception
     */
    public void getVolume_validate() throws P2Exception {
        Bucket bucket = new Bucket(getString("siteid"));
        if (!bucket.isAllReadyUsed()) {
            throw new P2AppException("", "桶不存在，请先创建桶");
        }
    }

    /**
     * 查询桶剩余容量
     *
     * @throws P2Exception
     */
    public void getVolume() throws P2Exception {
        Bucket bucket = new Bucket(getString("siteid"));
        if (bucket.isAllReadyUsed()) {
            long contentlength = bucket.getVolume();
            if ((contentlength / 1024d) < 1024d) {
                setValue("fusevolume", P2Math.div(contentlength, 1024d, 2) + "KB", 11L);
            } else if ((contentlength / (1024d * 1024d)) < 1024d) {
                setValue("fusevolume", P2Math.div(contentlength, (1024d * 1024d), 2) + "MB", 11L);
            } else {
                setValue("fusevolume", P2Math.div(contentlength, (1024d * 1024d * 1024d), 2) + "GB", 11L);
            }
            setValue("contentlength",contentlength, 11L);
            setValue("filecount", bucket.getFileCount(), 11L);
            setValue("flastdate", getDateTime(), 11L);
        }
    }
}
