zhengyiming
2 天以前 6dcaf1eec6c8e0326c27e8e7726e3d976e5b1026
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
import { Message, toRound } from '@12333/utils';
import * as taskCheckReceiveServices from '@12333/services/apiV2/taskCheckReceive';
import dayjs from 'dayjs';
import { EnumTaskUserSubmitCheckReceiveStatus } from '@12333/constants';
import { MaybeRef } 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 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;
      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: dayjs(checkInTime).format('YYYY-MM-DD HH:mm:ss'),
        checkOutTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      };
      return await taskCheckReceiveServices.calcTaskCheckReceive(params);
    } catch (error) {}
  }
 
  function handleCancel() {
    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,
  };
}