ActivityLogic.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  52. $res['coupons'] = CouponRules::with(['couponCategoryOne'])->where('property_activity_id',$params['property_activity_id'])
  53. ->select()
  54. ->toArray();
  55. foreach ($res['coupons'] as &$coupon) {
  56. $coupon['goods_category_id'] = $coupon['couponCategoryOne']['goods_category_id'];
  57. }
  58. $res['goods'] = Goods::where('property_activity_id',$params['property_activity_id'])
  59. ->where('is_agent',1)
  60. ->visible(['id','goods_name','goods_image','base_service_fee','service_total','service_fee','goods_type'])
  61. ->order('is_recommend desc')
  62. ->select()
  63. ->toArray();
  64. return $res;
  65. }catch(\Exception $e){
  66. throw new \Exception($e->getMessage());
  67. }
  68. }
  69. public static function createPropertyOrder($params,$serviceOrder){
  70. try{
  71. // 判断商品是否为 代理活动商品
  72. $goods = Goods::findOrEmpty($params['goods_id']);
  73. if($goods->isEmpty()){
  74. throw new \Exception('产品不存在!');
  75. }
  76. if($goods->property_activity_id > 0){
  77. $propertyActivity = PropertyActivity::findOrEmpty($goods->property_activity_id);
  78. if($propertyActivity->isEmpty()){
  79. throw new \Exception('活动不存在');
  80. }
  81. if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
  82. throw new \Exception('活动未开始');
  83. }
  84. if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
  85. throw new \Exception('活动已结束');
  86. }
  87. // 生成代理单 user_info
  88. // remark address property_head_id householder_name householder_mobile
  89. $result = PropertyOrderLogic::add(array_merge($params,[
  90. 'property_head_id' => $propertyActivity['property_head_id'],
  91. 'householder_mobile' => $params['user_info']['mobile'],
  92. 'householder_name' => $params['user_info']['real_name'],
  93. 'address' => $params['address'],
  94. 'remark' => '',
  95. 'order_status' => 1,
  96. 'work_id' => $serviceOrder['work_id'],
  97. ]));
  98. if($result === false){
  99. throw new \Exception('生成代理单失败');
  100. }
  101. }
  102. return true;
  103. }catch(\Exception $e){
  104. throw new \Exception($e->getMessage());
  105. }
  106. }
  107. }