package com.example.administrator.zhapp.service;

import android.app.IntentService;
import android.content.Intent;

import com.example.administrator.zhapp.DBModel.AjhScInfo;
import com.example.administrator.zhapp.DBModel.Ajhjh;
import com.example.administrator.zhapp.DBModel.Ajhxcjs;
import com.example.administrator.zhapp.DBModel.Ajhxzrwqy;
import com.example.administrator.zhapp.DBModel.Djjh;
import com.example.administrator.zhapp.DBModel.DjjhRwQy;
import com.example.administrator.zhapp.DBModel.LyXcjsInfo;
import com.example.administrator.zhapp.DBModel.LyYhpcInfo;
import com.example.administrator.zhapp.DBModel.LyxcXm;
import com.example.administrator.zhapp.DBModel.LyxcXmJg;
import com.example.administrator.zhapp.DBModel.Lyxcrwqy;
import com.example.administrator.zhapp.DBModel.QxgdInfo;
import com.example.administrator.zhapp.DBModel.XcjsInfo;
import com.example.administrator.zhapp.DBModel.XfBaxcRwqy;
import com.example.administrator.zhapp.DBModel.XfDjjh;
import com.example.administrator.zhapp.DBModel.XfDjjhRwqy;
import com.example.administrator.zhapp.DBModel.XfXcjsInfo;
import com.example.administrator.zhapp.DBModel.XfXcmhqc;
import com.example.administrator.zhapp.DBModel.XfXcxm;
import com.example.administrator.zhapp.DBModel.XfXcxmjg;
import com.example.administrator.zhapp.DBModel.XwaqgcJh;
import com.example.administrator.zhapp.DBModel.XwaqgcJs;
import com.example.administrator.zhapp.DBModel.XwaqgcSc;
import com.example.administrator.zhapp.DBModel.YhpcInfo;
import com.example.administrator.zhapp.utils.UiUtlis;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 删除计划到期的服务
 */
public class DeleteOverdueJHService extends IntentService {


    private List<String> djjhs;
    private List<String> xfDjjhs;
    private List<String> ajhjhs;
    private List<String> xwaqgcJhs;

    public DeleteOverdueJHService() {
        super("DeleteOverdueJHService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        djjhs = new ArrayList<>();
        xfDjjhs = new ArrayList<>();
        ajhjhs = new ArrayList<>();
        xwaqgcJhs = new ArrayList<>();
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        List<Djjh> djjh = DataSupport.findAll(Djjh.class);
        for (Djjh jh : djjh) {
            if (UiUtlis.isdelete(jh.getDQSJ())) {
                djjhs.add(jh.getJHID());
            }
        }
        List<Ajhjh> ajhjh = DataSupport.findAll(Ajhjh.class);
        for (Ajhjh jh : ajhjh) {
            if (UiUtlis.isdelete(jh.getDQSJ())) {
                ajhjhs.add(jh.getJHID());
            }
        }
        List<XfDjjh> xfDjjh = DataSupport.findAll(XfDjjh.class);
        for (XfDjjh jh : xfDjjh) {
            if (UiUtlis.isdelete(jh.getNexttime())) {
                xfDjjhs.add(jh.getJhid());
            }
        }

        List<XwaqgcJh> xwaqgcJh = DataSupport.findAll(XwaqgcJh.class);

        for (XwaqgcJh jh : xwaqgcJh) {
            if (UiUtlis.isdelete(jh.getDQSJ())) {
                xwaqgcJhs.add(jh.getJHID());
            }
        }
    }


    //计划过期,删除数据
    private void removeData() {

        //删除点检数据
        for (String jhid : djjhs) {
            DataSupport.deleteAll(Djjh.class, "jhid = ?", jhid);
            DataSupport.deleteAll(DjjhRwQy.class, "jhid = ?", jhid);
            List<XcjsInfo> infos = DataSupport.where("jhid = ?", jhid).find(XcjsInfo.class);
            for (XcjsInfo info : infos) {
                File file = new File(info.getFilename());
                if (file.isFile()) {
                    file.delete();
                }
            }
            DataSupport.deleteAll(XcjsInfo.class, "jhid = ?", jhid);
            DataSupport.deleteAll(QxgdInfo.class);
        }

        //删除安健环数据
        for (String jhid : ajhjhs) {
            List<Ajhxcjs> ajhxcjses = DataSupport.findAll(Ajhxcjs.class);
            for (Ajhxcjs js : ajhxcjses) {
                File file = new File(js.getFile());
                if (file.isFile()) {
                    file.delete();
                }
            }
            DataSupport.deleteAll(Ajhxcjs.class, "jhid = ?", jhid);
            DataSupport.deleteAll(Ajhjh.class, "jhid = ?", jhid);
            DataSupport.deleteAll(Ajhxzrwqy.class, "jhid = ?", jhid);
            DataSupport.deleteAll(AjhScInfo.class, "jhid = ?", jhid);
            DataSupport.deleteAll(YhpcInfo.class);
        }

        //删除消防数据
        for (String jhid : xfDjjhs) {

            List<XfXcjsInfo> infos = DataSupport.where("jhid = ?", jhid).find(XfXcjsInfo.class);
            for (XfXcjsInfo info : infos) {
                File file = new File(info.getPath());
                if (file.isFile()) {
                    file.delete();
                }
            }

            List<LyXcjsInfo> lyjs = DataSupport.where("jhid = ?", jhid).find(LyXcjsInfo.class);
            for (LyXcjsInfo info : lyjs) {
                File file = new File(info.getPath());
                if (file.isFile()) {
                    file.delete();
                }
            }
            DataSupport.deleteAll(XfDjjh.class, "jhid = ?", jhid);
            DataSupport.deleteAll(XfDjjhRwqy.class, "jhid = ?", jhid);
            DataSupport.deleteAll(XfXcjsInfo.class, "jhid = ?", jhid);
            DataSupport.deleteAll(XfXcxmjg.class, "jhid = ?", jhid);
            DataSupport.deleteAll(XfXcmhqc.class, "jhid = ?", jhid);
            DataSupport.deleteAll(XfBaxcRwqy.class, "jhid = ?", jhid);
            DataSupport.deleteAll(XfXcxm.class, "jhid = ?", jhid);
            DataSupport.deleteAll(Lyxcrwqy.class, "jhid = ?", jhid);
            DataSupport.deleteAll(LyxcXm.class, "jhid = ?", jhid);
            DataSupport.deleteAll(LyXcjsInfo.class, "jhid = ?", jhid);
            DataSupport.deleteAll(LyxcXmJg.class, "jhid = ?", jhid);
            DataSupport.deleteAll(LyYhpcInfo.class);
        }

        for (String jh : xwaqgcJhs) {
            DataSupport.deleteAll(XwaqgcJh.class, "jhid = ?", jh);
            DataSupport.deleteAll(XwaqgcSc.class, "jhid = ?", jh);
            DataSupport.deleteAll(XwaqgcJs.class, "jhid = ?", jh);
        }
    }
}
