verify.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { getGoodsById, getQuoteDayInfoByCode } from '@/services/bus/goods';
  2. import { getRules } from '@/services/bus/rules';
  3. import { RuleObject } from "ant-design-vue/lib/form/interface";
  4. // 验证 挂牌数量
  5. /**
  6. *
  7. * @param delistingQty 起摘数量
  8. * @param 挂牌数量对应的key
  9. * @param minivalue 最小单位
  10. * @param maxValue 最大数量(可选)
  11. * @returns
  12. */
  13. export const useVerifyListingNum = <T extends object, k extends keyof T>(formState: T, key: k, minivalue: number, maxValue?: number) => {
  14. const v_num = async (rule: RuleObject, value: number) => {
  15. if (value) {
  16. if (formState[key] && value < (formState[key] as unknown as number)) {
  17. return Promise.reject(`挂牌数量不能小于起摘数量`)
  18. }
  19. if (value < minivalue) {
  20. return Promise.reject(`挂牌数量不能小于${minivalue}`)
  21. }
  22. if (maxValue && value > maxValue) {
  23. return Promise.reject(`挂牌数量不能大于${maxValue}`)
  24. }
  25. if (value % minivalue) {
  26. return Promise.reject(`挂牌数量不是${minivalue}的倍数`)
  27. }
  28. return Promise.resolve();
  29. } else {
  30. return Promise.reject('请输入挂牌数量')
  31. }
  32. };
  33. return { v_num }
  34. }
  35. /**
  36. * 验证 挂牌基差
  37. * @param formState 表单状态
  38. * @param key 商品ID对应的key
  39. * @returns
  40. */
  41. export const useVerifyListingBasis = <T, k extends keyof T>(formState: T, key: k) => {
  42. const v_basis = async (rule: RuleObject, value: number) => {
  43. if (formState[key]) {
  44. if (value) {
  45. const goodsCode = getGoodsById(formState[key] as unknown as number)?.goodscode as string;
  46. const quote = getQuoteDayInfoByCode(goodsCode);
  47. if (quote) {
  48. const { basisdownratio, basisupratio } = getRules()[0];
  49. // 最小值 = 基差下限百分比 * 昨日收盘价 * -1
  50. const minValue = (basisdownratio * quote.presettle * -1).toFixed(2);
  51. // 最大值 = 基差上限百分比 * 昨日收盘价
  52. const maxValue = (basisupratio * quote.presettle).toFixed(2);
  53. if (value < Number(minValue)) {
  54. return Promise.reject(`基差不能小于${minValue}`);
  55. }
  56. if (value > Number(maxValue)) {
  57. return Promise.reject(`基差不能大于${maxValue}`);
  58. }
  59. return Promise.resolve();
  60. }
  61. return Promise.reject('商品合约不存在');
  62. } else {
  63. return Promise.reject('请输入基差');
  64. }
  65. } else {
  66. return Promise.reject('请选择商品合约');
  67. }
  68. };
  69. return { v_basis }
  70. }