'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2018/5/24
 * @version
 */

import mongoose from 'mongoose';
import schedule from 'node-schedule';
import async from 'async';
const projectModel = mongoose.model('projects');
const projSettingModel = mongoose.model('proj_setting');
const calcProgramModel = mongoose.model('calc_programs');
const labourCoeModel = mongoose.model('labour_coes');
const billsModel = mongoose.model('bills');
const rationModel = mongoose.model('ration');
const projGljModel = mongoose.model('glj_list');
const rationGljModel = mongoose.model('ration_glj');
const rationCoeMolde = mongoose.model('ration_coe');
const installationModel = mongoose.model('installation_fee');
const rationInstallationModel = mongoose.model('ration_installation');
const quantityDetailModel = mongoose.model('quantity_detail');
const unitPriceFileModel = mongoose.model('unit_price_file');
const unitPriceModel = mongoose.model('unit_price');
const mixRatioModel = mongoose.model('mix_ratio');
const feeRateFileModel = mongoose.model('fee_rate_file');
const feeRateModel = mongoose.model('fee_rates');

//删除垃圾数据
async function clearJunkData(callback){
    let functions = [];
    //获取彻底删除了的项目
    let junkProjs = await projectModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
    let junkProjIds = [];
    for(let jProj of junkProjs){
        junkProjIds.push(jProj.ID);
    }
    if(junkProjIds.length > 0){
        //清除proj_setting
        functions.push(function(cb){
            projSettingModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除calcProgram
        functions.push(function(cb){
            calcProgramModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除labourCoe
        functions.push(function(cb){
            labourCoeModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除bills
        functions.push(function(cb){
            billsModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除ration
        functions.push(function(cb){
            rationModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除project_glj
        functions.push(function(cb){
            projGljModel.remove({project_id: {$in: junkProjIds}}, cb);
        });
        //清除ration_glj
        functions.push(function(cb){
            rationGljModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除ration_coe
        functions.push(function(cb){
            rationCoeMolde.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除installation_fee
        functions.push(function(cb){
            installationModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除ration_installation
        functions.push(function(cb){
            rationInstallationModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
        //清除quantity_detail
        functions.push(function(cb){
            quantityDetailModel.remove({projectID: {$in: junkProjIds}}, cb);
        });
    }
    //彻底删除了的单价文件
    let junkUFs = await unitPriceFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
    let junkUFIds = [];
    for(let jUF of junkUFs){
        junkUFIds.push(jUF.id);
    }
    if(junkUFIds.length > 0){
        functions.push(function(cb){
            unitPriceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
        });
        functions.push(function(cb){
            mixRatioModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
        });
    }
    //彻底删除了的费率文件
    let junkFFs = await feeRateFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
    let junkFFIds = [];
    for(let jFF of junkFFs){
        junkFFIds.push(jFF.feeRateID);
    }
    if(junkFFIds.length > 0){
        functions.push(function(cb){
            feeRateModel.remove({ID: {$in: junkFFIds}}, cb);
        });
    }
    //清除
    if(functions.length > 0){
        async.parallel(functions, async function(err, result){
            if(!err){
                //清除项目
                await projectModel.remove({ID: {$in: junkProjIds}});
                //清除单价文件
                await unitPriceFileModel.remove({id: {$in: junkUFIds}});
                //清除费率文件
                await feeRateFileModel.remove({feeRateID: {$in: junkFFIds}});
            }
            if(callback){
                callback(err);
            }
        });
    }
    else {
        callback(0);
    }

}

const sysSchedule = {
    clearJunkData: clearJunkData
};

//export {sysSchedule as default}
module.exports = sysSchedule;
