index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
  4. <el-form-item label="日期" prop="dataDate">
  5. <el-date-picker clearable
  6. v-model="queryParams.dataDate"
  7. type="date"
  8. value-format="YYYY-MM-DD"
  9. placeholder="请选择日期" @change="handleQuery">
  10. </el-date-picker>
  11. </el-form-item>
  12. <el-form-item label="机台号" prop="deviceId">
  13. <el-select
  14. v-model="queryParams.deviceId"
  15. placeholder="请选择机台号"
  16. filterable
  17. style="width: 80px"
  18. @change="handleQuery"
  19. >
  20. <el-option
  21. v-for="item in deviceOptions"
  22. :key="item.value"
  23. :label="item.label"
  24. :value="item.value"
  25. />
  26. </el-select>
  27. </el-form-item>
  28. <el-form-item label="时段" prop="remark">
  29. <el-select v-model="queryParams.remark" placeholder="选择时段" style="width: 180px" @change="handleQuery">
  30. <el-option
  31. v-for="dict in in_out_time"
  32. :key="dict.value"
  33. :label="dict.label"
  34. :value="dict.value"
  35. ></el-option>
  36. </el-select>
  37. </el-form-item>
  38. <el-form-item>
  39. <el-button type="success" @click="viewTrend">查看近5日织造米数</el-button>
  40. </el-form-item>
  41. </el-form>
  42. <!-- 图表弹窗 -->
  43. <el-dialog title="近5日织造米数趋势图" v-model="chartDialogVisible" width="800px" append-to-body>
  44. <div ref="chartRef" style="width: 100%; height: 400px;"></div>
  45. <template #footer>
  46. <div class="dialog-footer">
  47. <el-button @click="closeChartDialog">关闭</el-button>
  48. </div>
  49. </template>
  50. </el-dialog>
  51. <el-table v-loading="loading" :data="calcSpecList" show-summary :summary-method="getSummaries"
  52. @selection-change="handleSelectionChange">
  53. <el-table-column label="日期" align="center" prop="dataDate" width="180">
  54. <template #default="scope">
  55. <span>{{ parseTime(scope.row.dataDate, '{y}-{m}-{d}') }}</span>
  56. </template>
  57. </el-table-column>
  58. <el-table-column label="小时" align="center" prop="hour"/>
  59. <el-table-column label="机台号" align="center" prop="deviceId"/>
  60. <el-table-column label="织造米数" align="center" prop="length"/>
  61. <el-table-column label="规格" align="center" prop="spec"/>
  62. <el-table-column label="毛高" align="center" prop="height"/>
  63. <el-table-column label="米克重" align="center" prop="mick"/>
  64. <el-table-column label="密度" align="center" prop="density"/>
  65. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  66. <template #default="scope">
  67. <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">核对</el-button>
  68. </template>
  69. </el-table-column>
  70. </el-table>
  71. <!-- 添加或修改按配方1小时统计数据对话框 -->
  72. <el-dialog :title="title" v-model="open" width="500px" append-to-body>
  73. <el-form ref="calcSpecRef" :model="form" :rules="rules" label-width="80px">
  74. <el-form-item label="日期" prop="dataDate">
  75. <el-date-picker disabled
  76. v-model="form.dataDate"
  77. type="date"
  78. value-format="YYYY-MM-DD"
  79. placeholder="请选择日期">
  80. </el-date-picker>
  81. </el-form-item>
  82. <el-form-item label="小时" prop="hour">
  83. <el-input v-model="form.hour" disabled />
  84. </el-form-item>
  85. <el-form-item label="机台号" prop="deviceId">
  86. <el-input v-model="form.deviceId" disabled />
  87. </el-form-item>
  88. <el-form-item label="织造米数" prop="length">
  89. <el-input-number v-model="form.length" placeholder="请输入织造米数" style="width: 180px"/>
  90. </el-form-item>
  91. <el-form-item label="密度" prop="density">
  92. <el-input-number v-model="form.density" placeholder="请输入密度" style="width: 180px"/>
  93. </el-form-item>
  94. <el-form-item label="规格" prop="spec">
  95. <el-input v-model="form.spec" disabled/>
  96. </el-form-item>
  97. <el-form-item label="毛高" prop="height">
  98. <el-input v-model="form.height" disabled/>
  99. </el-form-item>
  100. <el-form-item label="米克重" prop="mick">
  101. <el-input v-model="form.mick" disabled />
  102. </el-form-item>
  103. </el-form>
  104. <template #footer>
  105. <div class="dialog-footer">
  106. <el-button type="primary" @click="submitForm">确 定</el-button>
  107. <el-button @click="cancel">取 消</el-button>
  108. </div>
  109. </template>
  110. </el-dialog>
  111. </div>
  112. </template>
  113. <script setup name="CalcSpec">
  114. import {addCalcSpec, delCalcSpec, getCalcSpec, listCalcSpec, updateCalcSpec,trend} from "@/api/calc/calcSpec";
  115. import {listDevice} from "@/api/biz/device"
  116. import * as echarts from 'echarts';
  117. import { nextTick } from 'vue';
  118. const {proxy} = getCurrentInstance();
  119. const {in_out_time} = proxy.useDict('in_out_time');
  120. const calcSpecList = ref([]);
  121. const open = ref(false);
  122. const loading = ref(true);
  123. const showSearch = ref(true);
  124. const ids = ref([]);
  125. const single = ref(true);
  126. const multiple = ref(true);
  127. const total = ref(0);
  128. const title = ref("");
  129. const deviceOptions = ref([]);
  130. const chartRef = ref(null);
  131. let chartInstance = null;
  132. const chartDialogVisible = ref(false);
  133. const trendData = ref({});
  134. const data = reactive({
  135. form: {},
  136. queryParams: {
  137. pageNum: 1,
  138. pageSize: 100,
  139. dataDate: new Date(new Date().getTime() - 31 * 60 * 60 * 1000).Format('yyyy-MM-dd'),
  140. hour: null,
  141. deviceId: null,
  142. length: null,
  143. spec: null,
  144. height: null,
  145. mick: null,
  146. density: null,
  147. price: null,
  148. remark: null
  149. },
  150. rules: {
  151. density: [
  152. { required: true, message: "密度不能为空", trigger: "blur" },
  153. { type: "number", message: "密度必须为数字值", trigger: "blur" },
  154. {
  155. validator: (rule, value, callback) => {
  156. if (value <= 0) {
  157. callback(new Error('密度必须大于0'));
  158. } else {
  159. callback();
  160. }
  161. },
  162. trigger: "blur"
  163. }
  164. ],
  165. length: [
  166. { required: true, message: "织造米数不能为空", trigger: "blur" },
  167. { type: "number", message: "织造米数必须为数字值", trigger: "blur" },
  168. {
  169. validator: (rule, value, callback) => {
  170. if (value <= 0) {
  171. callback(new Error('织造米数必须大于0'));
  172. } else {
  173. callback();
  174. }
  175. },
  176. trigger: "blur"
  177. }
  178. ]
  179. }
  180. });
  181. const {queryParams, form, rules} = toRefs(data);
  182. function getDevice() {
  183. listDevice({online: 1, pageNum: 1, pageSize: 1000}).then(response => {
  184. deviceOptions.value = response.rows.map(item => ({
  185. value: item.deviceId,
  186. label: item.deviceId
  187. }));
  188. }).catch(error => {
  189. console.error('获取机台号列表失败:', error);
  190. proxy.$modal.notifyError('获取机台号列表失败');
  191. });
  192. }
  193. /** 查询按配方1小时统计数据列表 */
  194. function getList() {
  195. loading.value = true;
  196. if (!queryParams.value.deviceId) {
  197. queryParams.value.deviceId = 1;
  198. }
  199. if (!queryParams.value.remark) {
  200. queryParams.value.remark = '7-19';
  201. }
  202. listCalcSpec(queryParams.value).then(response => {
  203. calcSpecList.value = response.rows;
  204. total.value = response.total;
  205. loading.value = false;
  206. });
  207. }
  208. /**查询趋势*/
  209. function viewTrend(){
  210. trend(queryParams.value).then(response => {
  211. // 保存数据并显示图表弹窗
  212. trendData.value = response;
  213. chartDialogVisible.value = true;
  214. // 等待DOM更新后初始化图表
  215. nextTick(() => {
  216. initChart(response);
  217. });
  218. });
  219. }
  220. // 关闭图表弹窗
  221. function closeChartDialog() {
  222. chartDialogVisible.value = false;
  223. // 销毁图表实例
  224. if (chartInstance) {
  225. chartInstance.dispose();
  226. chartInstance = null;
  227. }
  228. }
  229. // 初始化图表
  230. function initChart(data) {
  231. // 销毁之前的图表实例
  232. if (chartInstance) {
  233. chartInstance.dispose();
  234. }
  235. // 初始化新的图表实例
  236. chartInstance = echarts.init(chartRef.value);
  237. console.log('传入initChart的数据:', data); // 调试信息
  238. // 处理数据 - 修正数据访问路径
  239. let responseData = data.data.data;
  240. const timestamps = responseData.timestamps || [];
  241. const values = responseData.values || [];
  242. console.log('提取的原始数据:', { timestamps, values }); // 调试信息
  243. // 将时间戳转换为日期字符串 (mm-dd hh:mm格式)
  244. const dates = timestamps.map(timestamp => {
  245. const date = new Date(timestamp);
  246. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  247. const day = date.getDate().toString().padStart(2, '0');
  248. const hours = date.getHours().toString().padStart(2, '0');
  249. const minutes = date.getMinutes().toString().padStart(2, '0');
  250. return `${month}-${day} ${hours}:${minutes}`;
  251. });
  252. // 提取数值(处理嵌套数组)
  253. const chartValues = [];
  254. const validDates = [];
  255. values.forEach((item, index) => {
  256. // 检查是否有足够的数据点
  257. if (Array.isArray(item) && item.length > 0) {
  258. // 获取第一个元素并尝试转为数字
  259. const rawValue = item[0];
  260. const numericValue = parseFloat(rawValue);
  261. // 检查转换后的值是否有效
  262. if (!isNaN(numericValue)) {
  263. chartValues.push(numericValue);
  264. validDates.push(dates[index]);
  265. } else if (typeof rawValue === 'number' && !isNaN(rawValue)) {
  266. // 如果本身就是数字
  267. chartValues.push(rawValue);
  268. validDates.push(dates[index]);
  269. }
  270. }
  271. });
  272. console.log('处理后的图表数据:', { validDates, chartValues }); // 调试信息
  273. // 如果没有有效数据,显示提示
  274. if (validDates.length === 0 && chartValues.length === 0) {
  275. chartInstance.setOption({
  276. title: {
  277. text: '暂无有效数据',
  278. left: 'center',
  279. top: 'center'
  280. }
  281. });
  282. return;
  283. }
  284. // 计算默认显示的时间范围(根据选择的日期和时段)
  285. let startIndex = 0;
  286. let endIndex = validDates.length - 1;
  287. if (queryParams.value.dataDate && queryParams.value.remark) {
  288. // 解析时段,例如 "7-19" 或 "23-7"
  289. const timeRange = queryParams.value.remark.split('-');
  290. if (timeRange.length === 2) {
  291. const startTime = parseInt(timeRange[0]);
  292. const endTime = parseInt(timeRange[1]);
  293. // 构造选择的日期
  294. const selectedDate = new Date(queryParams.value.dataDate);
  295. const month = (selectedDate.getMonth() + 1).toString().padStart(2, '0');
  296. const day = selectedDate.getDate().toString().padStart(2, '0');
  297. // 判断是否是跨天时段(如23-7)
  298. if (startTime > endTime) {
  299. // 跨天时段处理,例如 23-7 表示当天23点到次日7点
  300. // 查找当天23点开始的数据
  301. const startDateTime = `${month}-${day} ${startTime.toString().padStart(2, '0')}:00`;
  302. const startTimeIndex = validDates.findIndex(date => date.startsWith(startDateTime));
  303. if (startTimeIndex !== -1) {
  304. startIndex = startTimeIndex;
  305. }
  306. // 查找次日7点的数据
  307. const nextDay = new Date(selectedDate);
  308. nextDay.setDate(nextDay.getDate() + 1);
  309. const nextMonth = (nextDay.getMonth() + 1).toString().padStart(2, '0');
  310. const nextDayStr = nextDay.getDate().toString().padStart(2, '0');
  311. const endDateTime = `${nextMonth}-${nextDayStr} ${endTime.toString().padStart(2, '0')}:00`;
  312. const endTimeIndex = validDates.findIndex(date => date.startsWith(endDateTime));
  313. if (endTimeIndex !== -1) {
  314. endIndex = endTimeIndex;
  315. }
  316. } else {
  317. // 正常时段处理,例如 7-19 表示当天7点到19点
  318. // 查找开始时间索引
  319. const startDateTime = `${month}-${day} ${startTime.toString().padStart(2, '0')}:00`;
  320. const startTimeIndex = validDates.findIndex(date => date.startsWith(startDateTime));
  321. if (startTimeIndex !== -1) {
  322. startIndex = startTimeIndex;
  323. }
  324. // 查找结束时间索引
  325. const endDateTime = `${month}-${day} ${endTime.toString().padStart(2, '0')}:00`;
  326. const endTimeIndex = validDates.findIndex(date => date.startsWith(endDateTime));
  327. if (endTimeIndex !== -1) {
  328. endIndex = endTimeIndex;
  329. } else {
  330. // 如果找不到确切的结束时间,尝试查找最接近的时间点
  331. for (let i = validDates.length - 1; i >= 0; i--) {
  332. if (validDates[i].startsWith(`${month}-${day}`)) {
  333. endIndex = i;
  334. break;
  335. }
  336. }
  337. }
  338. }
  339. // 确保索引范围合理
  340. if (startIndex >= endIndex) {
  341. endIndex = Math.min(startIndex + 10, validDates.length - 1); // 默认显示10个点
  342. }
  343. }
  344. }
  345. // 配置图表选项 - 遵循项目规范
  346. const option = {
  347. title: {
  348. text: '近5日织造米数趋势图',
  349. left: 'center'
  350. },
  351. tooltip: {
  352. trigger: 'axis',
  353. formatter: function (params) {
  354. const date = params[0].axisValue;
  355. const value = params[0].data;
  356. return `${date}<br/>织造米数: ${value}`;
  357. }
  358. },
  359. dataZoom: [
  360. {
  361. type: 'inside',
  362. startValue: startIndex,
  363. endValue: endIndex
  364. },
  365. {
  366. type: 'slider',
  367. startValue: startIndex,
  368. endValue: endIndex,
  369. bottom: 10
  370. }
  371. ],
  372. xAxis: {
  373. type: 'category',
  374. data: validDates.length > 0 ? validDates : dates, // 如果有有效日期则使用,否则使用全部日期
  375. name: '时间',
  376. axisLabel: {
  377. formatter: '{value}',
  378. rotate: 45 // 旋转标签以避免重叠
  379. },
  380. splitLine: {
  381. show: false // 遵循规范:不显示网格线
  382. }
  383. },
  384. yAxis: {
  385. type: 'value',
  386. name: '织造米数',
  387. splitLine: {
  388. show: false // 遵循规范:不显示网格线
  389. }
  390. },
  391. series: [{
  392. data: chartValues.length > 0 ? chartValues : values, // 如果有处理后的数据则使用,否则使用原始数据
  393. type: 'line',
  394. smooth: true,
  395. showSymbol: false, // 遵循规范:不显示数据点小圆圈
  396. itemStyle: {
  397. color: '#409EFF'
  398. },
  399. markPoint: {
  400. data: [
  401. {type: 'max', name: '最大值', itemStyle: {color: '#F56C6C'}},
  402. {type: 'min', name: '最小值', itemStyle: {color: '#67C23A'}}
  403. ]
  404. },
  405. markLine: {
  406. data: [
  407. {type: 'average', name: '平均值'}
  408. ]
  409. }
  410. }]
  411. };
  412. // 设置图表配置
  413. chartInstance.setOption(option);
  414. // 监听窗口大小变化,自适应图表
  415. window.addEventListener('resize', () => {
  416. chartInstance.resize();
  417. });
  418. }
  419. //自定义方法
  420. function getSummaries(param) {
  421. const {columns, data} = param;
  422. const sums = {};
  423. columns.forEach((column, index) => {
  424. if (index === 0) {
  425. sums[index] = '合计';
  426. return;
  427. }
  428. if (index != 3) {
  429. sums[index] = '';
  430. return;
  431. }
  432. const values = data.map(item => Number(item[column.property]));
  433. if (!values.every(value => isNaN(value))) {
  434. sums[index] = values.reduce((prev, curr) => {
  435. const value = Number(curr);
  436. if (!isNaN(value)) {
  437. return prev + curr;
  438. } else {
  439. return prev;
  440. }
  441. }, 0);
  442. sums[index] = sums[index].toFixed(2);
  443. } else {
  444. sums[index] = 'N/A';
  445. }
  446. });
  447. return sums;
  448. }
  449. // 取消按钮
  450. function cancel() {
  451. open.value = false;
  452. reset();
  453. }
  454. // 表单重置
  455. function reset() {
  456. form.value = {
  457. id: null,
  458. dataDate: null,
  459. hour: null,
  460. deviceId: null,
  461. length: null,
  462. spec: null,
  463. height: null,
  464. mick: null,
  465. density: null,
  466. price: null,
  467. remark: null
  468. };
  469. proxy.resetForm("calcSpecRef");
  470. }
  471. /** 搜索按钮操作 */
  472. function handleQuery() {
  473. queryParams.value.pageNum = 1;
  474. getList();
  475. }
  476. /** 重置按钮操作 */
  477. function resetQuery() {
  478. proxy.resetForm("queryRef");
  479. getDevice();
  480. handleQuery();
  481. }
  482. // 多选框选中数据
  483. function handleSelectionChange(selection) {
  484. ids.value = selection.map(item => item.id);
  485. single.value = selection.length != 1;
  486. multiple.value = !selection.length;
  487. }
  488. /** 新增按钮操作 */
  489. function handleAdd() {
  490. reset();
  491. open.value = true;
  492. title.value = "添加按配方1小时统计数据";
  493. }
  494. /** 修改按钮操作 */
  495. function handleUpdate(row) {
  496. reset();
  497. const _id = row.id || ids.value
  498. getCalcSpec(_id).then(response => {
  499. form.value = response.data;
  500. open.value = true;
  501. title.value = "产量数据核对";
  502. });
  503. }
  504. /** 提交按钮 */
  505. function submitForm() {
  506. proxy.$refs["calcSpecRef"].validate(valid => {
  507. if (valid) {
  508. if (form.value.id != null) {
  509. updateCalcSpec(form.value).then(response => {
  510. proxy.$modal.msgSuccess("修改成功");
  511. open.value = false;
  512. getList();
  513. });
  514. } else {
  515. addCalcSpec(form.value).then(response => {
  516. proxy.$modal.msgSuccess("新增成功");
  517. open.value = false;
  518. getList();
  519. });
  520. }
  521. }
  522. });
  523. }
  524. /** 删除按钮操作 */
  525. function handleDelete(row) {
  526. const _ids = row.id || ids.value;
  527. proxy.$modal.confirm('是否确认删除按配方1小时统计数据编号为"' + _ids + '"的数据项?').then(function () {
  528. return delCalcSpec(_ids);
  529. }).then(() => {
  530. getList();
  531. proxy.$modal.msgSuccess("删除成功");
  532. }).catch(() => {
  533. });
  534. }
  535. /** 导出按钮操作 */
  536. function handleExport() {
  537. proxy.download('calc/calcSpec/export', {
  538. ...queryParams.value
  539. }, `calcSpec_${new Date().getTime()}.xlsx`)
  540. }
  541. getDevice();
  542. getList();
  543. </script>