|
- // https://pinia.esm.dev/introduction.html
- import { defineStore } from 'pinia'
- import {GQLRequest, PinyinHelper, Session} from "../utils";
- import {gql} from "graphql-tag";
- import {ContactData} from "./contacts";
- import {ref, Ref, UnwrapRef} from "vue";
- import Taro from "@tarojs/taro";
- import dayjs from "dayjs";
-
- export interface ContactVisitRecord {
- id: number
- visitorId: number
- userId: number
- receiverName?: string
- receiverPhone?: string
- recordStatus?: number
- visitDate: string
- visitCode?: string
- }
-
- export const useContactVisitRecordStore = defineStore('contact-visit-record', () => {
-
- const visitRecord: Ref<UnwrapRef<ContactVisitRecord|null>> = ref(null)
-
- function reset() {
- visitRecord.value = null
- }
- function setContact(contactItem: ContactData) {
- contact.value = contactItem
- console.log("setContact", contact.value)
- }
- function setNewAvatar(avatar: string) {
- contactNewAvatar.value = avatar
- }
-
- const GET_VISIT_RECORD = gql`
- query ($visitorId: ID, $limit: Int ) {
- visitRecords(visitorId: $visitorId, limit: $limit) {
- id
- visitorId
- userId
- visitDate
- visitorCode
- }
- }
- `
-
- const CREATE_VISIT_RECORD = gql`
- mutation CreateVisitRecord($staffId: ID!, $visitorId: ID!, $visitDate: Date!) {
- createVisitRecord( staffId: $staffId, input: {
- visitorId: $visitorId,
- userId: $staffId,
- visitDate: $visitDate,
- } ) {
- id
- visitorId
- userId
- visitDate
- visitorCode
- }
- }
- `
-
- const UPDATE_VISIT_RECORD = gql`
- mutation UpdateVisitRecord($staffId: ID!, $visitRecordId: ID!, $visitorId: ID!, $visitDate: Date!) {
- updateVisitRecord( staffId: $staffId, input: {
- id: $visitRecordId,
- visitorId: $visitorId,
- userId: $staffId,
- visitDate: $visitDate,
- } ) {
- id
- visitorId
- userId
- visitDate
- visitorCode
- }
- }
- `
-
- function getVisitRecord(visitorId: string) {
- reset()
- const limit = 1
- return new Promise((resolve, reject) => {
- GQLRequest.query(GET_VISIT_RECORD, { visitorId, limit })
- .then(result=> {
- if (result.code == 500) {
- reject("Network Error")
- } else {
- console.log("result", result)
- if (result.data.visitRecords.length > 0) {
- const item = result.data.visitRecords[0]
- createData(item)
- }
- resolve(visitRecord.value)
- }
- })
- .catch(e => {
- reject()
- })
- })
- }
-
- function createData(item: any) {
- const todayString = dayjs().format("YYYY-MM-DD")
- const dateDiff = dayjs(todayString + " 00:00:00").diff(dayjs(item.visitDate, "YYYY-MM-DD"))
- if (dateDiff <= 0) {
- visitRecord.value = {
- id: item.id,
- visitorId: item.visitorId,
- userId: item.userId,
- receiverName: undefined,
- receiverPhone: undefined,
- recordStatus: undefined,
- visitDate: item.visitDate,
- visitCode: undefined,
- } as ContactVisitRecord
- }
- }
-
- function save(visitorId: string, date: string) {
- return new Promise((resolve, reject) => {
- if (visitRecord.value) {
-
- updateVisitRecord(visitRecord.value.id, visitorId, date).then(r => resolve(r))
- .catch(e => reject(e))
- }
- else {
- createVisitRecord(visitorId, date).then(r => resolve(r))
- .catch(e => reject(e))
- }
- })
- }
-
- function createVisitRecord(visitorId: string, visitDate: string) {
- const staffId = Session.get("staff").id
- return new Promise((resolve, reject) => {
- GQLRequest.query(CREATE_VISIT_RECORD, { staffId, visitorId, visitDate })
- .then(r=> {
- if (r.code == 500) reject("Network Error")
- else {
- console.log(r)
- createData(r.data.createVisitRecord)
- resolve(r)
- }
- })
- .catch(e=> reject(e))
- })
-
- }
-
- function updateVisitRecord(visitRecordId: string, visitorId: string, visitDate: string) {
- const staffId = Session.get("staff").id
- return new Promise((resolve, reject) => {
- GQLRequest.query(UPDATE_VISIT_RECORD, { staffId, visitRecordId, visitorId, visitDate })
- .then(r=> {
- if (r.code == 500) reject("Network Error")
- else resolve(r)
- })
- .catch(e=> reject(e))
- })
-
- }
-
- return { visitRecord, getVisitRecord , save , reset}
- })
|