'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2018/10/25
 * @version
 */

module.exports = {
    getInstallation,
    updateFeeItem,
    updateSection,
    batchUpdateInst,
};

const mongoose = require("mongoose");
const counter = require('../../../public/counter/counter');
const async = require('async');
const deleteQuery = [{deleteInfo: null}, {'deleteInfo.deleted': false}];
const complefeeItemModel = mongoose.model('complementary_ration_installation');
const compleSectionModel = mongoose.model('complementary_ration_installationSection');
const compleRationModel = mongoose.model('complementary_ration_items');

async function getInstallation(userId, compilationId){
        let feeItems = await complefeeItemModel.find({userId: userId, compilationId: compilationId, $or: deleteQuery});
        for (let feeItem of feeItems) {
            let sids = [];
            for (let sec of feeItem.section) {
                sids.push(sec.ID);
            }
            if (sids.length > 0) {
                let sections = await compleSectionModel.find({ID: {$in: sids}, $or: deleteQuery});
                feeItem._doc.section = sections;
            }
        }
        return feeItems;
}

//更新费用项
async function updateFeeItem(userId, compilationId, updateData){
    for (let data of updateData) {
        if (data.updateType === 'new') {
            data.updateData.userId = userId;
            data.updateData.compilationId = compilationId;
            await complefeeItemModel.create(data.updateData);
        } else if (data.updateType === 'update' && !data.updateData.deleted) {
            await complefeeItemModel.update({ID: data.updateData.ID}, data.updateData);
        } else {
            let deleteInfo = {
                deleted: true,
                deleteDateTime: new Date(),
                deleteBy: userId
            };
            data.updateData.deleteInfo = deleteInfo;
            delete data.updateData.deleted;
            await complefeeItemModel.remove({ID: data.updateData.ID});
        }
    }
}

//更新分册章节
async function updateSection(userId, compilationId, updateData){
    for (let data of updateData) {
        if (data.updateType === 'new') {
            data.updateData.userId = userId;
            data.updateData.compilationId = compilationId;
            await compleSectionModel.create(data.updateData);
        } else if (data.updateType === 'update' && !data.updateData.deleted) {
            await compleSectionModel.update({ID: data.updateData.ID}, data.updateData);
        } else {
            let deleteInfo = {
                deleted: true,
                deleteDateTime: new Date(),
                deleteBy: userId
            };
            data.updateData.deleteInfo = deleteInfo;
            delete data.updateData.deleted;
            await compleSectionModel.remove({ID: data.updateData.ID});
        }
    }
}

//批量关联至定额（选择某一章节）
async function batchUpdateInst(rationSection, inst){
    for (let sectionId of rationSection) {
        await compleRationModel.update({sectionId: sectionId, $or: deleteQuery},
            {$addToSet: {rationInstList: {feeItemId: inst.feeItemId, sectionId: inst.sectionId}}}, {multi: true});
    }
}