ActivityLogic.php 4.8 KB

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