ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<script lang="ts" setup>
import { computed, ref, unref, watch } from 'vue';
 
import { useAttrs } from '@jnpf/hooks';
import { formatToDateTime, getDateTimeUnit, mergeNumberOfExps } from '@jnpf/utils';
 
import { $t } from '@vben/locales';
 
import { useDebounceFn } from '@vueuse/core';
import { Form } from 'ant-design-vue';
import dayjs from 'dayjs';
 
defineOptions({ inheritAttrs: false, name: 'JnpfDateCalculate' });
const props = defineProps({
  componentVModel: String,
  detailed: {
    default: false,
    type: Boolean,
  },
  disabled: {
    default: false,
    type: Boolean,
  },
  expression: Array,
  format: {
    default: 'yyyy-MM-dd',
    type: String,
  },
  formData: Object,
  isStorage: {
    default: 0,
    type: Number,
  },
  rowIndex: Number,
  startRelationField: String,
  startTimeType: {
    default: 1,
    type: Number,
  },
  startTimeValue: [Number, String],
  tableVModel: String,
  value: [Number, String],
});
const emit = defineEmits(['update:value', 'change']);
const attrs: any = useAttrs({ excludeDefaultKeys: false });
const innerValue = ref<any>('');
const computeExps = ref<any>(null);
const formItemContext = Form.useInjectFormItemContext();
const startTime = ref<any>(new Date());
 
const getExp = computed(() => mergeNumberOfExps(props.expression));
const getStyle = computed(() => (Reflect.has(unref(attrs), 'style') ? unref(attrs).style : {}));
const getBindValue = computed(() => ({
  disabled: props.disabled,
  placeholder: props.isStorage ? $t('component.jnpf.calculate.storage') : $t('component.jnpf.calculate.unStorage'),
  readonly: true,
}));
 
watch(
  () => props.formData,
  (val) => {
    if (!val) return;
    if (!computeExps.value) {
      // formData更新可能比较频繁
      computeExps.value = useDebounceFn(execRPN, 100);
    }
    unref(computeExps)();
  },
  { deep: true, immediate: true },
);
watch(
  () => unref(props.value),
  (val) => {
    innerValue.value = val;
  },
  { immediate: true },
);
 
/**
 * 获取指定组件的值
 */
function getFormVal(vModel) {
  try {
    const formData: any = props.formData;
    if (vModel.includes('.')) {
      const [tableVModel, cmpVModel] = vModel.split('.');
      if (typeof props.rowIndex === 'number') {
        if (!Array.isArray(formData[tableVModel]) || formData[tableVModel].length < props.rowIndex + 1) return 0;
        return formData[tableVModel][props.rowIndex][cmpVModel] || 0;
      } else {
        if (formData[tableVModel].length === 0) return 0;
        return formData[tableVModel].reduce((sum, c) => (c[cmpVModel] ? Number(c[cmpVModel]) : 0) + sum, 0);
      }
    }
    return formData[vModel] || 0;
  } catch (error) {
    console.warn('日期公式出错, 可能包含无效的组件值', error);
    return 0;
  }
}
/**
 * 计算表达式
 */
function execRPN() {
  const formData: any = props.formData;
  const temp = unref(getExp).map((t) => (typeof t === 'object' ? getFormVal(t.__vModel__) : t));
  if (props.startTimeType == 1) startTime.value = props.startTimeValue;
  if (props.startTimeType == 2) startTime.value = getFormVal(props.startRelationField);
  innerValue.value = calcDate(startTime.value, getDateInfo(temp));
  if ((props.rowIndex as any) >= 0 && props.componentVModel && props.tableVModel) {
    if (
      formData[props.tableVModel][props.rowIndex as any] &&
      formData[props.tableVModel][props.rowIndex as any][props.componentVModel] !== innerValue.value &&
      props.isStorage
    ) {
      emit('update:value', innerValue.value);
      emit('change', innerValue.value);
      formItemContext.onFieldChange();
    }
  } else {
    if (props.isStorage) {
      emit('update:value', innerValue.value);
      formItemContext.onFieldChange();
    }
  }
}
function getDateInfo(exp) {
  let days = 0;
  let months = 0;
  let years = 0;
  let hours = 0;
  let minutes = 0;
  let seconds = 0;
  for (let i = 0; i < exp.length; i += 3) {
    const sign = exp[i];
    const value = Number.parseInt(exp[i + 1], 10);
    const unit = exp[i + 2];
    const factor = sign === '+' ? 1 : -1;
    switch (unit) {
      case 'd': {
        days += factor * value;
        break;
      }
      case 'h': {
        hours += factor * value;
        break;
      }
      case 'M': {
        months += factor * value;
        break;
      }
      case 'm': {
        minutes += factor * value;
        break;
      }
      case 's': {
        seconds += factor * value;
        break;
      }
      case 'Y': {
        years += factor * value;
        break;
      }
    }
  }
  return { days, hours, minutes, months, seconds, years };
}
function calcDate(date, change) {
  if (!date) return '';
  const newDate = new Date(date);
  newDate.setFullYear(newDate.getFullYear() + change.years);
  newDate.setMonth(newDate.getMonth() + change.months);
  newDate.setDate(newDate.getDate() + change.days);
  newDate.setHours(newDate.getHours() + change.hours);
  newDate.setMinutes(newDate.getMinutes() + change.minutes);
  newDate.setSeconds(newDate.getSeconds() + change.seconds);
  return dayjs(newDate).startOf(getDateTimeUnit(props.format)).valueOf();
}
</script>
 
<template>
  <div :style="getStyle" class="jnpf-date-calculate">
    <a-input :value="formatToDateTime(innerValue, format)" v-bind="getBindValue" v-if="!detailed" />
    <p v-else :title="formatToDateTime(innerValue, format)" class="detail-txt leading-[32px]">{{ formatToDateTime(innerValue, format) }}</p>
  </div>
</template>
<style lang="scss" scoped>
.ant-table,
.jnpf-vxe-table {
  .jnpf-date-calculate {
    .detail-txt {
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 22px !important;
      white-space: nowrap;
    }
  }
}
</style>