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

class CompilationModel extends BaseModel {

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

    /**
     * 获取编办列表
     *
     * @return {Promise}
     */
    async getList() {
        // 筛选字段
        let field = {_id: 1, name: 1, is_release: 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.bill_valuation === undefined) {
            return compilationData;
        }

        if (compilationData.bill_valuation.length > 0) {
            for (let index in compilationData.bill_valuation) {
                if (compilationData.bill_valuation[index].enable) {
                    continue;
                }
                delete compilationData.bill_valuation[index];
            }
        }

        if (compilationData.ration_valuation.length > 0) {
            for (let index in compilationData.ration_valuation) {
                if (compilationData.ration_valuation[index].enable) {
                    continue;
                }
                delete compilationData.ration_valuation[index];
            }
        }

        return compilationData;
    }

}

export default CompilationModel;
