ActivityLogic.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\CouponRules;
  6. use app\common\model\goods\Goods;
  7. use app\common\model\goods_category\GoodsCategory;
  8. use app\common\model\property\PropertyActivity;
  9. use app\common\model\user\User;
  10. use think\facade\Db;
  11. use think\facade\Log;
  12. /**
  13. * 活动逻辑处理
  14. * Class ActivityLogic
  15. * @package app\api\logic
  16. */
  17. class ActivityLogic extends BaseLogic
  18. {
  19. public static function getHomepageByActivityId($params){
  20. $res = [];
  21. try{
  22. $propertyActivity = PropertyActivity::with(['propertyHeadInfo'])->where('id',$params['property_activity_id'])->findOrEmpty();
  23. if($propertyActivity->isEmpty()){
  24. throw new \Exception('活动不存在');
  25. }
  26. if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
  27. throw new \Exception('活动未开始');
  28. }
  29. if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
  30. throw new \Exception('活动已结束');
  31. }
  32. Log::info('getHomepageByActivityId params:'.json_encode($params));
  33. if($params['user_id']){
  34. // property_head_id householder_mobile householder_name address
  35. $userInfo = User::where('id',$params['user_id'])->findOrEmpty();
  36. if (!$userInfo->isEmpty()) {
  37. // 检查/注册
  38. Log::info('getHomepageByActivityId params-102:'.json_encode([$userInfo]));
  39. PropertyUserLogic::getPropertyUserIdByMobile([
  40. 'householder_mobile' => $userInfo['mobile'],
  41. 'householder_name' => $userInfo['real_name'],
  42. 'address' => '',
  43. 'property_head_id' => $propertyActivity['property_head_id']
  44. ]);
  45. }
  46. }
  47. $res['activity_info'] = $propertyActivity->toArray();
  48. if(in_array($res['activity_info']['url_page'],['adver','adver1','adver2'])){
  49. array_multisort(array_column($res['activity_info']['block_data'], 'sort'), SORT_DESC, $res['activity_info']['block_data']);
  50. foreach ($res['activity_info']['block_data'] as &$v){
  51. array_multisort(array_column($v['goods'], 'recommend_weight'), SORT_DESC, $v['goods']);
  52. if(!empty($v['goods'])){
  53. foreach ($v['goods'] as &$item){
  54. $item['service_fee'] = $item['base_service_fee'];
  55. }
  56. }
  57. $v['goods'] = self::groupArrays($v['goods']);
  58. }
  59. }else{
  60. $res['block_data_structure'] = self::blockDataStructure($res['activity_info']['url_page'],$res['activity_info']['block_data']);
  61. }
  62. $res['coupons'] = CouponRules::with(['couponCategoryOne'])->where('property_activity_id',$params['property_activity_id'])
  63. ->select()
  64. ->toArray();
  65. foreach ($res['coupons'] as &$coupon) {
  66. $coupon['goods_category_id'] = $coupon['couponCategoryOne']['goods_category_id'];
  67. }
  68. $res['goods'] = Goods::where('property_activity_id',$params['property_activity_id'])
  69. ->where('is_agent',1)
  70. ->visible(['id','goods_name','goods_image','base_service_fee','service_total','service_fee','goods_type'])
  71. ->order('is_recommend desc')
  72. ->select()
  73. ->toArray();
  74. // 临时添加
  75. foreach ($res['goods'] as &$good){
  76. $good['service_fee'] = $good['base_service_fee'];
  77. }
  78. return $res;
  79. }catch(\Exception $e){
  80. throw new \Exception($e->getMessage());
  81. }
  82. }
  83. public static function blockDataStructure(string $url_page,array $data): array {
  84. if (empty($data)) return [];
  85. array_multisort(array_column($data, 'sort'), SORT_DESC, $data);
  86. foreach ($data as &$v){
  87. array_multisort(array_column($v['goods'], 'recommend_weight'), SORT_DESC, $v['goods']);
  88. // 临时改价 11-28
  89. if(!empty($v['goods'])){
  90. foreach ($v['goods'] as &$item){
  91. $item['service_fee'] = $item['base_service_fee'];
  92. }
  93. }
  94. // 不同模板 - 板块内商品数据结构不同
  95. switch ($url_page){
  96. case 'adver5':
  97. break;
  98. default:
  99. $v['goods'] = self::groupArrays($v['goods']);
  100. break;
  101. }
  102. }
  103. return $data;
  104. }
  105. public static function groupArrays($array) {
  106. $result = []; $groupSize = 2;
  107. $totalElements = count($array);
  108. for ($i = 0; $i < $totalElements; $i += $groupSize)
  109. { $end = min($i + $groupSize, $totalElements);
  110. $result[] = array_slice($array, $i, $end - $i);
  111. }
  112. return $result;
  113. }
  114. public static function createPropertyOrder($params,$serviceOrder){
  115. try{
  116. // 判断商品是否为 代理活动商品
  117. $goods = Goods::findOrEmpty($params['goods_id']);
  118. if($goods->isEmpty()){
  119. throw new \Exception('产品不存在!');
  120. }
  121. if($goods->property_activity_id > 0){
  122. $propertyActivity = PropertyActivity::findOrEmpty($goods->property_activity_id);
  123. if($propertyActivity->isEmpty()){
  124. throw new \Exception('活动不存在');
  125. }
  126. if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
  127. throw new \Exception('活动未开始');
  128. }
  129. if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
  130. throw new \Exception('活动已结束');
  131. }
  132. // 生成代理单 user_info
  133. // remark address property_head_id householder_name householder_mobile
  134. $result = PropertyOrderLogic::add(array_merge($params,[
  135. 'property_head_id' => $propertyActivity['property_head_id'],
  136. 'householder_mobile' => $params['user_info']['mobile'],
  137. 'householder_name' => $params['user_info']['real_name'],
  138. 'address' => $params['address'],
  139. 'remark' => '',
  140. 'order_status' => 1,
  141. 'work_id' => $serviceOrder['work_id'],
  142. ]));
  143. if($result === false){
  144. throw new \Exception('生成代理单失败');
  145. }
  146. }
  147. return true;
  148. }catch(\Exception $e){
  149. throw new \Exception($e->getMessage());
  150. }
  151. }
  152. }