zhengyiming
2025-11-28 8cc48ecd9ce3baeacd1676b0cf6a80ad01338151
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
import { Message, toRound } from '@12333/utils';
import * as taskCheckReceiveServices from '@12333/services/apiV2/taskCheckReceive';
import dayjs from 'dayjs';
import { EnumTaskUserSubmitCheckReceiveStatus } from '@12333/constants';
import { computed, MaybeRef, reactive, ref, unref } from 'vue';
 
type UseSettlementAmountOptions = {
  id: MaybeRef<string>;
  date: string;
  onSuccess?: () => void;
  taskInfoUserId: MaybeRef<string>;
  timeoutServiceFee: MaybeRef<number>;
};
 
export function useSettlementAmount({
  id,
  date,
  taskInfoUserId,
  onSuccess,
  timeoutServiceFee,
}: UseSettlementAmountOptions) {
  const dialogVisible = ref(false);
 
  const preForm = reactive({
    /**是否记录过 */
    isRecord: false,
    serviceFee: 0,
    timeoutHours: 0,
    timeoutFee: 0,
    otherFee: 0,
    remark: '',
    timeoutServiceFee: 0,
  });
 
  const form = reactive({
    serviceFee: 0,
    timeoutHours: 0,
    timeoutFee: 0,
    otherFee: 0,
    remark: '',
    timeoutServiceFee: 0,
  });
 
  const settlementAmount = computed(() => sumSettlementAmount());
 
  function sumSettlementAmount() {
    return toRound(
      getFeeValue(Number(form.timeoutFee ?? 0)) +
        getFeeValue(Number(form.serviceFee ?? 0)) +
        getFeeValue(Number(form.otherFee ?? 0))
    );
  }
 
  function getFeeValue(val: number) {
    return val || 0;
  }
 
  async function openDialog(checkInTime: string) {
    let res = await calcTaskCheckReceive(checkInTime);
    if (res) {
      dialogVisible.value = true;
      if (preForm.isRecord) {
        form.serviceFee = preForm.serviceFee;
        form.timeoutHours = preForm.timeoutHours;
        form.otherFee = preForm.otherFee;
        form.timeoutFee = preForm.timeoutFee;
        form.remark = preForm.remark;
        form.timeoutServiceFee = preForm.timeoutServiceFee;
      } else {
        form.serviceFee = res.serviceFee ?? 0;
        form.timeoutHours = res.timeoutHours ?? 0;
        form.otherFee = 0;
        form.timeoutFee = 0;
        form.remark = '';
        form.timeoutServiceFee = unref(timeoutServiceFee) ?? 0;
      }
    }
  }
 
  async function calcTaskCheckReceive(checkInTime: string) {
    try {
      let params: API.CalcTaskCheckReceiveCommand = {
        taskInfoId: unref(id),
        checkInTime: !!checkInTime ? dayjs(checkInTime).format('YYYY-MM-DD HH:mm:ss') : null,
        checkOutTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      };
      return await taskCheckReceiveServices.calcTaskCheckReceive(params);
    } catch (error) {}
  }
 
  function handleCancel() {
    dialogVisible.value = false;
  }
 
  function recordForm() {
    preForm.serviceFee = form.serviceFee;
    preForm.timeoutHours = form.timeoutHours;
    preForm.otherFee = form.otherFee;
    preForm.timeoutFee = form.timeoutFee;
    preForm.remark = form.remark;
    preForm.timeoutServiceFee = form.timeoutServiceFee;
    preForm.isRecord = true;
    dialogVisible.value = false;
  }
 
  async function handleConfirm() {
    try {
      let params: API.CheckReceiveTaskCommand = {
        taskInfoUserId: unref(taskInfoUserId),
        date: dayjs(date).format('YYYY-MM-DD'),
        checkReceiveStatus: EnumTaskUserSubmitCheckReceiveStatus.Success,
        serviceFee: form.serviceFee,
        timeoutHours: form.timeoutHours,
        timeoutFee: form.timeoutFee,
        otherFee: form.otherFee,
        remark: form.remark,
        settlementAmount: settlementAmount.value,
      };
      let res = await taskCheckReceiveServices.checkReceiveTask(params);
      if (res) {
        Message.success('提交成功');
        dialogVisible.value = false;
        //   infiniteLoadingProps.value?.refetch?.();
        onSuccess?.();
      }
    } catch (error) {}
  }
 
  function onTimeoutHoursChange(event: any) {
    form.timeoutFee = Number(form.timeoutServiceFee)
      ? form.timeoutServiceFee * event.detail.value
      : 0;
  }
 
  return {
    dialogVisible,
    form,
    settlementAmount,
    handleCancel,
    openDialog,
    handleConfirm,
    onTimeoutHoursChange,
    recordForm,
  };
}