1
0

ActivityLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\api\logic;
  3. use app\adminapi\logic\property\PropertyUserLogic;
  4. use app\common\logic\BaseLogic;
  5. use app\common\model\coupon\CouponGoods;
  6. use app\common\model\coupon\CouponRules;
  7. use app\common\model\goods\Goods;
  8. use app\common\model\goods_category\GoodsCategory;
  9. use app\common\model\property\PropertyActivity;
  10. use app\common\model\user\User;
  11. use think\facade\Db;
  12. use think\facade\Log;
  13. /**
  14. * 活动逻辑处理
  15. * Class ActivityLogic
  16. * @package app\api\logic
  17. */
  18. class ActivityLogic extends BaseLogic
  19. {
  20. public static function getHomepageByActivityId($params){
  21. $res = [];
  22. try{
  23. $propertyActivity = PropertyActivity::with(['propertyHeadInfo'])->where('id',$params['property_activity_id'])->findOrEmpty();
  24. if($propertyActivity->isEmpty()){
  25. throw new \Exception('活动不存在');
  26. }
  27. if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
  28. throw new \Exception('活动未开始');
  29. }
  30. if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
  31. throw new \Exception('活动已结束');
  32. }
  33. Log::info('getHomepageByActivityId params:'.json_encode($params));
  34. if($params['user_id']){
  35. // property_head_id householder_mobile householder_name address
  36. $userInfo = User::where('id',$params['user_id'])->findOrEmpty();
  37. if (!$userInfo->isEmpty()) {
  38. // 检查/注册
  39. Log::info('getHomepageByActivityId params-102:'.json_encode([$userInfo]));
  40. PropertyUserLogic::getPropertyUserIdByMobile([
  41. 'householder_mobile' => $userInfo['mobile'],
  42. 'householder_name' => $userInfo['real_name'],
  43. 'address' => '',
  44. 'property_head_id' => $propertyActivity['property_head_id']
  45. ]);
  46. }
  47. }
  48. $res['activity_info'] = $propertyActivity->toArray();
  49. if(in_array($res['activity_info']['url_page'],['adver','adver1','adver2'])){
  50. array_multisort(array_column($res['activity_info']['block_data'], 'sort'), SORT_DESC, $res['activity_info']['block_data']);
  51. foreach ($res['activity_info']['block_data'] as &$v){
  52. array_multisort(array_column($v['goods'], 'recommend_weight'), SORT_DESC, $v['goods']);
  53. if(!empty($v['goods'])){
  54. foreach ($v['goods'] as &$item){
  55. $item['service_fee'] = $item['base_service_fee'];
  56. }
  57. }
  58. $v['goods'] = self::groupArrays($v['goods']);
  59. }
  60. $res['coupons'] = CouponRules::with(['couponCategoryOne'])->where('property_activity_id',$params['property_activity_id'])
  61. ->select()
  62. ->toArray();
  63. foreach ($res['coupons'] as &$coupon) {
  64. $coupon['goods_category_id'] = $coupon['couponCategoryOne']['goods_category_id'];
  65. }
  66. $res['goods'] = Goods::where('property_activity_id',$params['property_activity_id'])
  67. ->where('is_agent',1)
  68. ->visible(['id','goods_name','goods_image','base_service_fee','service_total','service_fee','goods_type'])
  69. ->order('is_recommend desc')
  70. ->select()
  71. ->toArray();
  72. // 临时添加
  73. foreach ($res['goods'] as &$good){
  74. $good['service_fee'] = $good['base_service_fee'];
  75. }
  76. }else{
  77. $res['block_data_structure'] = self::blockDataStructure($res['activity_info']['url_page'],$res['activity_info']['block_data']);
  78. $res['coupons'] = self::couponDataStructure($params['property_activity_id']);
  79. }
  80. return $res;
  81. }catch(\Exception $e){
  82. throw new \Exception($e->getMessage());
  83. }
  84. }
  85. public static function blockDataStructure(string $url_page,array $data): array {
  86. if (empty($data)) return [];
  87. array_multisort(array_column($data, 'sort'), SORT_DESC, $data);
  88. foreach ($data as &$v){
  89. array_multisort(array_column($v['goods'], 'recommend_weight'), SORT_DESC, $v['goods']);
  90. if(!empty($v['goods'])){
  91. foreach ($v['goods'] as &$item){
  92. $reserved_fields = $item;
  93. $item = Goods::findOrEmpty($item['id'])->toArray();
  94. $item['code'] = '';
  95. $goods_coupon_id = CouponGoods::where(['goods_id'=>$item['id']])->value('coupon_id')??0;
  96. if($goods_coupon_id){
  97. $coupon = CouponRules::where(['id'=>$goods_coupon_id])->field('code,server_category_name')->findOrEmpty();
  98. $coupon && $item['server_category_name'] = $coupon['server_category_name'];
  99. $coupon && $item['code'] = $coupon['code'];
  100. }
  101. foreach ($reserved_fields as $field => $value) {
  102. !empty($value) && $item[$field] = $value;
  103. }
  104. // 临时改价 11-28
  105. $item['service_fee'] = $item['base_service_fee'];
  106. }
  107. }
  108. // 不同模板 - 板块内商品数据结构不同
  109. switch ($url_page){
  110. case 'adver6':
  111. case 'adver5':
  112. break;
  113. default:
  114. $v['goods'] = self::groupArrays($v['goods']);
  115. break;
  116. }
  117. }
  118. return $data;
  119. }
  120. public static function couponDataStructure(int $property_activity_id): array {
  121. $couponIds = self::getDataIdsByType($property_activity_id, 'coupon_data');
  122. $coupons = CouponRules::with(['couponCategoryOne'])
  123. ->where('id','in',$couponIds)
  124. ->select()
  125. ->toArray();
  126. foreach ($coupons as &$coupon) {
  127. $coupon['goods_category_id'] = $coupon['couponCategoryOne']['goods_category_id'];
  128. }
  129. return $coupons??[];
  130. }
  131. public static function groupArrays($array) {
  132. $result = []; $groupSize = 2;
  133. $totalElements = count($array);
  134. for ($i = 0; $i < $totalElements; $i += $groupSize)
  135. { $end = min($i + $groupSize, $totalElements);
  136. $result[] = array_slice($array, $i, $end - $i);
  137. }
  138. return $result;
  139. }
  140. public static function createPropertyOrder($params,$serviceOrder){
  141. try{
  142. // 代理活动
  143. if(isset($params['property_activity_id']) && !empty($params['property_activity_id'])){
  144. $propertyActivity = PropertyActivity::findOrEmpty($params['property_activity_id']);
  145. if($propertyActivity->isEmpty()){
  146. throw new \Exception('活动不存在');
  147. }
  148. if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
  149. throw new \Exception('活动未开始');
  150. }
  151. if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
  152. throw new \Exception('活动已结束');
  153. }
  154. // 生成代理单 user_info
  155. // remark address property_head_id householder_name householder_mobile
  156. $result = PropertyOrderLogic::add(array_merge($params,[
  157. 'property_head_id' => $propertyActivity['property_head_id'],
  158. 'householder_mobile' => $params['user_info']['mobile'],
  159. 'householder_name' => $params['user_info']['real_name'],
  160. 'address' => $params['address'],
  161. 'remark' => '',
  162. 'order_status' => 1,
  163. 'work_id' => $serviceOrder['work_id'],
  164. ]));
  165. if($result === false){
  166. throw new \Exception('生成代理单失败');
  167. }
  168. }elseif (isset($params['goods_id']) && !empty($params['goods_id'])) {
  169. // 判断商品是否为 代理活动商品
  170. $goods = Goods::findOrEmpty($params['goods_id']);
  171. if ($goods->isEmpty()) {
  172. throw new \Exception('产品不存在!');
  173. }
  174. if ($goods->property_activity_id > 0) {
  175. $propertyActivity = PropertyActivity::findOrEmpty($goods->property_activity_id);
  176. if ($propertyActivity->isEmpty()) {
  177. throw new \Exception('活动不存在');
  178. }
  179. if (!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()) {
  180. throw new \Exception('活动未开始');
  181. }
  182. if (!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()) {
  183. throw new \Exception('活动已结束');
  184. }
  185. // 生成代理单 user_info
  186. // remark address property_head_id householder_name householder_mobile
  187. $result = PropertyOrderLogic::add(array_merge($params, [
  188. 'property_head_id' => $propertyActivity['property_head_id'],
  189. 'householder_mobile' => $params['user_info']['mobile'],
  190. 'householder_name' => $params['user_info']['real_name'],
  191. 'address' => $params['address'],
  192. 'remark' => '',
  193. 'order_status' => 1,
  194. 'work_id' => $serviceOrder['work_id'],
  195. ]));
  196. if ($result === false) {
  197. throw new \Exception('生成代理单失败');
  198. }
  199. }
  200. }
  201. return true;
  202. }catch(\Exception $e){
  203. throw new \Exception($e->getMessage());
  204. }
  205. }
  206. public static function getDataIdsByType($property_activity_id, $type): array
  207. {
  208. $ids = [];
  209. $activityInfo = PropertyActivity::where('id',$property_activity_id)->findOrEmpty();
  210. if(!$activityInfo->isEmpty()){
  211. $activityInfo = $activityInfo->toArray();
  212. if($type === 'block_data'){
  213. foreach ($activityInfo['block_data'] as &$item) {
  214. $ids = array_merge($ids,array_column($item['goods'], 'id'));
  215. }
  216. }
  217. if($type === 'coupon_data'){
  218. $activityInfo['coupon_data'] && $ids = array_merge($ids,array_column($activityInfo['coupon_data'], 'id'));
  219. }
  220. }
  221. return $ids??[0];
  222. }
  223. }