package hf.lskj.com.carstockcount.db;

import android.content.Context;

import org.greenrobot.greendao.DaoException;

import java.util.Calendar;
import java.util.List;

import hf.lskj.com.carstockcount.MyApplication;
import hf.lskj.com.carstockcount.utils.UHF.Sound;

/**
 * Created by yaoju on 2018/5/3.
 * 自定义的数据库操作方法
 */

public class MyDao {

    private static RecordDao mRecordDao = MyApplication.getInstances().getDaoSession().getRecordDao();
    private static CarInfoDao mCarInfoDao = MyApplication.getInstances().getDaoSession().getCarInfoDao();

    private static String[] getTime() {
        String startTime, endTime;
        Calendar midday = Calendar.getInstance();
        midday.set(Calendar.HOUR_OF_DAY, 12);
        midday.set(Calendar.MINUTE, 0);
        Calendar current = Calendar.getInstance();
        if (current.getTimeInMillis() > midday.getTimeInMillis()) {
            //下午 12-24点
            startTime = midday.getTimeInMillis() + "";
            midday.set(Calendar.HOUR_OF_DAY, 24);
            endTime = midday.getTimeInMillis() + "";
        } else {
            //上午 6-12点
            endTime = midday.getTimeInMillis() + "";
            midday.set(Calendar.HOUR_OF_DAY, 6);
            startTime = midday.getTimeInMillis() + "";
        }
        return new String[]{startTime, endTime};
    }


    public static boolean saveToDb(Context context, CarInfo carInfo, Record entity) {
        Sound mSound = new Sound(context);
        //根据当前时间判断是否已盘点
        String[] timePoints = getTime();
        Record dbEntity = mRecordDao.queryBuilder()
                .where(RecordDao.Properties.Rfid.eq(entity.getRfid()),
                        RecordDao.Properties.CreateTime.between(timePoints[0], timePoints[1]),
                        RecordDao.Properties.Type.eq(Record.TYPE_PANDIAN)).unique();
        if (dbEntity != null) {
            //如已盘点则update
            entity.setId(dbEntity.getId());
        }
        MyApplication.getInstances().getDb().beginTransaction();
        boolean success = false;
        if (mRecordDao.insertOrReplace(entity) > 0) {
            carInfo.setStatus(entity.getStatus());
            carInfo.setStockCountType(Record.TYPE_PANDIAN);
            carInfo.setStockCountTime(entity.getCreateTime());
            if (mCarInfoDao.insertOrReplace(carInfo) > 0) {
                mSound.callAlarm(true, 0);
                success = true;
                MyApplication.getInstances().getDb().setTransactionSuccessful();
            }
        }
        MyApplication.getInstances().getDb().endTransaction();
        return success;
    }

    //适用于导出盘点记录
    public static boolean saveToDb(List<CarInfo> cars, List<Record> records) {
        boolean success = true;
        String[] timePoints = getTime();
        MyApplication.getInstances().getDb().beginTransaction();
        for (CarInfo car : cars) {
            if (!Record.TYPE_PANDIAN.equals(car.getStockCountType())) {
                car.setStatus(CarInfo.STATUS_WEIPANDAO);
                car.setOperatorId(MyApplication.currentUser.getLoginId());
                car.setStockCountTime(System.currentTimeMillis() + "");
                Record dbEntity;
                try {
                    dbEntity = mRecordDao.queryBuilder()
                            .where(RecordDao.Properties.Vin.eq(car.getVin()),
                                    RecordDao.Properties.CreateTime.between(timePoints[0], timePoints[1]),
                                    RecordDao.Properties.Type.eq(Record.TYPE_PANDIAN)).unique();
                } catch (DaoException e) {
                    e.printStackTrace();
                    success = false;
                    break;
                }
                if (dbEntity == null) {
                    //新增“未盘到”的盘点记录
                    Record record = new Record();
                    record.setRfid(car.getRfid());
                    record.setRemark("");
                    record.setVin(car.getVin());
                    record.setStatus(car.getStatus());
                    record.setCreateTime(car.getStockCountTime());
                    record.setOperatorId(MyApplication.currentUser.getLoginId());
                    record.setStoreId(car.getStoreId());
                    record.setColor(car.getColor());
                    record.setCarType(car.getType());
                    record.setType(Record.TYPE_PANDIAN);
                    records.add(record);
                }
            }
        }
        if (success) {
            try {
                mRecordDao.insertOrReplaceInTx(records);
                mCarInfoDao.insertOrReplaceInTx(cars);
                MyApplication.getInstances().getDb().setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
                success = false;
            }
        }
        MyApplication.getInstances().getDb().endTransaction();
        return success;
    }
}
