exportTranPlan.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import { sjTime, isNumber } from '@/utils/sharedJsFile'
  2. import { getCookie } from '@/utils/util.js'
  3. export default {
  4. data() {
  5. return {
  6. tableData: [],
  7. shipperName: '四川省达州钢铁有限责任公司',
  8. //销售订单主表数据
  9. saleOrderList: [
  10. 'receiveName',
  11. 'isSelfMention',
  12. 'saleRemark',
  13. 'saleOrderReceiveCustomer',
  14. 'salerName',
  15. 'saleOrderNo',
  16. 'saleArea'
  17. ],
  18. saleOrderMapList: [
  19. 'cxh',
  20. 'province',
  21. 'district',
  22. 'town',
  23. 'truckRemark',
  24. 'place',
  25. 'saleOrderConsigneeTel',
  26. 'saleDateOfReceipt',
  27. 'materialName',
  28. 'materialSpecification',
  29. 'materialModel',
  30. 'orderPlanWeight',
  31. 'meterNumber',
  32. 'isPoundSale',
  33. 'inboundWarehouse'
  34. ]
  35. }
  36. },
  37. methods: {
  38. importExcel(file) {
  39. let that = this
  40. if (!file) {
  41. that.$message({
  42. message: '文件错误!',
  43. type: 'warning'
  44. })
  45. return
  46. }
  47. this.tableData = []
  48. var reader = new FileReader()
  49. var data = null
  50. var workbook = null
  51. //设置读取操作
  52. reader.onload = function(e) {
  53. data = e.target.result
  54. workbook = XLSX.read(data, {
  55. type: 'binary'
  56. })
  57. let rows = XLSX.utils.sheet_to_json(
  58. workbook.Sheets[workbook.SheetNames[0]]
  59. ) //只取第一页
  60. console.log(rows, 'rows')
  61. let uuid = Math.floor(Math.random() * 60 * 24)
  62. Array.from(rows).forEach((e, index) => {
  63. let map = {}
  64. try {
  65. Object.keys(e).forEach(e1 => {
  66. console.log(e1, 'e1')
  67. if (e1.includes('转运计划编号')) {
  68. map.saleOrderNo = e[e1]
  69. }
  70. if (e1.includes('物资名称')) {
  71. map.materialName = e[e1]
  72. if (
  73. e[e1].includes('盘螺') ||
  74. e[e1].includes('乱尺') ||
  75. e[e1].includes('盘元') ||
  76. e[e1].includes('盘圆')
  77. ) {
  78. map.isPoundSale = 0
  79. } else {
  80. map.isPoundSale = 1
  81. }
  82. //确定更改的物资有无米数
  83. if (e[e1].includes('米')) {
  84. map.meterNumber = Number(e[e1].replace(/[^0.0-9.0]/gi, ''))
  85. } else {
  86. map.meterNumber = null
  87. }
  88. }
  89. if (e1.includes('规格型号')) {
  90. // console.log(e[e1])
  91. let str = e[e1]
  92. let regex = /\((.+?)\)/g
  93. if (str.match(regex) != null) {
  94. let spa = str.replace(str.match(regex)[0], '')
  95. let model = str
  96. .match(regex)[0]
  97. .replace('(', '')
  98. .replace(')', '')
  99. map.materialModel = model
  100. if ((spa + '').includes('Φ')) {
  101. map.materialSpecification = spa
  102. } else {
  103. map.materialSpecification = 'Φ' + spa
  104. }
  105. } else {
  106. map.materialSpecification = e[e1]
  107. map.model = null
  108. }
  109. }
  110. if (e1.includes('下单客户')) {
  111. map.receiveName = e[e1]
  112. }
  113. if (
  114. e1.includes('米数') &&
  115. (map.meterNumber == null || map.meterNumber != '')
  116. ) {
  117. map.meterNumber = e[e1]
  118. }
  119. if (e1.includes('物资规格') && !e1.includes('规格型号')) {
  120. if ((e[e1] + '').includes('Φ')) {
  121. map.materialSpecification = e[e1]
  122. } else {
  123. map.materialSpecification = 'Φ' + e[e1]
  124. }
  125. }
  126. if (e1.includes('物资型号') && !e1.includes('规格型号')) {
  127. map.materialModel = e[e1]
  128. }
  129. if (e1.includes('物资件数')) {
  130. if (isNumber(e[e1])) {
  131. map.orderPlanWeight = e[e1]
  132. } else {
  133. throw new Error(
  134. `该Excel第--${index +
  135. 2}--行-“物资件数”错误,请核实后再输入(件数输入整数)`
  136. )
  137. }
  138. }
  139. if (e1.includes('车序号')) {
  140. if (typeof e[e1] != 'undefined' && isNumber(e[e1])) {
  141. map.cxh = e[e1]
  142. } else {
  143. throw new Error(
  144. `该Excel第--${index +
  145. 2}--行-“车序号”错误,请核实后再输入(车序号输入正整数)`
  146. )
  147. }
  148. }
  149. // if (e1.includes('收款公司')) {
  150. // if (that.receivingCompany.indexOf(e[e1]) === -1) {
  151. // throw new Error(
  152. // `该Excel第--${index + 2}--行-“收款公司”错误,请核实后再输入`
  153. // )
  154. // } else {
  155. // map.saleOrderReceiveCustomer = e[e1]
  156. // }
  157. // }
  158. if (e1.includes('业务员')) {
  159. map.salerName = e[e1]
  160. }
  161. if (e1.includes('摘要')) {
  162. map.saleRemark = e[e1]
  163. if (typeof map.saleRemark === 'string') {
  164. // 首先中文逗号转英文逗号
  165. map.saleRemark = map.saleRemark.replace(',', ',')
  166. let reg = /钢材.*组/g
  167. let reg1 = /直发.*(,|\s){0,1}/
  168. let saleAreaList = map.saleRemark.match(reg)
  169. let directlyList = map.saleRemark.match(reg1)
  170. if (saleAreaList != null && saleAreaList.length > 0) {
  171. map.saleRemark = saleAreaList[0]
  172. } else {
  173. throw new Error(
  174. `该Excel第--${index +
  175. 2}--行-“摘要填写”错误,请核实后再输入(应为“钢材某某组”)`
  176. )
  177. }
  178. if (directlyList != null && directlyList.length > 0) {
  179. map.saleRemark = map.saleRemark + ',' + directlyList[0]
  180. }
  181. }
  182. }
  183. if (e1.includes('省')) {
  184. map.province = e[e1]
  185. }
  186. if (e1.includes('市')) {
  187. map.district = e[e1]
  188. }
  189. if (e1.includes('县/区')) {
  190. map.town = e[e1]
  191. }
  192. if (e1.includes('具体收货地址')) {
  193. map.place = e[e1]
  194. }
  195. if (e1.includes('收货客户电话')) {
  196. map.saleOrderConsigneeTel = e[e1]
  197. }
  198. if (e1.includes('备注')) {
  199. map.truckRemark = e[e1]
  200. }
  201. if (e1.includes('出库库房')) {
  202. map.inboundWarehouse = e[e1]
  203. }
  204. if (e1.includes('是否自提')) {
  205. if (e[e1] === '是' || e[e1] === '否') {
  206. map.isSelfMention = e[e1]
  207. } else {
  208. map.isSelfMention = '否'
  209. }
  210. }
  211. })
  212. map.shipperName = that.shipperName
  213. if (typeof map.isSelfMention === 'undefined') {
  214. map.isSelfMention = '否'
  215. }
  216. if (typeof map.cxh === 'undefined') {
  217. map.cxh = Number(
  218. `${Math.floor(
  219. Math.random() *
  220. Math.random() *
  221. Math.random() *
  222. Math.random() *
  223. Math.random() *
  224. new Date().getTime() *
  225. 10000000
  226. )}${uuid}`
  227. )
  228. }
  229. if (map.saleRemark && map.receiveName && map.materialName) {
  230. that.tableData.push(map)
  231. }
  232. } catch (es) {
  233. that.$message.error(es.message)
  234. }
  235. })
  236. that.addExcelInport()
  237. return
  238. }
  239. reader.readAsBinaryString(file.raw) //以二进制方式读取
  240. },
  241. addExcelInport() {
  242. //加载虚拟
  243. const loading = this.$loading({
  244. lock: true,
  245. text: '正在快马加鞭为您生成销售订单,请稍候',
  246. spinner: 'el-icon-loading',
  247. background: 'rgba(0, 0, 0, 0.7)'
  248. })
  249. //遍历数组,根据摘要和下单客户自动给出销售订单编号
  250. for (let i = 0; i < this.tableData.length; i++) {
  251. if (i === 0) {
  252. this.tableData[i].saleOrderNo = 1
  253. } else {
  254. let sortArr = this.tableData
  255. .filter(e => {
  256. return (
  257. typeof e.saleOrderNo != 'undefined' &&
  258. e.saleOrderNo != 'undefined' &&
  259. isNumber(e.saleOrderNo)
  260. )
  261. })
  262. .map(e => {
  263. if (
  264. typeof e.saleOrderNo != 'undefined' &&
  265. e.saleOrderNo != 'undefined' &&
  266. isNumber(e.saleOrderNo)
  267. ) {
  268. return e.saleOrderNo
  269. }
  270. })
  271. .sort((a, b) => {
  272. return a - b
  273. })
  274. //当大于0的时候,需要遍历整个数组,与之相等的有哪些
  275. for (let j = 0; j < i; j++) {
  276. if (
  277. this.tableData[i].saleRemark === this.tableData[j].saleRemark &&
  278. this.tableData[i].receiveName === this.tableData[j].receiveName &&
  279. this.tableData[i].inboundWarehouse ===
  280. this.tableData[j].inboundWarehouse
  281. ) {
  282. this.tableData[i].saleOrderNo = this.tableData[j].saleOrderNo
  283. break
  284. } else {
  285. if (j == i - 1) {
  286. this.tableData[i].saleOrderNo = sortArr[sortArr.length - 1] + 1
  287. }
  288. }
  289. }
  290. }
  291. }
  292. // this.tableData = this.tableData.sort(this.compare("saleOrderNo"));
  293. // debugger;
  294. //需要遍历数组,以销售订单编号为分类
  295. const map1 = this.tableData.reduce((result, item) => {
  296. result[item.saleOrderNo] = result[item.saleOrderNo] || []
  297. result[item.saleOrderNo].push(item)
  298. return result
  299. }, {})
  300. // result即为所求
  301. let result = Object.values(map1)
  302. console.log(result, 'resilt')
  303. //遍历数组
  304. let saleOrderListMap = []
  305. result.forEach((saleOrderList, index) => {
  306. let map = {}
  307. let arr = []
  308. saleOrderList.forEach(e => {
  309. let mapMaterial = {}
  310. Object.keys(e).forEach(e1 => {
  311. if (this.saleOrderList.includes(e1)) {
  312. map[e1] = e[e1]
  313. }
  314. if (this.saleOrderMapList.includes(e1)) {
  315. mapMaterial[e1] = e[e1]
  316. }
  317. })
  318. arr.push(mapMaterial)
  319. })
  320. map.shipperId = 1
  321. map.userName = getCookie('loginName')
  322. map.mapList = arr
  323. saleOrderListMap.push(map)
  324. })
  325. if (saleOrderListMap.length > 0) {
  326. console.log(saleOrderListMap, 'saleOrderListMap')
  327. this.axios
  328. .post('/api/v1/ams/addTransPlanList', saleOrderListMap)
  329. .then(res => {
  330. if (res.data.code == '200') {
  331. this.getRequestUrl()
  332. this.tableData = []
  333. loading.close()
  334. this.$message.success('新增转运计划成功!')
  335. } else {
  336. console.log(res.data.data)
  337. this.$message.error(res.data.data)
  338. this.getRequestUrl()
  339. loading.close()
  340. this.tableData = []
  341. }
  342. })
  343. .catch(e => {
  344. loading.close()
  345. this.$message.error(e)
  346. this.tableData = []
  347. })
  348. } else {
  349. this.$message({
  350. type: 'error',
  351. duration: 3500,
  352. offset: 250,
  353. message:
  354. '该Excel系统未识别到订单,请仔细检查格式。(销售订单必须在第一个sheet,且必须遵循导入模板!)'
  355. })
  356. this.tableData = []
  357. loading.close()
  358. }
  359. }
  360. }
  361. }