Explorar el Código

Merge remote-tracking branch 'origin/dev' into dev

xupuyuan hace 10 meses
padre
commit
e2e744b14d

+ 51 - 1
app/common/dao/user/UserSignFugouDao.php

@@ -3,12 +3,20 @@
3 3
 namespace app\common\dao\user;
4 4
 
5 5
 use app\common\dao\BaseDao;
6
+use app\common\enum\user\UserSignFugouEnum;
7
+use app\common\model\BaseModel;
6 8
 use app\common\model\user\UserSignFugou;
7 9
 use app\entity\CommonEntity;
8 10
 use app\entity\data\user\UserSignFugouEntity;
11
+use think\db\exception\DataNotFoundException;
12
+use think\db\exception\DbException;
13
+use think\db\exception\ModelNotFoundException;
9 14
 
10 15
 class UserSignFugouDao extends BaseDao
11 16
 {
17
+    /**
18
+     * @return BaseModel
19
+     */
12 20
     protected function getModel(): string
13 21
     {
14 22
         return UserSignFugou::class;
@@ -18,7 +26,7 @@ class UserSignFugouDao extends BaseDao
18 26
      * @param array $idList
19 27
      * @return CommonEntity[]
20 28
      */
21
-    public function getStoreSignFugouEntityListByIdList(array $idList): array
29
+    public function getUserSignFugouEntityListByIdList(array $idList): array
22 30
     {
23 31
         $entityList = [];
24 32
         try {
@@ -38,4 +46,46 @@ class UserSignFugouDao extends BaseDao
38 46
         }
39 47
         return $entityList;
40 48
     }
49
+
50
+    /**
51
+     * @param string $orderType
52
+     * @param array $orderIdList
53
+     * @return UserSignFugouEntity[]
54
+     */
55
+    public function getUserSignFugouEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
56
+    {
57
+        $entityList = [];
58
+        try {
59
+            /** @var UserSignFugou $model */
60
+            $model = $this->getModel();
61
+            $dataList = $model::getDB()
62
+                ->where('order_type', '=', $orderType)
63
+                ->whereIn('order_id', $orderIdList)
64
+                ->where('status', '=', UserSignFugouEnum::STATUS['Pending']['code'])
65
+                ->select()
66
+                ->toArray();
67
+            if (!empty($dataList)) {
68
+                $entityList = array_map(function ($data) {
69
+                    return UserSignFugouEntity::newInstance($data);
70
+                }, $dataList);
71
+            }
72
+        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
73
+
74
+        }
75
+        return $entityList;
76
+    }
77
+
78
+    /**
79
+     * 批量更新
80
+     * @param $dataList
81
+     * @return array
82
+     */
83
+    public function saveAll($dataList): array
84
+    {
85
+        try {
86
+            return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
87
+        } catch (\Exception $e) {
88
+            return [];
89
+        }
90
+    }
41 91
 }

+ 11 - 0
app/common/repositories/store/order/StoreOrderRepository.php

@@ -11649,6 +11649,17 @@ class StoreOrderRepository extends BaseRepository
11649 11649
                 $userBillRepository->executeByIdList($billIdList);
11650 11650
             }
11651 11651
 
11652
+            /** @var UserSignFugouRepository $userSignFugouRepository */
11653
+            $userSignFugouRepository = app()->make(UserSignFugouRepository::class);
11654
+            $userSignFugouEntityList = $userSignFugouRepository->getUserSignFugouEntityListByOrderIdListAndPending(UserSignFugouEnum::ORDER_TYPE['Platform']['code'], $unsettledOrderIdList);
11655
+            if (!empty($userSignFugouEntityList)) {
11656
+                $fugouIdList = array_map(function ($userSignFugouEntity) {
11657
+                    return $userSignFugouEntity->getId();
11658
+                }, $userSignFugouEntityList);
11659
+                // 去结算 养老金 和佣金
11660
+                $userSignFugouRepository->executeByIdList($fugouIdList);
11661
+            }
11662
+
11652 11663
             /** 结算 积分 */
