| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 | /** * 组成物Spread * * @author CaiAoLin * @date 2017/7/18 * @version *//** * 构造函数 * * @return {void} */function CompositionSpread () {    this.isChanging = false;    this.sheetObj = null;    this.successCallback = null;    this.rightClickTarget = null;}/** * 初始化 * * @param {String} target * @return {Object} */CompositionSpread.prototype.init = function(target) {    let name = target === 'machine' ? '用量' : '消耗量';    let header = [        {name: '编码', field: 'code', visible: true},        {name: '名称', field: 'name', visible: true},        {name: '单位', field: 'unit', visible: true},        {name: 'ID', field: 'id', visible: false},        {name: '类型', field: 'unit_price.type', visible: false},        {name: '基价单价', field: "unit_price.base_price", visible: true},        {name: '调整基价', field: 'adjust_price', visible: true},        {name: '市场单价', field: "unit_price.market_price", visible: true},        {name: name, field: 'consumption', visible: true, validator: 'number'},        {name: 'CID', field: 'mix_ratio_id', visible: false},    ];    this.sheetObj = new CommonSpreadJs(header);    this.sheetObj.init(target);    // 获取列号    let codeColumn = this.sheetObj.getFieldColumn('code');    let unitColumn = this.sheetObj.getFieldColumn('unit');    let consumptionColumn = this.sheetObj.getFieldColumn('consumption');    // 居中样式    let centerStyleSetting = {hAlign: 1};    this.sheetObj.setStyle(-1, codeColumn, centerStyleSetting);    this.sheetObj.setStyle(-1, unitColumn, centerStyleSetting);    // 设置可编辑列    this.sheetObj.setColumnEditable(consumptionColumn);    // 绑定事件    let self = this;    this.sheetObj.bind(GC.Spread.Sheets.Events.ValueChanged, function(element, info) {        self.updateConsumption(info, self.successCallback);    });    return this.sheetObj;};/** * 初始化右键 * * @param {String} target * @return {void} */CompositionSpread.prototype.initRightClick = function(target) {    let activeSheet = this.sheetObj.getSheet();    let self = this;    $.contextMenu({        selector: '#' + target,        build: function ($trigger, e) {            self.rightClickTarget = SheetDataHelper.safeRightClickSelection($trigger, e, self.sheetObj.spread);            return self.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.viewport ||                self.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;        },        items: {            "deleteMixRatio": {                name: "删除",                icon: 'fa-trash-o',                disabled: function () {                    return self.rightClickTarget.row === undefined;                },                callback: function (key, opt) {                    let row = self.rightClickTarget.row;                    let idColumn = self.sheetObj.getFieldColumn('mix_ratio_id');                    let deleteId = activeSheet.getValue(row, idColumn);                    self.deleteComposition(deleteId, row, mixRatioSuccess);                }            },        }    });};/** * 获取组成物数据 * * @param {Number} projectGLJid * @return {void | boolean} */CompositionSpread.prototype.getRatioData = function(projectGLJid) {    projectGLJid = parseInt(projectGLJid);    let self = this;    if (isNaN(projectGLJid) || projectGLJid <= 0) {        this.sheetObj.setData(null);        return false;    }    $.ajax({        url: '/glj/get-ratio',        type: 'post',        data: {id: projectGLJid, project_id: scUrlUtil.GetQueryString('project')},        error: function() {            self.sheetObj.setData(null);        },        beforeSend: function() {        },        success: function(response) {            if (response.err === 0) {                response.data = JSON.parse(response.data);                // 设置数据                self.sheetObj.setData(response.data);                self.specialColumn(response.data);            } else {                self.sheetObj.setData(null);                console.log('不存在对应数据');            }        }    });};/** * 设置特殊单元格数据 * * @param {object} sourceData * @return {void} */CompositionSpread.prototype.specialColumn = function(sourceData) {    let rowCounter = 0;    // 获取市场单价列号    let consumptionColumn = this.sheetObj.getFieldColumn('consumption');    let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');    let activeSheet = this.sheetObj.getSheet();    for(let data of sourceData) {        // 把消耗量从对象中抽离出来        if (data.ratio_data.consumption !== undefined) {            activeSheet.setValue(rowCounter, consumptionColumn, data.ratio_data.consumption);            activeSheet.setValue(rowCounter, idColumn, data.ratio_data.id);        }        rowCounter++;    }};/** * 更新组成物消耗量或用量 * * @param {Object} info * @param {function} callback * return {void} */CompositionSpread.prototype.updateConsumption = function(info, callback) {    // 获取修改的数据    let column = info.col;    let row = info.row;    let field = this.sheetObj.getColumnField(column);    if (field === '') {        return false;    }    // 防止快速同时提交    if (this.isChanging) {        return false;    }    let activeSheet = this.sheetObj.getSheet();    // 校验数据    let value = info.newValue;    if (!this.sheetObj.checkData(column, value)) {        alert('数据格式错误,请重新输入!');        activeSheet.setValue(row, column, info.oldValue);        return false;    }    // 获取id    let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');    if (idColumn < 0) {        return false;    }    let id = activeSheet.getValue(row, idColumn);    let [parentMarketPrice, parentBasePrice] = this.getCompositionSumPrice('modify', row, info.newValue);    let self = this;    $.ajax({        url: '/glj/update',        type: 'post',        data: {id: id, field: 'mix_ratio.' + field, value: value, market_price: parentMarketPrice, base_price: parentBasePrice},        dataType: 'json',        error: function() {            alert('数据传输有误!');            self.isChanging = false;            activeSheet.setValue(row, column, info.oldValue);        },        beforeSend: function() {            self.isChanging = true;        },        success: function(response) {            self.isChanging = false;            // 修改失败则恢复原值            if (response.err !== 0) {                activeSheet.setValue(row, column, info.oldValue);                alert('更改数据失败!');            } else {                info.parentMarketPrice = parentMarketPrice;                info.parentBasePrice = parentBasePrice;                info.change = info.newValue - info.oldValue;                callback(info);            }        }    });};/** * 获取当前所有组成物累积的市场单价和基价单价 * 用于同步修改父级市场单价和基价单价 * * @param {String} scene * @param {Number} affectRow * @param {Number} newValue * @return {Array} */CompositionSpread.prototype.getCompositionSumPrice = function(scene, affectRow, newValue = 0) {    let activeSheet = this.sheetObj.getSheet();    // 计算父级3个价格    let maxRow = activeSheet.getRowCount();    // 获取对应列的列号    let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');    let consumptionColumn = this.sheetObj.getFieldColumn('consumption');    let basePriceColumn = this.sheetObj.getFieldColumn('unit_price.base_price');    let parentMarketPrice = 0;    let parentBasePrice = 0;    for(let i = 0; i < maxRow; i++) {        // 获取市场单价        let marketPrice = activeSheet.getValue(i, marketPriceColumn);        // 获取基价单价        let basePrice = activeSheet.getValue(i, basePriceColumn);        // 如果是删除则忽略即将被删除的行数据        if (scene === 'delete' && affectRow === i) {            continue;        }        // 获取消耗量(如果是当前修改的行则替换数据)        let consumption = i === affectRow ? newValue : activeSheet.getValue(i, consumptionColumn);        parentMarketPrice += consumption * marketPrice;        parentBasePrice += consumption * basePrice;    }    parentMarketPrice = parentMarketPrice.toDecimal(2);    parentBasePrice = parentBasePrice.toDecimal(2);    return [parentMarketPrice, parentBasePrice]};/** * 删除组成物 * * @param {Number} id * @param {Number} row * @param {function} callback * @return {void | boolean} */CompositionSpread.prototype.deleteComposition = function (id, row, callback) {    id = parseInt(id);    if (isNaN(id) || id <= 0) {        alert('参数错误!');    }    if (isDeleting) {        return false;    }    let activeSheet = this.sheetObj.getSheet();    // 获取当前行的消耗量    let consumptionColumn = this.sheetObj.getFieldColumn('consumption');    let consumption = activeSheet.getValue(row, consumptionColumn);    let self = this;    $.ajax({        url: '/glj/delete-ratio',        type: 'post',        data: {id: id},        dataType: 'json',        error: function() {            isDeleting = false;            alert('服务器繁忙');        },        beforeSend: function() {            isDeleting = true;        },        success: function(response) {            if (response.err === 0) {                // 计算同级的市场单价和基价单价                let [parentMarketPrice, parentBasePrice] = self.getCompositionSumPrice('delete', row);                let info = {                    parentMarketPrice: parentMarketPrice,                    parentBasePrice: parentBasePrice,                    change: -consumption                };                activeSheet.setValue(row, consumptionColumn, 0);                callback(info);            }        }    });};
 |