Browse Source

feat(create-order): 优化产品有效期计算逻辑

- 增加活动赠送周期的处理
- 优化时间计算方法,支持按日、月、年等多种周期计算
- 改进有效期显示格式,使其更加清晰易懂
- 修复数据流量包默认有效期为 2 年的问题

Signed-off-by: tangshizhe <48740614+tangshizhe@users.noreply.github.com>
tangshizhe 1 month ago
parent
commit
78de250766

+ 13 - 3
src/views/create-order/components/order-detail-submodule/OrderDetailCardProductList.vue

@@ -487,7 +487,7 @@ export default {
 
     },
     calculateValidityPeriod(parsedFilter, product) {
-      const { buy_cycle, buy_type, give_cycle, give_type, validYear } = parsedFilter;
+      const { buy_cycle, buy_type, give_cycle, give_type, validYear, activity_give_cycle, activity_give_type } = parsedFilter;
       const { product_type, returned_open } = product;
       const returned_opens = returned_open === 1 ? '(全额回款当日开通)' : '';
       if (product_type === '数据流量包') {
@@ -537,8 +537,14 @@ export default {
       const giveResult = processCycle(give_cycle, give_type);
 
       // 总天数
-      const totalDays = buyResult.days + giveResult.days;
+      let totalDays = buyResult.days + giveResult.days;
 
+      // 活动赠送时长
+      let activityGiveResult = ''
+      if (activity_give_cycle) {
+        activityGiveResult = processCycle(activity_give_cycle, activity_give_type);
+        totalDays += activityGiveResult.days;
+      }
       if (totalDays <= 0) return '-';
       let totalText = '';
       if(buy_type === 1) {
@@ -571,7 +577,11 @@ export default {
           }
         }
       }
-      const giveResultText = giveResult.text ? `赠送${giveResult.text},` : '';
+      let giveResultText = giveResult.text ? `赠送${giveResult.text},` : '';
+      if(activityGiveResult) {
+        giveResultText = giveResult.text ? `销售赠${giveResult.text},` : '';
+        return `付费${buyResult.text},活动赠${activityGiveResult.text},${giveResultText}合计<span class="color_main">${totalText}</span>${returned_opens}`; 
+      }
       return `付费${buyResult.text},${giveResultText}合计<span class="color_main">${totalText}</span>${returned_opens}`;
     },
     getValidityPeriodHtml(product, item) {

+ 72 - 27
src/views/create-order/components/order-detail-submodule/SelectOrderDetailCard.vue

@@ -710,57 +710,102 @@ export default {
       return div(a, b)
     },
     calculateValidityPeriod(parsedFilter, product) {
-      const { buy_cycle, buy_type, give_cycle, give_type, validYear } = parsedFilter
-      const { product_type, returned_open } = product
+      const { buy_cycle, buy_type, give_cycle, give_type, validYear, activity_give_cycle, activity_give_type } = parsedFilter;
+      const { product_type, returned_open } = product;
       const returned_opens = returned_open === 1 ? '(全额回款当日开通)' : '';
-      if(product_type === '数据流量包') {
-        return `${validYear}年${returned_opens}`;
+      if (product_type === '数据流量包') {
+        // 数据流量包默认2年
+        return `${validYear || 2}年${returned_opens}`;
       }
-      let totalMonths = 0;
-      let buyText = '';
-      let giveText = '';
+
       const TIME_MAP = {
-        '1': '',
+        '1': '日',
         '2': '月',
         '3': '年',
         '4': '季度'
-      }
+      };
+
       // 安全地将字符串转为整数,默认为 0
       const parseCycle = (cycle) => {
         const num = parseInt(cycle);
         return isNaN(num) ? 0 : num;
       };
-      // 计算周期并返回文本和月份数
+
+      // 处理单个周期,返回文本和总天数
       const processCycle = (cycle, type) => {
         const textValue = TIME_MAP[type] || '';
         const value = parseCycle(cycle);
 
-        let months = 0;
+        let days = 0;
         let text = '';
 
-        if (textValue.includes('月')) {
-          months += value;
+        if (textValue.includes('日')) {
+          days += value;
+          text = `${value}日`;
+        } else if (textValue.includes('月')) {
+          days += value * 30;
           text = `${value}个月`;
-        } else if (textValue.includes('年')) {
-          months += value * 12;
-          text = `${value}年`;
         } else if (textValue.includes('季度')) {
-          months += value * 3;
+          days += value * 90;
           text = `${value}季度`;
-        } else if (textValue.includes('')) {
-          months += value / 30; // 注意:此处为近似值
-          text = `${value}`;
+        } else if (textValue.includes('')) {
+          days += value * 365;
+          text = `${value}`;
         }
-
-        return { months, text };
+        return { days, text };
       };
+
+      // 计算购买和赠送部分
       const buyResult = processCycle(buy_cycle, buy_type);
       const giveResult = processCycle(give_cycle, give_type);
-      totalMonths = buyResult.months + giveResult.months;
-      buyText = buyResult.text;
-      giveText = giveResult.text;
-      if(totalMonths === 0) return '-'
-      return `付费${buyText},赠送${giveText},合计<span class="color_main">${totalMonths.toFixed(0)}</span>个月${returned_opens}`;
+
+      // 总天数
+      let totalDays = buyResult.days + giveResult.days;
+
+      // 活动赠送时长
+      let activityGiveResult = ''
+      if (activity_give_cycle) {
+        activityGiveResult = processCycle(activity_give_cycle, activity_give_type);
+        totalDays += activityGiveResult.days;
+      }
+      if (totalDays <= 0) return '-';
+      let totalText = '';
+      if(buy_type === 1) {
+        totalText = `${totalDays}日`;
+      } else if(buy_type === 3) {
+        if(totalDays % 365 === 0) {
+          totalText = `${Math.floor(totalDays / 365)}年`;
+        } else {
+          // 获取年份
+          const year = Math.floor(totalDays / 365);
+          // 获取不够一年的天数
+          const lessYearDays = totalDays % 365;
+          if(lessYearDays % 30 === 0) {
+            totalText = `${year ? year + '年' : ''}${Math.floor(lessYearDays / 30)}个月`; 
+          } else {
+            totalText = `${year ? year + '年' : ''}${lessYearDays % 30}日`;
+          }
+        }
+      } else {
+        if(totalDays % 30 === 0) {
+          totalText = `${Math.floor(totalDays / 30)}个月`;
+        } else {
+          // 如果天数小于30天,则不显示月,只显示天
+          const isHas30Days = totalDays / 30 < 1;
+          totalText = `${isHas30Days ? totalDays % 30 : Math.floor(totalDays / 30)}个月${totalDays % 30}日`;
+          if(isHas30Days) {
+            totalText = `${totalDays % 30}日`;
+          } else {
+            totalText = `${Math.floor(totalDays / 30)}个月${totalDays % 30}日`;
+          }
+        }
+      }
+      let giveResultText = giveResult.text ? `赠送${giveResult.text},` : '';
+      if(activityGiveResult) {
+        giveResultText = giveResult.text ? `销售赠${giveResult.text},` : '';
+        return `付费${buyResult.text},活动赠${activityGiveResult.text},${giveResultText}合计<span class="color_main">${totalText}</span>${returned_opens}`; 
+      }
+      return `付费${buyResult.text},${giveResultText}合计<span class="color_main">${totalText}</span>${returned_opens}`;
     },
 
     getValidityPeriodHtml(product, item) {