ActivityLogic.php 12 KB

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