新版订单消耗系统

OrderExtraData.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2023/7/12
  6. * Time: 11:18
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class OrderExtraData extends Model
  11. {
  12. protected $table = 'order_extra_data';
  13. public $timestamps = false;
  14. protected static $unguarded = true;
  15. public static function getExtraData($orderIds) {
  16. if(empty($orderIds)) return null;
  17. return self::query()->select(['order_id', 'ivrp', 'date_of_collection', 'payment_days'])
  18. ->where('enable', 1)->whereIn('order_id', $orderIds)->get();
  19. }
  20. public static function getOrderIdListBySearch($search) {
  21. $query = self::query()->where('enable', 1);
  22. if(!empty($search['date_of_collection'])) {
  23. $query->where('date_of_collection', '>=', $search['date_of_collection'][0])
  24. ->where('date_of_collection', '<=', $search['date_of_collection'][1]);
  25. }
  26. return $query->pluck('order_id')->all();
  27. }
  28. public static function batchSaveData($orderIdList, $params) {
  29. # 查询已存在的订单
  30. $existsOrderIdList = self::query()->whereIn('order_id', $orderIdList)->pluck('order_id')->toArray();
  31. # 找到需要新增的订单
  32. $newOrderIdList = array_diff($orderIdList, $existsOrderIdList);
  33. $insertData = [];
  34. if(!empty($newOrderIdList)) {
  35. foreach ($newOrderIdList as $orderId) {
  36. $insertData[] = [
  37. 'order_id' => $orderId,
  38. 'enable' => 1,
  39. 'date_of_collection' => isset($params['date_of_collection']) ? $params['date_of_collection'] : null,
  40. 'payment_days' => isset($params['payment_days']) ? $params['payment_days'] : null,
  41. ];
  42. }
  43. \DB::begintransaction();
  44. \App\Log::logInfo('回款日期编辑 - 新增记录', ['insert_data' => $insertData], 'dateOfCollectionRecord');
  45. $insertRes = self::query()->insert($insertData);
  46. if(!empty($existsOrderIdList)) {
  47. $updateRes = self::updateOrderExtraData($params, $existsOrderIdList);
  48. } else {
  49. $updateRes = true;
  50. }
  51. if($insertRes && $updateRes) {
  52. \DB::commit();
  53. return 0;
  54. } else {
  55. \DB::rollBack();
  56. return 500;
  57. }
  58. } else {
  59. $updateRes = self::updateOrderExtraData($params, $existsOrderIdList);
  60. if($updateRes) {
  61. return 0;
  62. } else {
  63. return 500;
  64. }
  65. }
  66. }
  67. public static function saveData($orderId, $params) {
  68. $exists = self::query()->where('order_id', $orderId)->exists();
  69. if($exists) {
  70. $res = self::query()->where('order_id', $orderId)->update($params);
  71. } else {
  72. $params['order_id'] = $orderId;
  73. $res = self::query()->insert($params);
  74. }
  75. if($res) {
  76. return 0;
  77. } else {
  78. return 500;
  79. }
  80. }
  81. /**
  82. * @param $params
  83. * @param array $existsOrderIdList
  84. * @return int
  85. */
  86. public static function updateOrderExtraData($params, array $existsOrderIdList)
  87. {
  88. $params['update_time'] = date('Y-m-d H:i:s');
  89. $params['enable'] = 1;
  90. unset($params['invoice_id']);
  91. if (empty($params['payment_days'])) unset($params['payment_days']);
  92. \App\Log::logInfo('回款日期编辑 - 更新记录', ['order_list' => $existsOrderIdList, 'update_data' => $params], 'dateOfCollectionRecord');
  93. return self::query()->whereIn('order_id', $existsOrderIdList)->update($params);
  94. }
  95. }