points.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { checkOut,calculateDiscount,ordersAndPrepay,currentUnlicensedPlate,unlicensedCarCheckIn,unlicensedCarCheckout } from '@/api/parking';
  2. import { setSelected } from './utils'
  3. export default {
  4. // 积分相关计算(仅计算用户可使用的积分)
  5. pointsRule( {commit,dispatch,state},checkOutResponse ) {
  6. const {discountInfo,parkingRule,parkInfo} = checkOutResponse
  7. const {
  8. points,
  9. usingTotalDiscount, // 当前车辆的已使用优惠的总上限
  10. } = discountInfo;
  11. const {
  12. parkMallCode
  13. } = parkInfo
  14. const {
  15. maxPointsTime = 0, // 最大积分
  16. enablePoints,
  17. availableDiscountFee,
  18. maxOneDayDiscountFee, // 每日最大优惠金额(深圳车场)
  19. remainConsumeTime, // 当前订单剩余可使用的优惠
  20. } = parkingRule;
  21. if ( !points ) return commit('setIntegralDesc','暂无可用积分'); // 积分优惠处的描述
  22. const [
  23. {
  24. pointsPerHour,
  25. available, // 用户可用的积分(当前车辆在) available是用户选择抵扣N小时后剩余的可用积分
  26. maxDiscountFee, // 单次上限(元)
  27. unitAmount, // 兑换值(元)
  28. unlimitUsePoints= "LIMIT", // 前海积分最大上限
  29. unitHour, // 兑换值(小时)
  30. pointsPerUnit = 0, // 500积分对应的价值
  31. discountFee,
  32. label,
  33. },
  34. ] = points;
  35. commit('setAvailableDiscountFee',availableDiscountFee);
  36. dispatch('maxPointsTimeMath',checkOutResponse) // 剩余积分可兑换上限
  37. commit('setEnablePoints',enablePoints);
  38. commit('setAvailable',available);
  39. commit('setMaxPointsTime',maxPointsTime);
  40. commit('setMaxDiscountFee',maxDiscountFee);
  41. let pointsTime = 0
  42. // 深圳
  43. if ( parkMallCode === 5 ) {
  44. pointsTime = discountFee || 0
  45. } else {
  46. pointsTime = discountFee ? discountFee / unitAmount: 0
  47. }
  48. commit('setPointsTime',pointsTime);
  49. commit('setPointsPerUnit',pointsPerUnit);
  50. commit('setMaxOneDayDiscountFee',maxOneDayDiscountFee);
  51. commit('setPointsPerHour',pointsPerUnit);
  52. commit('setUnitAmount',unitAmount);
  53. commit('setUnlimitUsePoints', unlimitUsePoints);
  54. commit('setUnitHour', unitHour);
  55. commit('setIntegralDesc',pointsTime ? `已选择兑换${ pointsTime }${parkMallCode === 5 ? '元' : '小时'}` : available > pointsPerUnit ? `${ available }积分可减免` : `${ pointsPerUnit }积分可停车1小时`); // 积分优惠处的描述
  56. },
  57. // 当前最大可选优惠时间
  58. /*
  59. * 当前最大可选优惠时间
  60. * 1、不能超过积分上限
  61. * 2、
  62. * */
  63. maxPointsTimeMath( {commit,dispatch,state},checkOutResponse ) {
  64. const {discountInfo,parkingRule,parkInfo} = checkOutResponse
  65. const {
  66. points: [{
  67. available, // 用户可用的积分(当前车辆在) available是用户选择抵扣N小时后剩余的可用积分
  68. unitAmount, // 兑换值(元)
  69. pointsPerUnit = 0, // 500 积分对应的价值
  70. discountFee,
  71. maxDiscountFee,
  72. }]
  73. } = discountInfo
  74. const {
  75. parkMallCode
  76. } = parkInfo
  77. // 总积分
  78. const max = available / pointsPerUnit * unitAmount;
  79. const timeNum = maxDiscountFee / unitAmount;
  80. console.log(';999988888;',timeNum);
  81. /**
  82. * maxDiscountFee 单次最大可使用金额
  83. * availableDiscountFee 当日剩余可使用优惠金额
  84. * available 当前用户的剩余积分,也可以说是可以抵扣的积分,如果有积分抵扣自动勾选,那么这个值=这个值-已勾选积分
  85. *
  86. */
  87. // if ( !state.maxPointsTime ) {
  88. // commit('setMaxPointsTime', discountFee || max)
  89. // }
  90. // if (
  91. // parkInfo.parkMallCode === 3 &&
  92. // enablePoints && !maxPointsTime
  93. // ) {
  94. // return commit('setIntegralDesc', `今日已达上限`);
  95. // }
  96. // return commit('setIntegralDesc', `${available}积分可减免`);
  97. },
  98. // 积分减免
  99. pointsMath( {commit,dispatch,state},{type,callback} ) {
  100. const {parkInfo,parkingRule} = state.orderDetail
  101. const {maxOneTimeDiscountTime,oneTimeLimitation,oneDayLimitation,hourPrice,remainConsumeTime} = parkingRule
  102. const {parkMallCode} = parkInfo
  103. console.log(376,maxOneTimeDiscountTime,state.availableDiscountFee)
  104. /**
  105. * maxDiscountFee 单次最大可使用金额
  106. * availableDiscountFee 当日剩余可使用优惠金额
  107. * available 当前用户的剩余积分,也可以说是可以抵扣的积分,如果有积分抵扣自动勾选,那么这个值=这个值-已勾选积分
  108. */
  109. // 计算用户当前的最大可使用积分的总数(单位:元)
  110. let maxPointsTime = state.available / ( state.pointsPerUnit / state.unitAmount ) + state.pointsTime;
  111. if ( maxPointsTime > state.maxDiscountFee ) {
  112. maxPointsTime = state.maxDiscountFee
  113. }
  114. let msg = ''
  115. // 验证最大积分的总数(单位:元)
  116. if ( maxPointsTime > state.availableDiscountFee ) {
  117. maxPointsTime = state.availableDiscountFee
  118. }
  119. console.log(392,state.pointsTime,maxPointsTime,parkMallCode);
  120. if ( parkMallCode === 5 && state.unlimitUsePoints === 'LIMIT' ) {
  121. if ( type === 'add' && state.pointsTime >= maxPointsTime ) {
  122. // 以下是深圳积分上限规则
  123. // 超出抵扣上限,每日最高可抵扣${this.parkFee.parkInfoEntity.maxOneDayHour}元 '优惠金额' >= '每日最高可抵扣'
  124. if ( state.usingTotalDiscount - state.paperDiscountFee >= state.maxOneDayDiscountFee && !msg ) {
  125. msg = `超出抵扣上限,每日最高可抵扣${ state.maxOneDayDiscountFee }元`
  126. }
  127. // 超出抵扣上限,每次最高可抵扣${this.integralMaxMoney}元 '积分兑换时长' >= '深圳前海停车积分上限'q
  128. if ( state.pointsTime >= state.maxDiscountFee && !msg ) {
  129. msg = `超出抵扣上限,每次最高可抵扣${ state.maxDiscountFee }元`
  130. }
  131. }
  132. }
  133. // 单日上限限制
  134. if ( oneDayLimitation && !msg && parkMallCode !== 5) {
  135. const maxOneTimeDiscountFee = maxOneTimeDiscountTime * hourPrice
  136. const remainConsumeTimeFee = remainConsumeTime * hourPrice
  137. // 计算单日剩余 remainConsumeTime
  138. // 当前使用优惠 state.usingTotalDiscount
  139. // 判断符合上限
  140. // 浦东积分上限判断
  141. if ( parkMallCode === 1 && type === 'add' && state.usingTotalDiscount >= maxOneTimeDiscountFee ) {
  142. msg = `超出抵扣上限,每次最高可抵扣${ maxOneTimeDiscountTime }小时`
  143. }
  144. }
  145. // 单次上限限制
  146. if ( oneTimeLimitation && !msg && parkMallCode !== 5) {
  147. const maxOneTimeDiscountFee = maxOneTimeDiscountTime * hourPrice
  148. const remainConsumeTimeFee = remainConsumeTime * hourPrice
  149. // 计算单日剩余 remainConsumeTime
  150. // 当前使用优惠 state.usingTotalDiscount
  151. // 判断符合上限
  152. // 浦东积分上限判断: 减去纸质优惠券的金额
  153. if ( parkMallCode === 1 && type === 'add' && state.usingTotalDiscount - state.paperDiscountFee >= maxOneTimeDiscountFee * hourPrice ) {
  154. msg = `超出抵扣上限,每次最高可抵扣${ maxOneTimeDiscountTime }小时`
  155. }
  156. }
  157. if ( msg ) {
  158. callback({
  159. message: msg
  160. });
  161. return
  162. }
  163. // 计算浦东的上限
  164. if ( type === 'add' && state.usingTotalDiscount >= state.maxOneDayDiscountFee && parkMallCode === 5 && state.unlimitUsePoints === 'LIMIT') {
  165. return;
  166. }
  167. if ( type === 'add' ) {
  168. let pointsTime = 0;
  169. pointsTime = state.pointsTime + (parkMallCode === 5 ? state.unitAmount : state.unitHour);
  170. const available = state.available - state.pointsPerUnit;
  171. const usingTotalDiscount = state.usingTotalDiscount + (state?.unitHour ? state.unitHour * hourPrice : state.unitAmount); // 重新计算已经使用的优惠逻辑
  172. if ( available > -1 ) {
  173. // 如果是深圳车场
  174. commit('setAvailable',available);
  175. commit('setPointsTime',pointsTime);
  176. commit('setUsingTotalDiscount',usingTotalDiscount);
  177. }
  178. }
  179. if ( type === 'minus' && state.pointsTime > 0 ) {
  180. const pointsTime = state.pointsTime - (parkMallCode === 5 ? state.unitAmount : state.unitHour);
  181. const usingTotalDiscount = state.usingTotalDiscount - (state?.unitHour ? state.unitHour * hourPrice : state.unitAmount); // 重新计算已经使用的优惠逻辑
  182. // 如果是深圳车场
  183. commit('setAvailable',state.available + state.pointsPerUnit);
  184. commit('setPointsTime',pointsTime);
  185. commit('setUsingTotalDiscount',usingTotalDiscount);
  186. }
  187. },
  188. cancelPointsMath( {commit,dispatch,state},callback ) {
  189. const {
  190. discountInfo: {points},
  191. parkInfo: {parkMallCode},
  192. parkingRule,
  193. } = state.orderDetail;
  194. const [{pointsPerHour,available,discountFee}] = points;
  195. const {maxPointsTime,enablePoints,unitAmount} = parkingRule;
  196. if ( !points ) {
  197. callback && callback();
  198. return
  199. }
  200. let pointsTime = 0
  201. // 深圳
  202. if ( parkMallCode === 5 ) {
  203. pointsTime = discountFee || 0
  204. } else {
  205. pointsTime = discountFee ? discountFee / unitAmount: 0
  206. }
  207. commit('setAvailable',available);
  208. commit('setPointsTime',discountFee || 0);
  209. callback && callback();
  210. },
  211. async savePointsMath( {commit,dispatch,state},callback ) {
  212. try {
  213. let orderDetail = {...state.orderDetail};
  214. const {parkInfo,parkingRule} = state.orderDetail
  215. const {maxOneTimeDiscountTime,oneTimeLimitation,oneDayLimitation,hourPrice,remainConsumeTime} = parkingRule
  216. const {parkMallCode} = parkInfo
  217. // 如果是重复提交,则不做重新计算
  218. if ( orderDetail.discountInfo.points[0].available === state.available ) {
  219. callback && callback();
  220. return
  221. }
  222. // 如果是会员等级减免\如果是消费减免\如果是纸质优惠券\电子券的话
  223. setSelected(['memberGrade','consume','paperCoupons','coupons'],orderDetail)
  224. // 计算积分修改之后的金额,返回给后端
  225. // orderDetail.discountInfo.points[0].discountFee = state.pointsTime * orderDetail.discountInfo.points[0].unitAmount;
  226. orderDetail.discountInfo.points[0].discountFee = parkMallCode === 5 ? state.pointsTime : state.pointsTime * orderDetail.discountInfo.points[0].unitAmount;
  227. orderDetail.discountInfo.points[0].available = state.available;
  228. orderDetail.discountInfo.points[0].selected = true;
  229. /* 其他优惠统计 */
  230. // 优惠券: 如果没有添加 selected 字段,则增加 selected; 默认取 defaultSelected;
  231. if ( orderDetail?.discountInfo?.hasOwnProperty('coupons') && !orderDetail?.discountInfo?.coupons[0]?.hasOwnProperty('selected') ) {
  232. orderDetail.discountInfo.coupons = orderDetail.discountInfo.coupons.map(elm => {
  233. elm.selected = elm.defaultSelected || false;
  234. return elm;
  235. })
  236. }
  237. const res = await calculateDiscount({
  238. ...orderDetail,
  239. });
  240. console.log('积分修改失败',{res});
  241. dispatch('orderInitRule',res);
  242. callback && callback();
  243. } catch ( err ) {
  244. console.log('积分修改失败!',err);
  245. callback && callback();
  246. }
  247. //
  248. },
  249. getIntegralDesc() {
  250. // 自动勾选积分优惠
  251. // 深圳是最大抵扣金额
  252. // available
  253. /*
  254. if (this.duration > 0) {
  255. // 深圳特殊处理(单位:金额)
  256. if (this.parkMallCode === 5) {
  257. return `已选择兑换${this.duration}元`;
  258. }
  259. return `已选择兑换${this.duration}小时`;
  260. }
  261. if (this.bonusCopy < this.integral) {
  262. return `${this.integral}积分可停车1小时`;
  263. }
  264. if (
  265. this.parkMallCode === 3 &&
  266. this.member?.currnentintegral >= this.integral &&
  267. !this.bonus
  268. ) {
  269. return `今日已达上限`;
  270. }
  271. return `${this.bonus}积分可减免`;
  272. */
  273. },
  274. }