ActivityLogic.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. array_multisort(array_column($res['activity_info']['block_data'], 'sort'), SORT_DESC, $res['activity_info']['block_data']);
  49. foreach ($res['activity_info']['block_data'] as &$v){
  50. array_multisort(array_column($v['goods'], 'recommend_weight'), SORT_DESC, $v['goods']);
  51. $v['goods'] = self::groupArrays($v['goods']);
  52. }
  53. $res['coupons'] = CouponRules::with(['couponCategoryOne'])->where('property_activity_id',$params['property_activity_id'])
  54. ->select()
  55. ->toArray();
  56. foreach ($res['coupons'] as &$coupon) {
  57. $coupon['goods_category_id'] = $coupon['couponCategoryOne']['goods_category_id'];
  58. }
  59. $res['goods'] = Goods::where('property_activity_id',$params['property_activity_id'])
  60. ->where('is_agent',1)
  61. ->visible(['id','goods_name','goods_image','base_service_fee','service_total','service_fee','goods_type'])
  62. ->order('is_recommend desc')
  63. ->select()
  64. ->toArray();
  65. // 临时添加
  66. foreach ($res['goods'] as &$good){
  67. $good['service_fee'] = $good['base_service_fee'];
  68. }
  69. return $res;
  70. }catch(\Exception $e){
  71. throw new \Exception($e->getMessage());
  72. }
  73. }
  74. public static function groupArrays($array) {
  75. $result = []; $groupSize = 2;
  76. $totalElements = count($array);
  77. for ($i = 0; $i < $totalElements; $i += $groupSize)
  78. { $end = min($i + $groupSize, $totalElements);
  79. $result[] = array_slice($array, $i, $end - $i);
  80. }
  81. return $result;
  82. }
  83. public static function createPropertyOrder($params,$serviceOrder){
  84. try{
  85. // 判断商品是否为 代理活动商品
  86. $goods = Goods::findOrEmpty($params['goods_id']);
  87. if($goods->isEmpty()){
  88. throw new \Exception('产品不存在!');
  89. }
  90. if($goods->property_activity_id > 0){
  91. $propertyActivity = PropertyActivity::findOrEmpty($goods->property_activity_id);
  92. if($propertyActivity->isEmpty()){
  93. throw new \Exception('活动不存在');
  94. }
  95. if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
  96. throw new \Exception('活动未开始');
  97. }
  98. if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
  99. throw new \Exception('活动已结束');
  100. }
  101. // 生成代理单 user_info
  102. // remark address property_head_id householder_name householder_mobile
  103. $result = PropertyOrderLogic::add(array_merge($params,[
  104. 'property_head_id' => $propertyActivity['property_head_id'],
  105. 'householder_mobile' => $params['user_info']['mobile'],
  106. 'householder_name' => $params['user_info']['real_name'],
  107. 'address' => $params['address'],
  108. 'remark' => '',
  109. 'order_status' => 1,
  110. 'work_id' => $serviceOrder['work_id'],
  111. ]));
  112. if($result === false){
  113. throw new \Exception('生成代理单失败');
  114. }
  115. }
  116. return true;
  117. }catch(\Exception $e){
  118. throw new \Exception($e->getMessage());
  119. }
  120. }
  121. }