/**
 * 编办业务逻辑
 *
 * @author CaiAoLin
 * @date 2017/8/16
 * @version
 */
import BaseModel from "../../common/base/base_model";
import mongoose from "mongoose";

class CompilationModel extends BaseModel {

    /**
     * 构造函数
     *
     * @return {void}
     */
    constructor() {
        let parent = super();
        parent.model =  mongoose.model('compilation');
        parent.init();
    }

    /**
     * 获取编办列表
     *
     * @return {Promise}
     */
    async getCompilationList(fields = null) {
        // 筛选字段
        let field = fields == null ?{_id: 1, name: 1, is_release: 1, categoryID: 1, description: 1,overWriteUrl: 1,example: 1, "ration_valuation.id": 1, "ration_valuation.name": 1, "ration_valuation.enable": 1,
            "bill_valuation.id": 1, "bill_valuation.name": 1, "bill_valuation.enable": 1}:fields;
        let compilationData = await this.findDataByCondition({name: {$ne: ''}}, field, false);

        return compilationData === null ? [] : compilationData;
    }

    /**
     * 获取编办列表
     *
     * @return {Promise}
     */
    async getList() {
        // 筛选字段
        let field = {_id: 1, name: 1, is_release: 1, description: 1, categoryID: 1};
        let compilationData = await this.findDataByCondition({name: {$ne: ''}, is_release: true}, field, false);

        return compilationData === null ? [] : compilationData;
    }

    /**
     * 根据id获取可用的编办数据
     *
     * @param {String} id
     * @return {Promise}
     */
    async getCompilationById(id) {
        let condition = {_id: id, is_release: true};
        let compilationData = await this.findDataByCondition(condition);
        if (!compilationData  || compilationData.bill_valuation === undefined) {
            return compilationData;
        }

        if (compilationData.bill_valuation.length > 0) {
            let enableValuation = [];
            for (let index in compilationData.bill_valuation) {
                if (compilationData.bill_valuation[index].enable) {
                    enableValuation.push(compilationData.bill_valuation[index]);
                }
            }
            compilationData.bill_valuation = enableValuation;
        }

        if (compilationData.ration_valuation.length > 0) {
            let enableValuation = [];
            for (let index in compilationData.ration_valuation) {
                if (compilationData.ration_valuation[index].enable) {
                    enableValuation.push(compilationData.ration_valuation[index]);
                }
            }
            compilationData.ration_valuation = enableValuation;
        }

        return compilationData;
    }

}

export default CompilationModel;
