'use strict';

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

module.exports = {
    getComplementaryCoes,
    getCoeItemsByIDs,
    getCoeItemsByNos,
    saveToCoeList,
};

const mongoose = require("mongoose");
const counter = require('../../../public/counter/counter');
const compleCoeModel = mongoose.model('complementary_ration_coe_list');
const async = require('async');
const deleteQuery = [{deleteInfo: null}, {'deleteInfo.deleted': false}];

//获取用户总的补充子目换算
async function getComplementaryCoes(userId, compilationId) {
    let coeData =  await compleCoeModel.find({userId: userId, compilationId: compilationId, $or: deleteQuery});
    return coeData;
}


//补充子目换算增删改
async function saveToCoeList(userId, compilationId, data, callback) {
    let bulks = [],
        newDatas = [];
    if (data.addArr && data.addArr.length > 0) {
        let insertOprs = await getInsertBulk(userId, compilationId, data.addArr);
        bulks = bulks.concat(insertOprs.insertBulk);
        newDatas = insertOprs.newDatas;
    }
    if (data.updateArr && data.updateArr.length > 0) {
        bulks = bulks.concat(getUpdateBulk(data.updateArr));
    }
    if (data.deleteArr && data.deleteArr.length > 0) {
        let deleteObj = {
            deleted: true,
            deleteDateTime: new Date(),
            deleteBy: userId
        };
        for (let delData of data.deleteArr) {
            delData.deleteInfo = deleteObj
        }
        bulks = bulks.concat(getUpdateBulk(data.deleteArr));
    }
    if (bulks.length > 0) {
        await compleCoeModel.bulkWrite(bulks);
    }
    return newDatas;
}

async function getInsertBulk(userId, compilationId, addArr) {
    let insertBulk = [],
        newDatas = [];
    let counterResult = await counter.counterDAO.getIDAfterCountSync(counter.moduleName.complementaryCoeList, addArr.length);
    let maxId = counterResult.sequence_value;
    for (let insertData of addArr) {
        insertData.userId = userId;
        insertData.compilationId = compilationId;
        insertData.ID = (maxId - (addArr.length - 1) + addArr.indexOf(insertData));
        insertData.coes = [];
        newDatas.push(insertData);
        insertBulk.push({insertOne: {document: insertData}});
    }
    return {insertBulk, newDatas};
}

function getUpdateBulk(updateArr) {
    let updateBulk = [],
        updateFields = ['name', 'content', 'coes', 'deleteInfo'];
    for (let updateData of updateArr) {
        let needSet = {};
        for (let attr in updateData){
            if (updateFields.includes(attr)){
                needSet[attr] = updateData[attr];
            }
        }
        updateBulk.push({updateOne:{filter: {ID: updateData.ID}, update: {$set: needSet}}});
    }
    return updateBulk;
}
//根据序号获取子目换算，插入子目换算时用
async function getCoeItemsByNos(userId, compilationId, serialNos) {
    return await compleCoeModel.find({userId: userId, compilationId: compilationId, serialNo: {$in: serialNos}}, '-_id ID serialNo name content');
}

//根据ID获取子目换算，获取定额子目换算用
async function getCoeItemsByIDs(coeIds) {
    return await compleCoeModel.find({ID: {$in: coeIds}}, '-_id ID serialNo name content');
}

