123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- /**
- * Created by Tony on 2017/4/20.
- */
- var rationRepository = require("../models/repository_map");
- import baseController from "../../common/base/base_controller";
- import CompilationModel from "../../users/models/compilation_model";
- import gljMapModel from "../../std_glj_lib/models/schemas";
- import async from "async";
- var callback = function(req, res, err, message, data){
- res.json({error: err, message: message, data: data});
- };
- // 上传控件
- const multiparty = require("multiparty");
- const fs = require("fs");
- // excel解析
- const excel = require("node-xlsx");
- const rationItem = require("../models/ration_item");
- class RationRepositoryController extends baseController {
- async getCompilationList(req, res) {
- try {
- let compilationModel = new CompilationModel(), rst = [];
- let compilationList = await compilationModel.getCompilationList();
- if (compilationList.length <= 0) {
- throw '没有数据';
- }
- else {
- compilationList.forEach(function (compilation) {
- rst.push({_id: compilation._id, name: compilation.name});
- });
- //获得相关编办下的工料机库
- let parallelFucs = [];
- for (let i = 0; i < rst.length; i++) {
- parallelFucs.push((function (obj) {
- return function (cb) {
- gljMapModel.find({compilationId: obj._id, deleted: false}, function (err, result) {
- if (err) {
- cb(err);
- }
- else {
- cb(null, result);
- }
- })
- }
- })(rst[i]));
- }
- async.parallel(parallelFucs, function (err, result) {
- if (err) {
- callback(req, res, err, '没有数据', null);
- }
- else {
- let gljLibsRst = [];
- for (let i = 0; i < result.length; i++) {
- for (let j = 0; j < result[i].length; j++) {
- gljLibsRst.push(result[i][j]);
- }
- }
- callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
- }
- })
- }
- }
- catch (err) {
- callback(req, res, err, '没有数据', null);
- }
- }
- addRationRepository(req, res) {
- var rationObj = JSON.parse(req.body.rationRepObj);
- rationRepository.addRationRepository(rationObj, function (err, data) {
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- })
- }
- getRationLib(req, res){
- let data = JSON.parse(req.body.data);
- let libId = data.libId;
- rationRepository.getRationLib(libId, function(err, data){
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- });
- }
- getDisPlayRationLibs(req, res) {
- rationRepository.getDisplayRationLibs(function (err, data) {
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- });
- }
- getRealLibName(req, res) {
- var libName = req.body.rationName;
- rationRepository.getRealLibName(libName, function (err, data) {
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- })
- }
- getLibIDByName(req, res) {
- rationRepository.getLibIDByName(req.body.libName, function (err, data) {
- if (data) {
- callback(req, res, err, "has ID", data);
- } else {
- callback(req, res, err, "no ID", null);
- }
- })
- }
- deleteRationLib(req, res) {
- let oprtor = req.body.oprtor,
- libId = req.body.libId;
- rationRepository.deleteRationLib(oprtor, libId, function (err, message) {
- callback(req, res, err, message, null);
- });
- }
- updateRationRepositoryName(req, res) {
- let oprtor = req.body.oprtor,
- renameObj = JSON.parse(req.body.renameObj);
- rationRepository.updateName(oprtor, renameObj, function (err, message) {
- callback(req, res, err, message, null);
- })
- }
- /**
- * 上传定额库原始数据
- *
- * @param {Object} request
- * @param {Object} response
- * @return {void}
- */
- async uploadSourceData(request, response) {
- let responseData = {
- err: 0,
- msg: ''
- };
- const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
- try {
- const uploadOption = {
- uploadDir: './public/tmp'
- };
- const form = new multiparty.Form(uploadOption);
- form.parse(request, function(err, fields, files) {
- const rationRepId = fields.rationRepId !== undefined && fields.rationRepId.length > 0 ?
- fields.rationRepId[0] : 0;
- if (rationRepId <= 0) {
- throw '参数错误';
- }
- const file = files.file !== undefined ? files.file[0] : null;
- if (err || file === null) {
- throw '上传失败';
- }
- // 判断类型
- if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
- throw '不支持该类型';
- }
- // 重命名文件名
- const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
- fs.renameSync(file.path, uploadFullName);
- const sheet = excel.parse(uploadFullName);
- if (sheet[0] === undefined || sheet[0].data === undefined) {
- throw 'excel没有对应数据';
- }
- rationItem.batchAddFromExcel(rationRepId, sheet[0].data);
- response.json(responseData);
- });
- } catch (error) {
- responseData.err = 1;
- responseData.msg = error;
- response.json(responseData);
- }
- return;
- }
- }
- export default RationRepositoryController;
- /*
- module.exports = {
- addRationRepository:function(req,res){
- var rationObj = JSON.parse(req.body.rationRepObj);
- rationRepository.addRationRepository(rationObj,function(err,data){
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- })
- },
- getDisPlayRationLibs: function(req, res){
- rationRepository.getDisplayRationLibs(function(err, data){
- if (data) {
- callback(req, res, err, "has data",data);
- } else {
- callback(req, res, err, "no data", null);
- }
- });
- },
- getRealLibName:function(req,res){
- var libName = req.body.rationName;
- rationRepository.getRealLibName(libName,function(err,data){
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- })
- },
- getLibIDByName:function(req,res){
- rationRepository.getLibIDByName(req.body.libName, function(err,data){
- if (data) {
- callback(req, res, err, "has ID", data);
- } else {
- callback(req, res, err, "no ID", null);
- }
- })
- },
- deleteRationLib:function(req,res){
- var rationName = req.body.rationName;
- rationRepository.deleteRationLib(rationName,function(err,data){
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- })
- },
- updateRationRepositoryName: function(req, res) {
- var orgName = req.body.rationName;
- var newName = req.body.newName;
- rationRepository.updateName(orgName, newName, function(err, data){
- if (data) {
- callback(req, res, err, "has data", data);
- } else {
- callback(req, res, err, "no data", null);
- }
- });
- }
- }*/
|