You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

103 lines
4.7 KiB

import random
import string
import gevent
from flask import make_response, send_from_directory, current_app
from pypinyin import lazy_pinyin, Style
from models.inStockExcel import InStockExcel
from utils import instockHelper
from utils.BatchCode import GetBatchCode
from utils.apiDoc import *
import os
import pandas as pd
filePath = './file/'
@file.route('/inStock')
class FileInStock(Resource):
@staticmethod
def post():
token = request.headers.get('X-Token')
inStockTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
excelFile = request.files.get('files')
batchCode = GetBatchCode()
batch_code_list = []
is_his_file = False
if not os.path.exists(filePath):
os.makedirs(filePath)
fileName = ''.join(random.choices(string.ascii_uppercase +
string.digits, k=16)) + \
os.path.splitext(excelFile.filename)[-1]
excelFile.save(filePath + fileName)
df = pd.read_excel(filePath + fileName, sheet_name='Sheet1')
if df.keys()[0] == '出库单号':
is_his_file = True
df = pd.read_excel(filePath + fileName, sheet_name='Sheet1',
names=['Code', 'Name', 'Specification', 'SupplierName', 'Maker', 'CertificationCode',
'ProductionBatch', 'Position', 'TypeName', 'Unit', 'SmallestUnit',
'Ratio', 'UnitPrice', 'SellingPrice', 'MaxValue', 'MinValue',
'Number', 'Expiration'])
else:
df = pd.read_excel(filePath + fileName, sheet_name='Sheet1',
names=['Name', 'Specification', 'SupplierName', 'Maker', 'CertificationCode',
'ProductionBatch', 'Position', 'TypeName', 'Unit', 'SmallestUnit',
'Ratio', 'UnitPrice', 'SellingPrice', 'MaxValue', 'MinValue',
'Number', 'Expiration'])
# 文件转实体
for index in df.index:
data = InStockExcel()
setattr(data, 'Name', str(df['Name'][index]).strip())
setattr(data, 'Specification', str(df['Specification'][index]))
setattr(data, 'SupplierName',
'' if pd.isnull(df['SupplierName'][index]) else str(df['SupplierName'][index]))
setattr(data, 'Maker', str(df['Maker'][index]))
setattr(data, 'CertificationCode',
'' if pd.isnull(df['CertificationCode'][index]) else str(df['CertificationCode'][index]))
setattr(data, 'ProductionBatch',
'' if pd.isnull(df['ProductionBatch'][index]) else str(df['ProductionBatch'][index]))
setattr(data, 'Position', str(df['Position'][index]))
setattr(data, 'TypeName', str(df['TypeName'][index]))
setattr(data, 'Unit', str(df['Unit'][index]))
setattr(data, 'SmallestUnit',
None if pd.isnull(df['SmallestUnit'][index]) else str(df['SmallestUnit'][index]))
setattr(data, 'Ratio',
None if pd.isnull(df['Ratio'][index]) else int(df['Ratio'][index]))
setattr(data, 'UnitPrice', float(df['UnitPrice'][index]))
setattr(data, 'SellingPrice', float(df['SellingPrice'][index]))
setattr(data, 'MaxValue', int(df['MaxValue'][index]))
setattr(data, 'MinValue', int(df['MinValue'][index]))
setattr(data, 'Number', int(df['Number'][index]))
setattr(data, 'Expiration', str(df['Expiration'][index]))
if is_his_file:
batchCode = str(df['Code'][index])
batch_code_list.append(batchCode)
# 入库信息写入
instockHelper.in_stock(data, batchCode, inStockTime, token, request.json, remark='文件入库')
try:
# 是否入库成功
db.session.commit()
except gevent.Timeout:
# 回调
db.session.invalidate()
raise
return SuccessResponse(ResultCode.ERROR, None, None)
except Exception:
# 回调
db.session.rollback()
raise
return SuccessResponse(ResultCode.ERROR, None, None)
os.remove(filePath + fileName)
return SuccessResponse(ResultCode.SUCCESS, None, fileName)
@file.route('/downloadInStock')
class DownloadInStock(Resource):
@staticmethod
def get():
uploads = os.path.join(os.path.abspath(os.path.dirname(current_app.root_path)),
app.config[request.args.get('path')])
app.logger.info(uploads)
return send_from_directory(directory=uploads, filename=request.args.get('fileName'))