package com.tofly.ghrq_route.module.job;

import com.tofly.data.sql.dao.DaoSession;
import com.tofly.data.sql.dao.JobBeanDao;
import com.tofly.data.sql.dao.TroublePictureBeanDao;
import com.tofly.ghrq_route.module.job.domain.JobBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;

import java.util.List;

public class JobDaoHelper {

    private static JobDaoHelper ourInstance = new JobDaoHelper();

    public static JobDaoHelper getInstance() {
        return ourInstance;
    }

    private DBDaoHelper dbDaoHelper;
    private DaoSession daoSession;

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

    /*插入工单信息*/
    public void insertJobBean(JobBean jobBean) {
        daoSession.insert(jobBean);
    }

    /*插入或者更新工单信息*/
    public void insertOrUpdateJobBean(JobBean jobBean) {
        daoSession.insertOrReplace(jobBean);
    }

    public JobBean getJobBean(String job_id) {
        JobBeanDao jobBeanDao = daoSession.getJobBeanDao();
        return jobBeanDao.load(job_id);
    }

    public List<JobBean> getJobList(String userid) {
        JobBeanDao jobBeanDao = daoSession.getJobBeanDao();
        return jobBeanDao.queryBuilder().where(
                JobBeanDao.Properties.Userid.eq(userid))
                .orderDesc(JobBeanDao.Properties.DispatchDate)
                .orderAsc(JobBeanDao.Properties.TaskStatus)
                .list();
    }

    public List<JobBean> getJobList(String userid, int job_state) {
        JobBeanDao jobBeanDao = daoSession.getJobBeanDao();
        return jobBeanDao.queryBuilder().where(
                JobBeanDao.Properties.Userid.eq(userid),
                JobBeanDao.Properties.TaskStatus.eq(job_state))
                .list();
    }

    public void deleteJobBeans(String userid, int job_state) {
        JobBeanDao jobBeanDao = daoSession.getJobBeanDao();
        List<JobBean> jobList = getJobList(userid, job_state);
        if (jobList != null && jobList.size() > 0) {
            jobBeanDao.deleteInTx(jobList);
        }
    }

    public void deleteJobBean(JobBean jobBean) {
        daoSession.delete(jobBean);
    }

    /**
     * 插入一个新的隐患图片或者更新已存在的隐患图片
     * @param pictureBean 新隐患图片或者需要更新的隐患图片
     * @return 数据表中的rowid
     */
    public long insertOrUpdateJobPicture(TroublePictureBean pictureBean) {
        return daoSession.insertOrReplace(pictureBean);
    }

    public List<TroublePictureBean> getSavedJobPicture(String job_id) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        return troublePictureBeanDao.queryBuilder().where(
                TroublePictureBeanDao.Properties.Troubleid.eq(job_id))
                .list();
    }

    public void deleteSavedJobPicture(TroublePictureBean pictureBean) {
        dbDaoHelper.deleteSavedTroublePicture(pictureBean);
    }

    public void deleteSavedJobPictures(List<TroublePictureBean> pictureBeans) {
        TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
        troublePictureBeanDao.deleteInTx(pictureBeans);
    }

    public void deleteSavedJobPictures(String job_id) {
        List<TroublePictureBean> pictureBeans = getSavedJobPicture(job_id);
        if (pictureBeans != null && pictureBeans.size() > 0) {
            TroublePictureBeanDao troublePictureBeanDao = daoSession.getTroublePictureBeanDao();
            troublePictureBeanDao.deleteInTx(pictureBeans);
        }
    }
}
