123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2023/7/12
- * Time: 11:18
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class OrderExtraData extends Model
- {
- protected $table = 'order_extra_data';
- public $timestamps = false;
- protected static $unguarded = true;
- public static function getExtraData($orderIds) {
- if(empty($orderIds)) return null;
- return self::query()->select(['order_id', 'ivrp', 'date_of_collection', 'payment_days'])
- ->where('enable', 1)->whereIn('order_id', $orderIds)->get();
- }
- public static function getOrderIdListBySearch($search) {
- $query = self::query()->where('enable', 1);
- if(!empty($search['date_of_collection'])) {
- $query->where('date_of_collection', '>=', $search['date_of_collection'][0])
- ->where('date_of_collection', '<=', $search['date_of_collection'][1]);
- }
- return $query->pluck('order_id')->all();
- }
- public static function batchSaveData($orderIdList, $params) {
- # 查询已存在的订单
- $existsOrderIdList = self::query()->whereIn('order_id', $orderIdList)->pluck('order_id')->toArray();
- # 找到需要新增的订单
- $newOrderIdList = array_diff($orderIdList, $existsOrderIdList);
- $insertData = [];
- if(!empty($newOrderIdList)) {
- foreach ($newOrderIdList as $orderId) {
- $insertData[] = [
- 'order_id' => $orderId,
- 'enable' => 1,
- 'date_of_collection' => isset($params['date_of_collection']) ? $params['date_of_collection'] : null,
- 'payment_days' => isset($params['payment_days']) ? $params['payment_days'] : null,
- ];
- }
- \DB::begintransaction();
- \App\Log::logInfo('回款日期编辑 - 新增记录', ['insert_data' => $insertData], 'dateOfCollectionRecord');
- $insertRes = self::query()->insert($insertData);
- if(!empty($existsOrderIdList)) {
- $updateRes = self::updateOrderExtraData($params, $existsOrderIdList);
- } else {
- $updateRes = true;
- }
- if($insertRes && $updateRes) {
- \DB::commit();
- return 0;
- } else {
- \DB::rollBack();
- return 500;
- }
- } else {
- $updateRes = self::updateOrderExtraData($params, $existsOrderIdList);
- if($updateRes) {
- return 0;
- } else {
- return 500;
- }
- }
- }
- public static function saveData($orderId, $params) {
- $exists = self::query()->where('order_id', $orderId)->exists();
- if($exists) {
- $res = self::query()->where('order_id', $orderId)->update($params);
- } else {
- $params['order_id'] = $orderId;
- $res = self::query()->insert($params);
- }
- if($res) {
- return 0;
- } else {
- return 500;
- }
- }
- /**
- * @param $params
- * @param array $existsOrderIdList
- * @return int
- */
- public static function updateOrderExtraData($params, array $existsOrderIdList)
- {
- $params['update_time'] = date('Y-m-d H:i:s');
- $params['enable'] = 1;
- unset($params['invoice_id']);
- if (empty($params['payment_days'])) unset($params['payment_days']);
- \App\Log::logInfo('回款日期编辑 - 更新记录', ['order_list' => $existsOrderIdList, 'update_data' => $params], 'dateOfCollectionRecord');
- return self::query()->whereIn('order_id', $existsOrderIdList)->update($params);
- }
- }
|