workOrderReport.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <template>
  2. <u-popup :show="visible" :round="0" :closeOnClickOverlay="false" :zIndex="99999" @close="handleCancel"
  3. class="u-popup-my">
  4. <view class="popup-content">
  5. <view class="popup-header">
  6. <text class="popup-title">{{ title }}</text>
  7. <view class="close-btn" @click="handleCancel">×</view>
  8. </view>
  9. <scroll-view class="popup-body" scroll-y>
  10. <view class="page">
  11. <view class="card-a">
  12. <!-- 头部 -->
  13. <view class="a-header">
  14. <text class="a-main-title">{{ '抄表记录' }}</text>
  15. </view>
  16. <!-- 设备信息 -->
  17. <view class="card-section">
  18. <view class="section-title">🔧 设备信息</view>
  19. <view class="info-grid">
  20. <view class="info-item">
  21. <text class="info-label">表计设备名称</text>
  22. <view class="info-value" :class="{ disabled: isView }"
  23. @click="!isView && openMaterialAdd()">
  24. {{ form.substanceName || '请选择' }}
  25. </view>
  26. </view>
  27. <view class="info-item">
  28. <text class="info-label">表计设备编号</text>
  29. <view class="info-value disabled">
  30. {{ form.meterEquipmentNumber || '-' }}
  31. </view>
  32. </view>
  33. <view class="info-item">
  34. <text class="info-label">用能分类</text>
  35. <view class="info-value disabled">
  36. {{ form.energyClassificationName || '-' }}
  37. </view>
  38. </view>
  39. <view class="info-item">
  40. <text class="info-label">能源名称</text>
  41. <view class="info-value disabled">
  42. {{ form.energyTypeName || '-' }}
  43. </view>
  44. </view>
  45. <view class="info-item">
  46. <text class="info-label">表计单位</text>
  47. <view class="info-value disabled">
  48. {{ form.unit || '-' }}
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 抄表信息 -->
  54. <view class="card-section">
  55. <view class="section-title">📝 抄表信息</view>
  56. <u--form labelPosition="left" labelWidth="190rpx" :model="form" :rules="rules"
  57. ref="formRef">
  58. <!-- 抄表人 -->
  59. <u-form-item label="抄表人" prop="meterReadingName" borderBottom>
  60. <u--input v-model="form.meterReadingName" placeholder="自动带出" disabled
  61. disabledColor="#f5f5f5" border="none" inputAlign="right" />
  62. </u-form-item>
  63. <!-- 本期抄表时间 -->
  64. <u-form-item label="本期抄表时间" prop="currentMeterReadingTime" borderBottom required>
  65. <view class="picker-value" @click="!isView && showDateTimePicker()">
  66. {{
  67. form.currentMeterReadingTime
  68. ? dayjs(form.currentMeterReadingTime).format('YYYY-MM-DD HH:mm:ss')
  69. : '请选择'
  70. }}
  71. </view>
  72. <u-icon slot="right" name="arrow-right" />
  73. </u-form-item>
  74. <!-- 上次抄表数 -->
  75. <u-form-item label="上次抄表数" prop="lastMeterReadingNum" borderBottom>
  76. <u--input v-model="form.lastMeterReadingNum" type="number" placeholder="自动获取"
  77. disabled disabledColor="#f5f5f5" border="none" inputAlign="right" />
  78. </u-form-item>
  79. <!-- 本期抄表数 -->
  80. <u-form-item label="本期抄表数" prop="currentMeterReadingNum" borderBottom required>
  81. <u--input v-model="form.currentMeterReadingNum" type="number" placeholder="请输入本期抄表数"
  82. :disabled="isView" border="none" inputAlign="right" @blur="setQuantity" />
  83. </u-form-item>
  84. <!-- 本期用量 -->
  85. <u-form-item label="本期用量" prop="quantity" borderBottom required>
  86. <u--input v-model="form.quantity" type="number" placeholder="请输入本期用量"
  87. :disabled="isView" border="none" inputAlign="right" />
  88. </u-form-item>
  89. </u--form>
  90. </view>
  91. </view>
  92. </view>
  93. </scroll-view>
  94. <view class="popup-footer">
  95. <u-button type="default" @click="handleCancel">
  96. {{ isView ? '关闭' : '取消' }}
  97. </u-button>
  98. <u-button type="primary" @click="handleSubmit" v-if="!isView" :loading="loading">确定</u-button>
  99. </view>
  100. </view>
  101. <u-toast ref="uToast"></u-toast>
  102. <!-- 日期时间选择器 -->
  103. <u-datetime-picker :show="showDateTimePopup" v-model="datetimeValue" mode="datetime"
  104. @confirm="onDateTimeConfirm" @cancel="showDateTimePopup = false" />
  105. <materialDialog ref="materialAddRef" @affirm="handleChooseEquipment"></materialDialog>
  106. </u-popup>
  107. </template>
  108. <script>
  109. import {
  110. save,
  111. update,
  112. getById,
  113. queryBySubstanceId,
  114. } from '@/api/centralizedMeterReading/index.js';
  115. import dayjs from 'dayjs';
  116. import {
  117. energyConsumingUnitList
  118. } from '@/enum/dict.js';
  119. import materialDialog from '@/components/MaterialSelector/materialDialog.vue';
  120. const defForm = {
  121. id: '',
  122. substanceId: '',
  123. substanceName: '',
  124. substanceCode: '',
  125. meterEquipmentNumber: '',
  126. energyClassificationName: '',
  127. energyTypeName: '',
  128. unit: '',
  129. meterReadingName: '',
  130. meterReadingId: '',
  131. currentMeterReadingTime: '',
  132. currentMeterReadingNum: '',
  133. lastMeterReadingNum: '',
  134. quantity: '',
  135. meterReadingMethod: 1,
  136. };
  137. export default {
  138. name: 'MeterReadingDialog',
  139. components: {
  140. materialDialog,
  141. },
  142. data() {
  143. return {
  144. visible: false,
  145. loading: false,
  146. title: '新增',
  147. type: 'add',
  148. form: {
  149. ...defForm
  150. },
  151. dayjs,
  152. // 日期时间选择器
  153. showDateTimePopup: false,
  154. datetimeValue: 0,
  155. // 标记是否手动输入了用量(防止自动计算覆盖用户输入)
  156. isManualQuantity: false,
  157. // uView Form 校验规则
  158. rules: {
  159. currentMeterReadingTime: {
  160. required: true,
  161. message: '请选择本期抄表时间',
  162. trigger: ['change'],
  163. },
  164. currentMeterReadingNum: {
  165. required: true,
  166. message: '请输入本期抄表数',
  167. trigger: ['blur', 'change'],
  168. },
  169. quantity: {
  170. required: true,
  171. message: '请输入本期用量',
  172. trigger: ['blur', 'change'],
  173. },
  174. },
  175. };
  176. },
  177. computed: {
  178. isView() {
  179. return this.type === 'view';
  180. },
  181. },
  182. methods: {
  183. // ===== 打开弹窗 =====
  184. async open(row, type = 'add') {
  185. this.type = type;
  186. this.isManualQuantity = false;
  187. if (type === 'add') {
  188. this.title = '新增';
  189. this.resetForm();
  190. const currentUser = uni.getStorageSync('userInfo');
  191. this.form.meterReadingName = currentUser?.name || '';
  192. this.form.meterReadingId = currentUser?.userId || '';
  193. this.form.currentMeterReadingTime = dayjs().format(
  194. 'YYYY-MM-DD HH:mm:ss',
  195. );
  196. } else {
  197. this.title = type === 'edit' ? '编辑' : '详情';
  198. try {
  199. const res = await getById(row.id);
  200. this.form = {
  201. ...defForm,
  202. ...res
  203. };
  204. } catch (e) {
  205. console.error('获取详情失败', e);
  206. }
  207. }
  208. this.visible = true;
  209. this.$nextTick(() => {
  210. if (this.$refs.formRef) {
  211. this.$refs.formRef.setRules(this.rules);
  212. }
  213. });
  214. },
  215. // ===== 关闭弹窗 =====
  216. handleCancel() {
  217. this.visible = false;
  218. this.resetForm();
  219. this.showDateTimePopup = false;
  220. this.isManualQuantity = false;
  221. },
  222. // ===== 重置表单 =====
  223. resetForm() {
  224. this.form = {
  225. ...defForm
  226. };
  227. },
  228. // ===== 设备选择 =====
  229. openMaterialAdd() {
  230. if (this.isView) return;
  231. this.$refs.materialAddRef.open();
  232. },
  233. // ===== 选择设备回调 =====
  234. async handleChooseEquipment(data) {
  235. const row = data[0] || {};
  236. this.form.substanceId = row.id;
  237. this.form.substanceName = row?.category?.name;
  238. this.form.substanceCode = row.code;
  239. this.form.meterEquipmentNumber = row.codeNumber;
  240. this.form.energyClassificationName = row.energyClassificationName;
  241. this.form.energyClassificationCode = row.energyClassificationCode;
  242. this.form.energyTypeName = row.energyTypeName || '';
  243. this.form.energyTypeId = row.energyTypeId || '';
  244. this.form.energyTypeCode = row.energyTypeCode || '';
  245. this.form.unit = energyConsumingUnitList
  246. .find((item) => item.value == row.energyClassificationCode)
  247. ?.unit.find((item) => item.value == row.energyUnit)?.label;
  248. // 设备变更后获取上次抄表数(排除当前记录)
  249. if (row.id) {
  250. await this.fetchLastMeterReading();
  251. }
  252. },
  253. // ===== 获取上次抄表数 =====
  254. async fetchLastMeterReading() {
  255. if (!this.form.substanceId) return;
  256. try {
  257. const data = await queryBySubstanceId(
  258. this.form.substanceId,
  259. this.form.id || undefined,
  260. );
  261. if (data && data.currentMeterReadingNum !== undefined) {
  262. this.form.lastMeterReadingNum = data.currentMeterReadingNum;
  263. } else {
  264. this.form.lastMeterReadingNum = '';
  265. }
  266. // 获取上次抄表数后重新计算本期用量
  267. this.setQuantity();
  268. } catch (e) {
  269. console.error('获取上次抄表数失败', e);
  270. this.form.lastMeterReadingNum = '';
  271. }
  272. },
  273. // ===== 计算本期用量 =====
  274. setQuantity() {
  275. // // 如果用户手动输入了用量,不自动覆盖
  276. // if (this.isManualQuantity) return;
  277. const currentNum = parseFloat(this.form.currentMeterReadingNum);
  278. const lastNum = parseFloat(this.form.lastMeterReadingNum);
  279. if (!isNaN(currentNum) && !isNaN(lastNum)) {
  280. const diff = currentNum - lastNum;
  281. this.form.quantity = diff >= 0 ? diff : 0;
  282. } else if (!isNaN(currentNum) && isNaN(lastNum)) {
  283. // 只有本期数,没有上次数,用量 = 本期数
  284. this.form.quantity = currentNum;
  285. } else {
  286. this.form.quantity = '';
  287. }
  288. },
  289. // ===== 日期时间选择器 =====
  290. showDateTimePicker() {
  291. if (this.isView || this.loading) return;
  292. const val = this.form.currentMeterReadingTime;
  293. this.datetimeValue = val ? new Date(val).getTime() : Date.now();
  294. this.showDateTimePopup = true;
  295. },
  296. onDateTimeConfirm(e) {
  297. this.showDateTimePopup = false;
  298. const value = this.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM:ss');
  299. this.form.currentMeterReadingTime = value;
  300. if (this.$refs.formRef) {
  301. this.$refs.formRef.validateField('currentMeterReadingTime');
  302. }
  303. },
  304. // ===== 提交 =====
  305. async handleSubmit() {
  306. try {
  307. // if (this.$refs.formRef) {
  308. // await this.$refs.formRef.validate();
  309. // }
  310. if (!this.form.substanceId) {
  311. this.$refs.uToast.show({
  312. type: 'error',
  313. icon: false,
  314. message: '请选择表计设备',
  315. });
  316. return;
  317. }
  318. if (!this.form.currentMeterReadingNum) {
  319. this.$refs.uToast.show({
  320. type: 'error',
  321. icon: false,
  322. message: '请输入本期抄表数',
  323. });
  324. return;
  325. }
  326. if (!this.form.quantity) {
  327. this.$refs.uToast.show({
  328. type: 'error',
  329. icon: false,
  330. message: '请输入本期用量',
  331. });
  332. return;
  333. }
  334. // 时间格式补齐
  335. const formatTime = (timeStr) => {
  336. if (!timeStr) return timeStr;
  337. if (/^\d{4}-\d{2}-\d{2}$/.test(timeStr)) {
  338. return timeStr + ' 00:00:00';
  339. }
  340. return timeStr;
  341. };
  342. this.form.currentMeterReadingTime = formatTime(
  343. this.form.currentMeterReadingTime,
  344. );
  345. const api = this.type === 'edit' ? update : save;
  346. this.loading = true;
  347. await api(this.form);
  348. this.$refs.uToast.show({
  349. type: 'success',
  350. message: this.type === 'edit' ? '编辑成功' : '新增成功',
  351. });
  352. this.visible = false;
  353. this.$emit('refresh');
  354. this.resetForm();
  355. this.isManualQuantity = false;
  356. } catch (e) {
  357. if (e && e.message) {
  358. this.$refs.uToast.show({
  359. type: 'error',
  360. message: e.message
  361. });
  362. }
  363. } finally {
  364. this.loading = false;
  365. }
  366. },
  367. },
  368. };
  369. </script>
  370. <style scoped lang="scss">
  371. .popup-content {
  372. width: 100vw;
  373. height: calc(100vh - 100px);
  374. background: #fff;
  375. border-radius: 0;
  376. display: flex;
  377. flex-direction: column;
  378. background-color: #eff2f7;
  379. }
  380. .popup-header {
  381. display: flex;
  382. justify-content: space-between;
  383. align-items: center;
  384. padding: 30rpx;
  385. border-bottom: 1rpx solid #e5e5e5;
  386. background: #fff;
  387. .popup-title {
  388. font-size: 36rpx;
  389. font-weight: bold;
  390. color: #333;
  391. }
  392. .close-btn {
  393. width: 60rpx;
  394. height: 60rpx;
  395. display: flex;
  396. align-items: center;
  397. justify-content: center;
  398. font-size: 60rpx;
  399. color: #999;
  400. line-height: 1;
  401. }
  402. }
  403. .popup-body {
  404. flex: 1;
  405. overflow-y: auto;
  406. padding: 28rpx;
  407. }
  408. .page {
  409. font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, sans-serif;
  410. }
  411. .card-a {
  412. background: #ffffff;
  413. border-radius: 48rpx;
  414. box-shadow: 0 12rpx 40rpx rgba(0, 0, 0, 0.05);
  415. overflow: hidden;
  416. }
  417. .a-header {
  418. background: linear-gradient(135deg, #e8f5e9 0%, #f1f8e9 100%);
  419. padding: 40rpx 32rpx 24rpx;
  420. text-align: center;
  421. }
  422. .a-main-title {
  423. font-size: 36rpx;
  424. font-weight: 800;
  425. color: #2e7d32;
  426. }
  427. .card-section {
  428. padding: 30rpx 32rpx;
  429. border-bottom: 2rpx solid #f0f2f5;
  430. &:last-child {
  431. border-bottom: none;
  432. }
  433. }
  434. .section-title {
  435. font-size: 30rpx;
  436. font-weight: 700;
  437. color: #1f2a44;
  438. margin-bottom: 24rpx;
  439. padding-left: 16rpx;
  440. border-left: 6rpx solid #4caf50;
  441. }
  442. .info-grid {
  443. display: grid;
  444. grid-template-columns: 1fr 1fr;
  445. gap: 28rpx 20rpx;
  446. }
  447. .info-item {
  448. display: flex;
  449. flex-direction: column;
  450. gap: 8rpx;
  451. }
  452. .info-label {
  453. font-size: 26rpx;
  454. font-weight: 600;
  455. color: #6c7a91;
  456. }
  457. .info-value {
  458. font-size: 28rpx;
  459. font-weight: 500;
  460. color: #1e2a3a;
  461. padding: 16rpx 20rpx;
  462. border-radius: 24rpx;
  463. border: 2rpx solid #e9edf2;
  464. background: #fff;
  465. &.disabled {
  466. color: #999;
  467. background: #f5f5f5;
  468. }
  469. }
  470. // ===== u--form 样式 =====
  471. /deep/ .u-form-item {
  472. padding: 12rpx 0;
  473. .u-form-item__body__left__label {
  474. font-size: 26rpx !important;
  475. color: #6c7a91 !important;
  476. }
  477. .u-form-item__body__right__content {
  478. .u-input__content__field-wrapper__field {
  479. font-size: 26rpx !important;
  480. text-align: right;
  481. }
  482. }
  483. }
  484. .picker-value {
  485. font-size: 26rpx;
  486. color: #1e2a3a;
  487. padding: 8rpx 0;
  488. text-align: right;
  489. flex: 1;
  490. min-height: 44rpx;
  491. }
  492. .popup-footer {
  493. display: flex;
  494. padding: 20rpx 30rpx;
  495. border-top: 1rpx solid #e5e5e5;
  496. background: #fff;
  497. gap: 20rpx;
  498. /deep/ .u-button {
  499. flex: 1;
  500. }
  501. }
  502. </style>