/**
 * Created by Mai on 2017/4/1.
 */
var ration_coe = {
    createNew: function (project) {
        // 用户定义private方法
        var tools = {};

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

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

        // prototype用于定义public方法
        ration_coe.prototype.loadData = function (datas) {
            this.datas = datas;
        };

        // 提交数据后返回数据处理
        ration_coe.prototype.doAfterUpdate = function(err, data){
            if(!err){
                if(data.updateTpye=='ut_update'){
                    this.refreshAfterUpdate(data);
                }else if(data.updateTpye=='ut_delete'){
                    this.refreshAfterDelete(data);
                } else {
                    this.refreshAfterSave(data);
                }
            }
        };
        ration_coe.prototype.refreshAfterSave=function(data){
            if(projectObj.project.ration_coe.datas&&Array.isArray(projectObj.project.ration_coe.datas)){
                projectObj.project.ration_coe.datas = projectObj.project.ration_coe.datas.concat(data);
            }else {
                projectObj.project.ration_coe.datas = data;
            }
            gljOprObj.showCoeData(gljOprObj.coeSheet,gljOprObj.coeSetting,data);
            gljOprObj.coeSheetData=data;
            // SheetDataHelper.loadSheetData(setting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
        };
        ration_coe.prototype.refreshAfterUpdate=function(data){
            var coe_list = projectObj.project.ration_coe.datas;
            var coe_index= _.findIndex(coe_list,function(coe){
                return coe.ID==data.query.ID&&coe.projectID==data.query.projectID;
            })
            _.forEach(data.doc, function(n, key) {
                coe_list[coe_index][key] = n;
            });

            var showList = _.filter(coe_list,{'projectID':data.query.projectID,'rationID':coe_list[coe_index].rationID});
            gljOprObj.coeSheetData=showList;
            gljOprObj.showCoeData(gljOprObj.coeSheet,gljOprObj.coeSetting,showList);
        };
        ration_coe.prototype.refreshAfterDelete=function(data){
            var glj_list = projectObj.project.ration_coe.datas;
            _.remove(glj_list,data.query);
            _.remove(gljOprObj.sheetData,data.query);
            gljOprObj.showCoeData(gljOprObj.coeSheet,gljOprObj.coeSetting,gljOprObj.sheetData);
        };

        ration_coe.prototype.getRationCoedata=function(newRation,data){
            var criteria= {};
            criteria.ration_coe_list = [];
            var dataLength = 0;
            if(data.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
                dataLength = data.rationCoeList.length
            }
            for(var i=0;i<=dataLength;i++){;
                var newCoe = {
                    libID :data.rationRepId,
                    rationID:newRation.ID,
                    projectID :  newRation.projectID
                }
                if(i==dataLength){
                    newCoe.coeID=-1;
                    newCoe.name = '自定义系数';
                    newCoe.content='人工×1，材料×1，机械×1，主材×1，设备×1';
                    newCoe.isAdjust=0;
                    newCoe.coes = this.getCustomerCoeData();
                }else {
                    newCoe.coeID= data.rationCoeList[i];
                }
                criteria.ration_coe_list.push(newCoe);
            }
            criteria.updateType = 'ut_create';
            return criteria;
        };
        ration_coe.prototype.getCustomerCoeData = function () {
            var coeList = [];
            coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'定额'});
            coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'人工'});
            coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'材料'});
            coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'机械'});
            coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'主材'});
            coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'设备'});
            return coeList;
        };

        ration_coe.prototype.getUpdateData=function(type,query,doc,callfunction){
            var updateData = [];
            var newobj = {
                'updateType': type,
                'query': query,
            }
            if(doc){
                newobj['doc']=doc;
            }
            if(callfunction){
                newobj['updateFunction']=callfunction;
            }
            updateData.push(newobj);
            return updateData;
        };
        ration_coe.prototype.adjustCoeClick = function(recode,newval){
            var query = {
                'ID':recode.ID,
                'projectID': recode.projectID,
                'rationID':recode.rationID
            };
            var doc ={
                isAdjust:newval,
            };
            var updateData = this.getUpdateData('ut_update',query,doc,'adjustUpdate');
            project.pushNow('updateRationCOE',[this.getSourceType()],updateData);
        };

        ration_coe.prototype.updateCustomerCoe = function (data) {
            var updateData = this.getUpdateData('ut_update',data.query,data.doc,'updateCustomerCoe');
            project.pushNow('updateCustomerCoe',[this.getSourceType()],updateData);
        };
        ration_coe.prototype.deleteByRation = function(ration){
            var coe_list = projectObj.project.ration_coe.datas;
            var newList =_.filter(coe_list,(coe)=>{
                return coe.rationID!=ration.ID;
            });
            if(newList!=undefined){
                projectObj.project.ration_coe.datas = newList;
            }
        };
        return new ration_coe(project);
    }



};