11653 11664
             /** @var UserSignScoreRepository $userSignScoreRepository */
11654 11665
             $userSignScoreRepository = app()->make(UserSignScoreRepository::class);

+ 124 - 40
app/common/repositories/user/UserSignFugouRepository.php

@@ -6,6 +6,7 @@ use app\common\dao\user\UserSignFugouDao;
6 6
 use app\common\enum\user\UserSignFugouEnum;
7 7
 use app\common\repositories\BaseRepository;
8 8
 use app\common\repositories\store\order\StoreOrderRepository;
9
+use app\entity\data\user\UserEntity;
9 10
 use app\entity\data\user\UserSignFugouEntity;
10 11
 use think\db\exception\DbException;
11 12
 use think\exception\ValidateException;
@@ -21,9 +22,9 @@ class UserSignFugouRepository extends BaseRepository
21 22
      * @param int $id
22 23
      * @return UserSignFugouEntity
23 24
      */
24
-    public function getStoreSignFugouEntityById(int $id): UserSignFugouEntity
25
+    public function getUserSignFugouEntityById(int $id): UserSignFugouEntity
25 26
     {
26
-        $entityList = $this->getStoreSignFugouEntityListByIdList([$id]);
27
+        $entityList = $this->getUserSignFugouEntityListByIdList([$id]);
27 28
         $entity = array_shift($entityList);
28 29
         if ($entity instanceof UserSignFugouEntity) {
29 30
             return $entity;
@@ -37,9 +38,19 @@ class UserSignFugouRepository extends BaseRepository
37 38
      * @param array $idList
38 39
      * @return UserSignFugouEntity[]
39 40
      */
40
-    public function getStoreSignFugouEntityListByIdList(array $idList): array
41
+    public function getUserSignFugouEntityListByIdList(array $idList): array
41 42
     {
42
-        return $this->dao->getStoreSignFugouEntityListByIdList($idList);
43
+        return $this->dao->getUserSignFugouEntityListByIdList($idList);
44
+    }
45
+
46
+    /**
47
+     * @param string $orderType
48
+     * @param array $orderIdList
49
+     * @return UserSignFugouEntity[]
50
+     */
51
+    public function getUserSignFugouEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
52
+    {
53
+        return $this->dao->getUserSignFugouEntityListByOrderIdListAndPending($orderType, $orderIdList);
43 54
     }
44 55
 
45 56
     public function create($data)
@@ -108,45 +119,118 @@ class UserSignFugouRepository extends BaseRepository
108 119
      */
109 120
     public function execute(int $id): UserSignFugouEntity
110 121
     {
111
-        $UserSignFugouEntity = $this->getStoreSignFugouEntityById($id);
112
-        if (empty($UserSignFugouEntity->getId()) || empty($UserSignFugouEntity->getUid())) {
113
-            throw new ValidateException('无效的复购记录');
114
-        }
115
-        if ($UserSignFugouEntity->getStatus() == UserSignFugouEnum::STATUS['Invalid']['code']) {
116
-            throw new ValidateException('该复购记录已失效');
117
-        }
118
-        if (!empty($UserSignFugouEntity->getSettlementTime()) || $UserSignFugouEntity->getStatus() == UserSignFugouEnum::STATUS['Valid']['code']) {
119
-            throw new ValidateException('该复购记录已更新,请勿重复操作');
120
-        }
122
+        $result = $this->executeByIdList([$id]);
123
+        return array_shift($result);
124
+    }
121 125
 
122
-        /** @var UserRepository $userRepository */
123
-        $userRepository = app()->make(UserRepository::class);
124
-        $userEntity = $userRepository->getUserEntityByUid($UserSignFugouEntity->getUid());
125
-        if (empty($userEntity->getUid())) {
126
-            throw new ValidateException('未查询到用户信息');
127
-        }
128
-        if ($UserSignFugouEntity->getType() == UserSignFugouEnum::TYPE['Consume']['code']) {
129
-            $surplus = bcsub($userEntity->getFugou(), $UserSignFugouEntity->getNumber(), 2);
130
-            if ($surplus < 0.00) {
131
-                throw new ValidateException('用户复购不足');
126
+    /**
127
+     * 将 复购 记录设置为生效状态 并对用户的 复购 字段进行增减
128
+     * @param array $idList
129
+     * @return array
130
+     */
131
+    public function executeByIdList(array $idList): array
132
+    {
133
+        $userSignFugouEntityList = $this->getUserSignFugouEntityListByIdList($idList);
134
+        $userIdList = [];
135
+
136
+        // 忽略 已经生效的 记录
137
+        $userSignFugouEntityList = array_filter($userSignFugouEntityList, function ($entity) use (&$userIdList) {
138
+            // 积分数据不为空 用户不为空 状态属于待结算 并且结算字段为空
139
+            if ((!empty($entity->getId()) && !empty($entity->getUid()) && ($entity->getStatus() == UserSignFugouEnum::STATUS['Pending']['code']) && empty($entity->getSettlementTime()))) {
140
+                $userIdList[] = $entity->getUid();
141
+                return true;
132 142
             }
133
-        } else {
134
-            $surplus = bcadd($userEntity->getFugou(), $UserSignFugouEntity->getNumber(), 2);
135
-        }
136
-        try {
137
-            $UserSignFugouEntity
138
-                ->setSurplus($surplus)
139
-                ->setSettlementTime(date('Y-m-d H:i:s'))
140
-                ->setStatus(UserSignFugouEnum::STATUS['Valid']['code']);
141
-            $userRepository->update($userEntity->getUid(), ['Fugou' => $surplus]);
142
-            $this->dao->update($UserSignFugouEntity->getId(), [
143
-                'surplus' => $UserSignFugouEntity->getSurplus(),
144
-                'settlement_time' => $UserSignFugouEntity->getSettlementTime(),
145
-                'status' => $UserSignFugouEntity->getStatus()
146
-            ]);
147
-        } catch (DbException $e) {
143
+            return false;
144
+        });
145
+
146
+        if (!empty($userSignFugouEntityList)) {
147
+            // 获取用户 映射信息
148
+            /** @var UserRepository $userRepository */
149
+            $userRepository = app()->make(UserRepository::class);
150
+            /** @var UserEntity[] $userEntityMapByUid */
151
+            $userEntityMapByUid = [];
152
+            if (!empty($userIdList)) {
153
+                $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList);
154
+            }
155
+
156
+            /** @var UserEntity[] $updateUserEntityList */
157
+            $updateUserEntityList = [];
158
+            /** @var UserSignFugouEntity[] $updateUserFugouEntityList */
159
+            $updateUserFugouEntityList = [];
160
+            foreach ($userSignFugouEntityList as $userSignFugouEntity) {
161
+
162
+                $userEntity = $userEntityMapByUid[$userSignFugouEntity->getUid()];
163
+                if (empty($userEntity) || empty($userEntity->getUid())) {
164
+                    throw new ValidateException("未查询到用户信息(FugouId:{$userSignFugouEntity->getId()})");
165
+                }
166
+
167
+                // 组建 更新 用户信息
168
+                if (isset($updateUserEntityList[$userEntity->getUid()])) {
169
+                    $updateUserEntity = $updateUserEntityList[$userEntity->getUid()];
170
+                } else {
171
+                    /** @var UserEntity $updateUserEntity */
172
+                    $updateUserEntity = UserEntity::newInstance();
173
+                    $updateUserEntity
174
+                        ->setUid($userEntity->getUid())
175
+                        ->setFugou($userEntity->getFugou());
176
+
177
+                }
178
+                // 组建 账单 记录
179
+                if (isset($updateUserFugouEntityList[$userSignFugouEntity->getId()])) {
180
+                    $updateUserSignFugouEntity = $updateUserFugouEntityList[$userSignFugouEntity->getId()];
181
+                } else {
182
+                    /** @var UserSignFugouEntity $updateUserSignFugouEntity */
183
+                    $updateUserSignFugouEntity = UserSignFugouEntity::newInstance();
184
+                    // 更新 账单记录 状态
185
+                    $updateUserSignFugouEntity
186
+                        ->setId($userSignFugouEntity->getId())
187
+                        ->setStatus(UserSignFugouEnum::STATUS['Valid']['code'])
188
+                        ->setSettlementTime(date('Y-m-d H:i:s'));
189
+                }
190
+
191
+                if ($userSignFugouEntity->getType() == UserSignFugouEnum::TYPE['Consume']['code']) {
192
+                    // 支出
193
+                    $updateUserEntity->setFugou(bcsub($updateUserEntity->getFugou(), $userSignFugouEntity->getNumber(), 2));
194
+                    if ($updateUserEntity->getFugou() < 0.00) {
195
+                        throw new ValidateException('用户复购金不足');
196
+                    }
197
+                } else {
198
+                    $updateUserEntity->setFugou(bcadd($updateUserEntity->getFugou(), $userSignFugouEntity->getNumber(), 2));
199
+                }
200
+                $updateUserSignFugouEntity->setSurplus($updateUserEntity->getFugou());
148 201
 
202
+                $updateUserEntityList[$userSignFugouEntity->getUid()] = $updateUserEntity;
203
+                $updateUserFugouEntityList[$userSignFugouEntity->getId()] = $updateUserSignFugouEntity;
204
+            }
205
+            try {
206
+                if (!empty($updateUserEntityList)) {
207
+                    $userRepository->batchUpdateUserDataByDataList(
208
+                        array_map(function (UserEntity $updateUserEntity) {
209
+                            return $updateUserEntity->toUnderlineArray();
210
+                        }, $updateUserEntityList)
211
+                    );
212
+                }
213
+
214
+                if (!empty($updateUserFugouEntityList)) {
215
+                    $this->batchUpdateUserSignFugouDataByDataList(
216
+                        array_map(function (UserSignFugouEntity $updateUserSignFugouEntity) {
217
+                            return $updateUserSignFugouEntity->toUnderlineArray();
218
+                        }, $updateUserFugouEntityList)
219
+                    );
220
+                }
221
+            } catch (\Exception $e) {
222
+                throw new ValidateException('用户复购金结算失败');
223
+            }
149 224
         }
150
-        return $UserSignFugouEntity;
225
+        return $idList;
226
+    }
227
+
228
+    /**
229
+     * @param $dataList
230
+     * @return array
231
+     */
232
+    public function batchUpdateUserSignFugouDataByDataList($dataList): array
233
+    {
234
+        return $this->dao->saveAll($dataList);
151 235
     }
152 236
 }

+ 1 - 1
app/common/repositories/user/UserSignGongxianRepository.php

@@ -214,7 +214,7 @@ class UserSignGongxianRepository extends BaseRepository
214 214
                 );
215 215
             }
216 216
 
217
-            if (!empty($updateUserBillEntityList)) {
217
+            if (!empty($updateUserScoreEntityList)) {
218 218
                 $this->batchUpdateUserSignGongxianDataByDataList(
219 219
                     array_map(function (UserSignGongxianEntity $updateUserSignGongxianEntity) {
220 220
                         return $updateUserSignGongxianEntity->toUnderlineArray();

+ 2 - 2
app/common/repositories/user/UserSignScoreRepository.php

@@ -228,8 +228,8 @@ class UserSignScoreRepository extends BaseRepository
228 228
                         }, $updateUserScoreEntityList)
229 229
                     );
230 230
                 }
231
-            } catch (DbException $e) {
232
-
231
+            } catch (\Exception $e) {
232
+                throw new ValidateException('用户积分结算失败');
233 233
             }
234 234
         }
235 235
         return $idList;