package com.tofly.ghrq_route.module.sketch;

import com.tofly.data.sql.dao.DaoSession;
import com.tofly.data.sql.dao.SketchBeanDao;
import com.tofly.ghrq_route.module.sketch.domain.SketchBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import java.util.List;


public class SketchDaoHelper {

    private static SketchDaoHelper ourInstance = new SketchDaoHelper();

    public static SketchDaoHelper getInstance() {
        return ourInstance;
    }

    private DBDaoHelper dbDaoHelper;
    private DaoSession daoSession;

    private SketchDaoHelper() {
        dbDaoHelper = DBDaoHelper.getInstance();
        daoSession = dbDaoHelper.getDaoSession();
    }

    /*插入草图信息*/
    public void insertSketchBean(SketchBean sketchBean) {
        daoSession.insert(sketchBean);
    }

    /*插入或者更新草图信息*/
    public void insertOrUpdateSketchBean(SketchBean sketchBean) {
        daoSession.insertOrReplace(sketchBean);
    }

    public SketchBean getSketchBean(String sketch_id) {
        SketchBeanDao sketchBeanDao = daoSession.getSketchBeanDao();
        return sketchBeanDao.load(sketch_id);
    }

    public List<SketchBean> getSketchList(String userid) {
        SketchBeanDao sketchBeanDao = daoSession.getSketchBeanDao();
        return sketchBeanDao.queryBuilder().where(SketchBeanDao.Properties.Userid.eq(userid)).list();
    }

    public List<SketchBean> getSketchList(String userid, int sketch_state) {
        SketchBeanDao sketchBeanDao = daoSession.getSketchBeanDao();
        return sketchBeanDao.queryBuilder().where(
                SketchBeanDao.Properties.Userid.eq(userid),
                SketchBeanDao.Properties.Status.eq(sketch_state))
                .list();
    }

    public void deleteSketchBean(String sketch_id) {
        SketchBean sketchBean = getSketchBean(sketch_id);
        if (sketchBean != null) {
            daoSession.delete(sketchBean);
        }
    }

    public void deleteSketchBeans(String userid, int sketch_state) {
        SketchBeanDao sketchBeanDao = daoSession.getSketchBeanDao();
        List<SketchBean> jobList = getSketchList(userid, sketch_state);
        if (jobList != null && jobList.size() > 0) {
            sketchBeanDao.deleteInTx(jobList);
        }
    }
}
