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

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

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

        // 从后台获取数据
        /*glj.prototype.pullData = function (){
            this.project.pullData(
                '/glj/getData',
                {projectID: this.project.ID},
                function(result){
                    if (result.error ===0){
                        this.loadDatas(result.data);
                    }
                    else {
                        // to do: 错误处理需要细化
                        alert(result.message);
                    }
                },
                function (){}//to do: 错误处理需要细化
            )
        };*/
        // prototype用于定义public方法
        glj.prototype.loadData = function (datas) {
            this.datas = datas;
        };

        // 提交数据后的错误处理方法
        glj.prototype.doAfterUpdate = function(err, data){
            // to do
        };

        // CSL,2017.05.09
        glj.prototype.modifyQuantity = function (data, newQuantity) {
            this.project.beginUpdate('modifyQuantity');
            data.quantity = newQuantity;
            data.customQuantity = newQuantity;
            data.updateType = 'ut_update';
            this.project.push(this.getSourceType, data);
            this.project.endUpdate();
        };

        glj.prototype.modifyPrice = function (data, newPrice) {
            this.project.beginUpdate('modifyPrice');
            data.price = newPrice;
            data.updateType = 'ut_update';
            this.project.push(this.getSourceType, data);
            this.project.endUpdate();
        };

        glj.prototype.deleteGLJ = function (data) {
            this.project.beginUpdate('deleteGLJ');
            data.customQuantity = 0;
            data.quantity = 0;
            data.rationItemQuantity = 0;
            data.updateType = 'ut_update';
            this.project.push(this.getSourceType, data);
            this.project.endUpdate();
        };

        glj.prototype.replaceGLJ = function (data, newGLJID) {
            this.project.beginUpdate('replaceGLJ');
            data.GLJID = newGLJID;
            data.updateType = 'ut_update';
            this.project.push(this.getSourceType, data);
            this.project.endUpdate();
        };

        glj.prototype.addGLJ = function (data) {
            var newGLJ = {};
            this.project.beginUpdate('addGLJ');
            newGLJ.projectID = this.project;
            newGLJ.GLJID = data.GLJID;
            newGLJ.rGLJID = data.rGLJID;
            newGLJ.rationID = data.rationID;
            newGLJ.billsItemID = data.billsItemID;
            newGLJ.orgRQuantity = data.orgRQuantity;
            newGLJ.rQuantity = data.rQuantity;
            newGLJ.quantity = data.quantity;
            newGLJ.rationPrice = data.rationPrice;
            newGLJ.adjustPrice = data.adjustPrice;
            newGLJ.marketPrice = data.marketPrice;
            newGLJ.price = data.price;

            newGLJ.updateType = 'ut_create';
            this.project.push(this.getSourceType, newGLJ);
            this.project.endUpdate();
        };


        return new glj(project);
    }
};