Refactoring code module 03_recruiting

This commit is contained in:
STW_TTTY\stwtt 2024-09-17 15:56:06 +07:00
parent b223c2433e
commit 87e2e3b080
36 changed files with 6139 additions and 6335 deletions

View file

@ -1,4 +1,361 @@
<!-- page:ดการรอบการสอบแขงข สรรหา -->
<script setup lang="ts">
import type { QTableProps } from "quasar";
import { onMounted, ref, watch } from "vue";
import { useQuasar, QForm } from "quasar";
import { useRouter, useRoute } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import type { RequestPeriodCompete } from "@/modules/03_recruiting/interface/request/Period";
import type {
DataOption,
UploadType,
} from "@/modules/02_organizational/interface/index/Main";
const $q = useQuasar(); // show dialog
const mixin = useCounterMixin();
const router = useRouter();
const route = useRoute();
const { date2Thai, success, dateToISO, messageError, showLoader, hideLoader } =
mixin;
const myForm = ref<QForm | null>(null); //form data input
const name = ref<string>("");
const note = ref<string>("");
const editor = ref<string>("");
const announcementExam = ref<boolean>(true);
const fee = ref<number>(0);
const round = ref<number>(1);
const yearly = ref<number>(new Date().getFullYear());
const dateRegister = ref<[Date, Date] | null>(null); //
const datePayment = ref<[Date, Date] | null>(null); //
const dateAnnouncement = ref<[Date, Date] | null>(null); //
const dateExam = ref<Date | null>(null); //
const dateAnnounce = ref<Date | null>(null); //
const positionPathOptions = ref<DataOption[]>([]);
const organizationShortName = ref<DataOption>({ id: "", name: "" });
const organizationName = ref<DataOption>({ id: "", name: "" });
const organizationNameOptions = ref<DataOption[]>([]);
const fileDocDataUpload = ref<File[]>([]);
const fileDocs = ref<UploadType[]>([]);
const fileImgDataUpload = ref<File[]>([]);
const fileImgs = ref<UploadType[]>([]);
const id = ref<string>("");
const edit = ref<boolean>(false);
/** กลับไปหน้าหลัก */
function clickBack() {
router.push({ name: "competePeriod" });
}
/** ดึงรายละเอียด */
async function fetchData() {
showLoader();
await http
.get(config.API.getPeriodById(id.value))
.then((res) => {
const data = res.data.result.periods;
const images = res.data.result.images;
const files = res.data.result.files;
id.value = data.id;
name.value = data.name;
round.value = data.order;
yearly.value = data.year;
fee.value = data.fee;
dateAnnouncement.value =
data.announcementStartDate != null && data.announcementEndDate != null
? [
new Date(data.announcementStartDate),
new Date(data.announcementEndDate),
]
: null;
dateExam.value = data.examDate != null ? new Date(data.examDate) : null;
dateRegister.value =
data.registerStartDate != null && data.registerEndDate != null
? [new Date(data.registerStartDate), new Date(data.registerEndDate)]
: null;
datePayment.value =
data.paymentStartDate != null && data.paymentEndDate != null
? [new Date(data.paymentStartDate), new Date(data.paymentEndDate)]
: null;
editor.value = data.detail;
note.value = data.note;
dateAnnounce.value =
data.announcementDate != null ? new Date(data.announcementDate) : null;
fileDocs.value = files;
fileImgs.value = images;
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
hideLoader();
});
}
/** อัพโหลดไฟล์ */
async function fileUploadDoc(files: any) {
files.forEach((file: any) => {
fileDocDataUpload.value.push(file);
});
}
/** ลบไฟล์ เอกสาร */
async function fileRemoveDoc(files: any) {
files.forEach((file: any) => {
const index = fileDocDataUpload.value.findIndex(
(x: any) => x.__key == file.__key
);
if (index > -1) {
fileDocDataUpload.value.splice(index, 1);
}
});
}
/** อัพโหลด เอกสาร */
async function uploadDocData() {
const formData = new FormData();
if (fileDocDataUpload.value.length > 0) {
fileDocDataUpload.value.forEach((file: any) => {
formData.append("", file);
});
showLoader();
await http
.put(config.API.periodRecruitDoc(id.value), formData)
.then((res) => {})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
success($q, "บันทึกข้อมูลสำเร็จ");
clickBack();
});
} else {
success($q, "บันทึกข้อมูลสำเร็จ");
clickBack();
}
}
/** อัปโหลด รูปภาพ */
async function uploadImgData() {
if (fileImgDataUpload.value.length > 0) {
const formData = new FormData();
fileImgDataUpload.value.forEach((file: any) => {
formData.append("", file);
});
showLoader();
await http
.put(config.API.periodRecruitImg(id.value), formData)
.then((res) => {})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
}
}
/** อัพโหลดไฟล์ immg */
async function fileUploadImg(files: any) {
files.forEach((file: any) => {
fileImgDataUpload.value.push(file);
});
}
/** ฟังชั่น ลบ รูป */
async function fileRemoveImg(files: any) {
files.forEach((file: any) => {
const index = fileImgDataUpload.value.findIndex(
(x: any) => x.__key == file.__key
);
if (index > -1) {
fileImgDataUpload.value.splice(index, 1);
}
});
}
/**
* ดาวหโหลดไฟล
* @param path อย url
*/
async function downloadData(path: string) {
window.open(path);
}
/**
* พเดต
* @param e
*/
async function updateYear(e: number) {
yearly.value = e;
}
/** ฟังชั่น add edit */
async function checkSave() {
if (myForm.value !== null) {
myForm.value.validate().then(async (success) => {
if (success) {
if (edit.value) {
await editData(id.value);
} else {
await addData();
}
}
});
}
}
/** body ที่บันทึก */
function sendData() {
const valueData: RequestPeriodCompete = {
announcementEndDate:
dateAnnouncement.value !== null
? dateToISO(dateAnnouncement.value[1])
: null,
announcementStartDate:
dateAnnouncement.value !== null
? dateToISO(dateAnnouncement.value[0])
: null,
examDate: dateExam.value !== null ? dateToISO(dateExam.value) : null,
detail: editor.value,
fee: fee.value,
id: "",
name: name.value,
note: note.value,
paymentEndDate:
datePayment.value !== null ? dateToISO(datePayment.value[1]) : null,
paymentStartDate:
datePayment.value !== null ? dateToISO(datePayment.value[0]) : null,
registerEndDate:
dateRegister.value !== null ? dateToISO(dateRegister.value[1]) : null,
registerStartDate:
dateRegister.value !== null ? dateToISO(dateRegister.value[0]) : null,
order: round.value,
year: yearly.value,
announcementDate:
dateAnnounce.value !== null ? dateToISO(dateAnnounce.value) : null,
};
return valueData;
}
/**
* ลบ เอกสารประกอบ
* @param docId id เอกสาร
*/
async function deleteDocData(docId: string) {
showLoader();
await http
.delete(config.API.periodDeleteDoc(docId))
.then(async () => {
await fetchData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
}
/**
* ลบรปประกอบ
* @param docId id ของร
*/
async function deleteImgData(docId: string) {
showLoader();
await http
.delete(config.API.periodDeleteImg(docId))
.then(async () => {
await fetchData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
}
/** เพิ่ม รอบสอบแข่งขัน */
async function addData() {
showLoader();
await http
.post(config.API.savePeriod, sendData())
.then(async (res) => {
const data = res.data.result;
id.value = data.id;
await uploadImgData();
await uploadDocData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
}
/**
* นท แกไขขอม
* @param id
*/
async function editData(id: string) {
showLoader();
await http
.put(config.API.editPeriod(id), sendData())
.then(async () => {
await uploadImgData();
await uploadDocData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
}
/**
* แปลงชวงวนทา2คาเปนวนเดยวกนจะโชววนเดยวแตาไมเทากนจะแสดงเปนชวง
* @param val วงวนท
*/
function dateThaiRange(val: [Date, Date]) {
if (val === null) {
return "";
} else if (date2Thai(val[0], true) === date2Thai(val[1], true)) {
return `${date2Thai(val[0], true)}`;
} else {
return `${date2Thai(val[0], true)} - ${date2Thai(val[1], true)}`;
}
}
watch(organizationShortName, (count: DataOption, prevCount: DataOption) => {
organizationNameOptions.value = [];
});
watch(organizationName, (count: DataOption, prevCount: DataOption) => {
positionPathOptions.value = [];
});
onMounted(async () => {
hideLoader();
if (route.params.id != undefined) {
edit.value = true;
id.value = route.params.id.toString();
await fetchData();
} else {
edit.value = false;
}
});
</script>
<template>
<div class="toptitle text-dark col-12 row items-center">
<q-btn
@ -36,6 +393,7 @@
label="รอบการสอบ(ครั้ง)"
dense
lazy-rules
hide-bottom-space
:rules="[(val) => val > 0 || `${'กรุณากรอกรอบการสอบให้ถูกต้อง'}`]"
></q-input>
</div>
@ -56,6 +414,7 @@
<template #trigger>
<q-input
dense
hide-bottom-space
outlined
:rules="[(val) => !!val || `${'กรุณาเลือกปีงบประมาณ'}`]"
:model-value="yearly + 543"
@ -77,6 +436,7 @@
<q-input
outlined
v-model="fee"
hide-bottom-space
type="number"
label="ค่าธรรมเนียม"
lazy-rules
@ -655,405 +1015,5 @@
</q-form>
</q-card>
</template>
<script setup lang="ts">
import type { QTableProps } from "quasar";
import { onMounted, ref, watch } from "vue";
import { useQuasar, QForm } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import { useRouter, useRoute } from "vue-router";
import type { RequestPeriodCompete } from "@/modules/03_recruiting/interface/request/Period";
import type {
DataOption,
UploadType,
} from "@/modules/02_organizational/interface/index/Main";
import http from "@/plugins/http";
import config from "@/app.config";
const $q = useQuasar(); // show dialog
const mixin = useCounterMixin();
const router = useRouter();
const route = useRoute();
const { date2Thai, success, dateToISO, notifyError, showLoader, hideLoader } =
mixin;
const myForm = ref<QForm | null>(null); //form data input
const name = ref<string>("");
const note = ref<string>("");
const editor = ref<string>("");
const announcementExam = ref<boolean>(true);
const fee = ref<number>(0);
const round = ref<number>(1);
const yearly = ref<number>(new Date().getFullYear());
const dateRegister = ref<[Date, Date] | null>(null); //
const datePayment = ref<[Date, Date] | null>(null); //
const dateAnnouncement = ref<[Date, Date] | null>(null); //
const dateExam = ref<Date | null>(null); //
const dateAnnounce = ref<Date | null>(null); //
const positionPathOptions = ref<DataOption[]>([]);
const organizationShortName = ref<DataOption>({ id: "", name: "" });
const organizationName = ref<DataOption>({ id: "", name: "" });
const organizationNameOptions = ref<DataOption[]>([]);
const { messageError } = mixin;
const fileDocDataUpload = ref<File[]>([]);
const fileDocs = ref<UploadType[]>([]);
const fileImgDataUpload = ref<File[]>([]);
const fileImgs = ref<UploadType[]>([]);
const id = ref<string>("");
const pay = ref<string>("");
const edit = ref<boolean>(false);
const columnsPayment = ref<QTableProps["columns"]>([
{
name: "accountNumber",
align: "left",
label: "เลขบัญชี",
sortable: true,
field: "accountNumber",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, {
numeric: true,
sensitivity: "base",
}),
},
{
name: "bankName",
align: "left",
label: "ธนาคาร",
sortable: true,
field: "bankName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, {
numeric: true,
sensitivity: "base",
}),
},
{
name: "accountName",
align: "left",
label: "ชื่อบัญชี",
sortable: true,
field: "accountName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, {
numeric: true,
sensitivity: "base",
}),
},
]);
const visibleColumnsPosition = ref<String[]>(["position", "type"]);
const columnsPosition = ref<QTableProps["columns"]>([
{
name: "position",
align: "left",
label: "ตำแหน่ง",
sortable: true,
field: "position",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, {
numeric: true,
sensitivity: "base",
}),
},
{
name: "type",
align: "left",
label: "ประเภทแบบฟอร์ม",
sortable: true,
field: "type",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, {
numeric: true,
sensitivity: "base",
}),
},
]);
watch(organizationShortName, (count: DataOption, prevCount: DataOption) => {
organizationNameOptions.value = [];
});
watch(organizationName, (count: DataOption, prevCount: DataOption) => {
positionPathOptions.value = [];
});
onMounted(async () => {
hideLoader();
if (route.params.id != undefined) {
edit.value = true;
id.value = route.params.id.toString();
await fetchData();
} else {
edit.value = false;
}
});
const clickBack = () => {
router.push({ name: "competePeriod" });
};
const fetchData = async () => {
showLoader();
await http
.get(config.API.getPeriodById(id.value))
.then((res) => {
const data = res.data.result.periods;
const images = res.data.result.images;
const files = res.data.result.files;
id.value = data.id;
name.value = data.name;
round.value = data.order;
yearly.value = data.year;
fee.value = data.fee;
dateAnnouncement.value =
data.announcementStartDate != null && data.announcementEndDate != null
? [
new Date(data.announcementStartDate),
new Date(data.announcementEndDate),
]
: null;
dateExam.value = data.examDate != null ? new Date(data.examDate) : null;
dateRegister.value =
data.registerStartDate != null && data.registerEndDate != null
? [new Date(data.registerStartDate), new Date(data.registerEndDate)]
: null;
datePayment.value =
data.paymentStartDate != null && data.paymentEndDate != null
? [new Date(data.paymentStartDate), new Date(data.paymentEndDate)]
: null;
editor.value = data.detail;
note.value = data.note;
dateAnnounce.value =
data.announcementDate != null ? new Date(data.announcementDate) : null;
fileDocs.value = files;
fileImgs.value = images;
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
hideLoader();
});
};
const fileUploadDoc = async (files: any) => {
files.forEach((file: any) => {
fileDocDataUpload.value.push(file);
});
};
const fileRemoveDoc = async (files: any) => {
files.forEach((file: any) => {
const index = fileDocDataUpload.value.findIndex(
(x: any) => x.__key == file.__key
);
if (index > -1) {
fileDocDataUpload.value.splice(index, 1);
}
});
};
const uploadDocData = async () => {
const formData = new FormData();
if (fileDocDataUpload.value.length > 0) {
fileDocDataUpload.value.forEach((file: any) => {
formData.append("", file);
});
showLoader();
await http
.put(config.API.periodRecruitDoc(id.value), formData)
.then((res) => {})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
success($q, "บันทึกข้อมูลสำเร็จ");
clickBack();
});
} else {
success($q, "บันทึกข้อมูลสำเร็จ");
clickBack();
}
};
const uploadImgData = async () => {
if (fileImgDataUpload.value.length > 0) {
const formData = new FormData();
fileImgDataUpload.value.forEach((file: any) => {
formData.append("", file);
});
showLoader();
await http
.put(config.API.periodRecruitImg(id.value), formData)
.then((res) => {})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
}
};
const fileUploadImg = async (files: any) => {
files.forEach((file: any) => {
fileImgDataUpload.value.push(file);
});
};
const fileRemoveImg = async (files: any) => {
files.forEach((file: any) => {
const index = fileImgDataUpload.value.findIndex(
(x: any) => x.__key == file.__key
);
if (index > -1) {
fileImgDataUpload.value.splice(index, 1);
}
});
};
const downloadData = async (path: string) => {
window.open(path);
};
const updateYear = async (e: number) => {
yearly.value = e;
};
const checkSave = async () => {
if (myForm.value !== null) {
myForm.value.validate().then(async (success) => {
if (success) {
if (edit.value) {
await editData(id.value);
} else {
await addData();
}
}
});
}
};
const sendData = () => {
const valueData: RequestPeriodCompete = {
announcementEndDate:
dateAnnouncement.value !== null
? dateToISO(dateAnnouncement.value[1])
: null,
announcementStartDate:
dateAnnouncement.value !== null
? dateToISO(dateAnnouncement.value[0])
: null,
examDate: dateExam.value !== null ? dateToISO(dateExam.value) : null,
detail: editor.value,
fee: fee.value,
id: "",
name: name.value,
note: note.value,
paymentEndDate:
datePayment.value !== null ? dateToISO(datePayment.value[1]) : null,
paymentStartDate:
datePayment.value !== null ? dateToISO(datePayment.value[0]) : null,
registerEndDate:
dateRegister.value !== null ? dateToISO(dateRegister.value[1]) : null,
registerStartDate:
dateRegister.value !== null ? dateToISO(dateRegister.value[0]) : null,
order: round.value,
year: yearly.value,
announcementDate:
dateAnnounce.value !== null ? dateToISO(dateAnnounce.value) : null,
};
return valueData;
};
const deleteDocData = async (docId: string) => {
showLoader();
await http
.delete(config.API.periodDeleteDoc(docId))
.then(async () => {
await fetchData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
};
const deleteImgData = async (docId: string) => {
showLoader();
await http
.delete(config.API.periodDeleteImg(docId))
.then(async () => {
await fetchData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
};
const addData = async () => {
showLoader();
await http
.post(config.API.savePeriod, sendData())
.then(async (res) => {
const data = res.data.result;
id.value = data.id;
await uploadImgData();
await uploadDocData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
};
const editData = async (id: string) => {
showLoader();
await http
.put(config.API.editPeriod(id), sendData())
.then(async () => {
await uploadImgData();
await uploadDocData();
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
hideLoader();
});
};
/**
* แปลงชวงวนทา2คาเปนวนเดยวกนจะโชววนเดยวแตาไมเทากนจะแสดงเปนชวง
* @param val วงวนท
*/
const dateThaiRange = (val: [Date, Date]) => {
if (val === null) {
return "";
} else if (date2Thai(val[0], true) === date2Thai(val[1], true)) {
return `${date2Thai(val[0], true)}`;
} else {
return `${date2Thai(val[0], true)} - ${date2Thai(val[1], true)}`;
}
};
</script>
<style></style>