ActivityLogic.php 15 KB

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