/**
 * Created by zhang on 2018/2/24.
 */
let ration_installation = {
    createNew: function (project) {
        // 用户定义private方法
        var tools = {};

        // 所有通过this访问的属性，都不应在此单元外部进行写入操作
        var ration_installation = function (proj) {
            // this.project = proj;
            this.datas = [];

            var sourceType = ModuleNames.ration_installation;
            this.getSourceType = function () {
                return sourceType;
            }
            proj.registerModule(ModuleNames.ration_installation, this);
        };

        // prototype用于定义public方法
        ration_installation.prototype.loadData = function (datas) {
            this.datas = datas;
        };
        ration_installation.prototype.addDatasToList = function (datas) {
            let  me = this;
            if(datas&&datas.length>0){
                if (me.datas && Array.isArray(me.datas)) {
                    me.datas = me.datas.concat(datas);
                } else {
                    me.datas = datas;
                }
            }
        };
        ration_installation.prototype.getInstallationByRationID = function (rationID) {
            let insList = _.filter(this.datas, {'rationID': rationID});
            return insList;
        };
        ration_installation.prototype.getBySectionID = function(sectionID){
            var ri_list = this.datas;
            return _.filter(ri_list,{'sectionId':sectionID});
        };
        ration_installation.prototype.deleteByRation = function(ration){
            var ri_list = projectObj.project.ration_installation.datas;
            _.remove(ri_list,{'rationID':ration.ID});
        };
        ration_installation.prototype.getByID = function (ID) {
            let  me = this;
            let ri = _.find(me.datas,{'ID':ID})
            return ri;
        };
        ration_installation.prototype.getCalcRIByItemID = function (libID,itemID) {//按费用项ID取费用规则不为空的定额安装增加费
            return _.filter(this.datas,function (item) {
                if(item.libID == libID && item.feeItemId == itemID && item.ruleId && item.ruleId !=""){
                    return true;
                }
                return false;
            })
        };
        ration_installation.prototype.getDataByConditions = function (conditions) {
            return _.filter(this.datas,function (item) {
                let match = true;
                for(let ckey in conditions){
                    if( Array.isArray(conditions[ckey])){
                       let isContain =  _.includes(conditions[ckey],item[ckey]);
                       if(!isContain){//不包含
                           match = false;
                           break;
                       }
                    }else {
                        if(conditions[ckey]!=item[ckey]){
                            match = false;
                            break;
                        }
                    }
                }
                return match;
            })
        };
        ration_installation.prototype.applyRuleByIDs = function(projectID,IDs,ruleId,callback){
            let me = this;
            $.bootstrapLoading.start();
            CommonAjax.post('/installation/applyRuleByIDs',{projectID:projectID,IDs:IDs,ruleId:ruleId},function (data) {
                //更新缓存
                for(let ID of IDs){
                    let ri = _.find(me.datas,{'ID':ID});
                    ri.ruleId = ruleId;
                    ri.unifiedSetting = 0;
                }
                if(callback){
                    callback();
                }
                $.bootstrapLoading.end();
            },function () {
                $.bootstrapLoading.end();
            })

        };
        ration_installation.prototype.update = function (updateData,callback) {
            let  me = this;
            $.bootstrapLoading.start();
            CommonAjax.post('/installation/updateRationInstallation',updateData,function (data) {
                //更新缓存
                let ri = _.find(me.datas,{'ID':updateData.ID});
                if(ri){
                    for(let key in updateData){
                        ri[key] = updateData[key];
                    }
                }
                if(callback){
                    callback();
                }
                $.bootstrapLoading.end();
            },function () {
                $.bootstrapLoading.end();
            })
        };

        return new ration_installation(project);
    }
};