| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace app\api\logic;
- use app\adminapi\logic\property\PropertyUserLogic;
- use app\common\logic\BaseLogic;
- use app\common\model\coupon\CouponGoods;
- use app\common\model\coupon\CouponRules;
- use app\common\model\coupon\UserCoupon;
- use app\common\model\goods\Goods;
- use app\common\model\goods_category\GoodsCategory;
- use app\common\model\property\PropertyActivity;
- use app\common\model\property\PropertyHead;
- use app\common\model\property\PropertyOrder;
- use app\common\model\property\PropertyUser;
- use app\common\model\recharge\RechargeOrder;
- use app\common\model\user\User;
- use app\common\model\works\ServiceWork;
- use think\facade\Db;
- use think\facade\Log;
- /**
- * 活动逻辑处理
- * Class ActivityLogic
- * @package app\api\logic
- */
- class ActivityLogic extends BaseLogic
- {
- public static function getHomepageByActivityId($params){
- $res = [];
- try{
- $propertyActivity = PropertyActivity::with(['propertyHeadInfo'])->where('id',$params['property_activity_id'])->findOrEmpty();
- if($propertyActivity->isEmpty()){
- throw new \Exception('活动不存在');
- }
- if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
- throw new \Exception('活动未开始');
- }
- if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
- throw new \Exception('活动已结束');
- }
- Log::info('getHomepageByActivityId params:'.json_encode($params));
- if($params['user_id']){
- // property_head_id householder_mobile householder_name address
- $userInfo = User::where('id',$params['user_id'])->findOrEmpty();
- if (!$userInfo->isEmpty()) {
- // 检查/注册
- Log::info('getHomepageByActivityId params-102:'.json_encode([$userInfo]));
- PropertyUserLogic::getPropertyUserIdByMobile([
- 'householder_mobile' => $userInfo['mobile'],
- 'householder_name' => $userInfo['real_name'],
- 'address' => '',
- 'property_head_id' => $propertyActivity['property_head_id']
- ]);
- }
- }
- $res['activity_info'] = $propertyActivity->toArray();
- if(in_array($res['activity_info']['url_page'],['adver','adver1','adver2'])){
- array_multisort(array_column($res['activity_info']['block_data'], 'sort'), SORT_DESC, $res['activity_info']['block_data']);
- foreach ($res['activity_info']['block_data'] as &$v){
- array_multisort(array_column($v['goods'], 'recommend_weight'), SORT_DESC, $v['goods']);
- if(!empty($v['goods'])){
- foreach ($v['goods'] as &$item){
- $item['service_fee'] = $item['base_service_fee'];
- }
- }
- $v['goods'] = self::groupArrays($v['goods']);
- }
- $res['coupons'] = CouponRules::with(['couponCategoryOne'])->where('property_activity_id',$params['property_activity_id'])
- ->select()
- ->toArray();
- foreach ($res['coupons'] as &$coupon) {
- $coupon['goods_category_id'] = $coupon['couponCategoryOne']['goods_category_id'];
- }
- $res['goods'] = Goods::where('property_activity_id',$params['property_activity_id'])
- ->where('is_agent',1)
- ->visible(['id','goods_name','goods_image','base_service_fee','service_total','service_fee','goods_type'])
- ->order('is_recommend desc')
- ->select()
- ->toArray();
- // 临时添加
- foreach ($res['goods'] as &$good){
- $good['service_fee'] = $good['base_service_fee'];
- }
- }else{
- $res['block_data_structure'] = self::blockDataStructure($res['activity_info']['url_page'],$res['activity_info']['block_data']);
- $res['coupons'] = self::couponDataStructure($params['property_activity_id']);
- }
- return $res;
- }catch(\Exception $e){
- throw new \Exception($e->getMessage());
- }
- }
- public static function blockDataStructure(string $url_page,array $data): array {
- if (empty($data)) return [];
- array_multisort(array_column($data, 'sort'), SORT_DESC, $data);
- foreach ($data as &$v){
- array_multisort(array_column($v['goods'], 'recommend_weight'), SORT_DESC, $v['goods']);
- if(!empty($v['goods'])){
- foreach ($v['goods'] as &$item){
- $reserved_fields = $item;
- $item = Goods::findOrEmpty($item['id'])->toArray();
- $item['code'] = '';
- $goods_coupon_id = CouponGoods::where(['goods_id'=>$item['id']])->value('coupon_id')??0;
- if($goods_coupon_id){
- $coupon = CouponRules::where(['id'=>$goods_coupon_id])->field('code,server_category_name')->findOrEmpty();
- $coupon && $item['server_category_name'] = nl2br($coupon['server_category_name'],false);
- $coupon && $item['code'] = $coupon['code'];
- }
- foreach ($reserved_fields as $field => $value) {
- !empty($value) && $item[$field] = $value;
- }
- // 临时改价 11-28
- $item['service_fee'] = $item['base_service_fee'];
- }
- }
- // 不同模板 - 板块内商品数据结构不同
- switch ($url_page){
- case 'adver6':
- case 'adver5':
- break;
- default:
- $v['goods'] = self::groupArrays($v['goods']);
- break;
- }
- }
- return $data;
- }
- public static function couponDataStructure(int $property_activity_id): array {
- $couponIds = self::getDataIdsByType($property_activity_id, 'coupon_data');
- $coupons = CouponRules::with(['couponCategoryOne'])
- ->where('id','in',$couponIds)
- ->select()
- ->toArray();
- foreach ($coupons as &$coupon) {
- $coupon['goods_category_id'] = $coupon['couponCategoryOne']['goods_category_id'];
- }
- return $coupons??[];
- }
- public static function groupArrays($array) {
- $result = []; $groupSize = 2;
- $totalElements = count($array);
- for ($i = 0; $i < $totalElements; $i += $groupSize)
- { $end = min($i + $groupSize, $totalElements);
- $result[] = array_slice($array, $i, $end - $i);
- }
- return $result;
- }
- public static function createPropertyOrder($params,$serviceOrder){
- try{
- // 代理活动
- if(isset($params['property_activity_id']) && !empty($params['property_activity_id'])){
- $propertyActivity = PropertyActivity::findOrEmpty($params['property_activity_id']);
- if($propertyActivity->isEmpty()){
- throw new \Exception('活动不存在');
- }
- if(!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()){
- throw new \Exception('活动未开始');
- }
- if(!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()){
- throw new \Exception('活动已结束');
- }
- // 生成代理单 user_info
- // remark address property_head_id householder_name householder_mobile
- $result = PropertyOrderLogic::add(array_merge($params,[
- 'property_head_id' => $propertyActivity['property_head_id'],
- 'householder_mobile' => $params['user_info']['mobile'],
- 'householder_name' => $params['user_info']['real_name'],
- 'address' => $params['address'],
- 'remark' => '',
- 'order_status' => 1,
- 'work_id' => $serviceOrder['work_id'],
- ]));
- if($result === false){
- throw new \Exception('生成代理单失败');
- }
- }elseif (isset($params['goods_id']) && !empty($params['goods_id'])) {
- // 判断商品是否为 代理活动商品
- $goods = Goods::findOrEmpty($params['goods_id']);
- if ($goods->isEmpty()) {
- throw new \Exception('产品不存在!');
- }
- if ($goods->property_activity_id > 0) {
- $propertyActivity = PropertyActivity::findOrEmpty($goods->property_activity_id);
- if ($propertyActivity->isEmpty()) {
- throw new \Exception('活动不存在');
- }
- if (!empty($propertyActivity->getData('activity_start_time')) && $propertyActivity->getData('activity_start_time') > time()) {
- throw new \Exception('活动未开始');
- }
- if (!empty($propertyActivity->getData('activity_end_time')) && $propertyActivity->getData('activity_end_time') < time()) {
- throw new \Exception('活动已结束');
- }
- // 生成代理单 user_info
- // remark address property_head_id householder_name householder_mobile
- $result = PropertyOrderLogic::add(array_merge($params, [
- 'property_head_id' => $propertyActivity['property_head_id'],
- 'householder_mobile' => $params['user_info']['mobile'],
- 'householder_name' => $params['user_info']['real_name'],
- 'address' => $params['address'],
- 'remark' => '',
- 'order_status' => 1,
- 'work_id' => $serviceOrder['work_id'],
- ]));
- if ($result === false) {
- throw new \Exception('生成代理单失败');
- }
- }else{
- //判断当前用户是否是物业代理用户
- $property_user = PropertyUser::where(['user_id'=>$params['user_id']])->findOrEmpty();
- if(!$property_user->isEmpty()){
- $property_order = PropertyOrder::where(['property_user_id'=>$property_user['id'],'order_status'=>0,'work_id'=>0])->findOrEmpty();
- if(!$property_order->isEmpty()){
- $property_head = PropertyHead::where('id',$property_order->property_head_id)->findOrEmpty();
- if(!$property_head->isEmpty() && (((time()-strtotime($property_order['create_time']))<=($property_head['bind_date']*24*3600)) || ($property_head['bind_date']==0))){
- //绑定物业代理
- $property_order->work_id = $serviceOrder['work_id'];
- $property_order->order_status = 1;
- $property_order->save();
- }
- }
- }
- }
- }
- return true;
- }catch(\Exception $e){
- throw new \Exception($e->getMessage());
- }
- }
- public static function getDataIdsByType($property_activity_id, $type): array
- {
- $ids = [];
- $activityInfo = PropertyActivity::where('id',$property_activity_id)->findOrEmpty();
- if(!$activityInfo->isEmpty()){
- $activityInfo = $activityInfo->toArray();
- if($type === 'block_data'){
- foreach ($activityInfo['block_data'] as &$item) {
- $ids = array_merge($ids,array_column($item['goods'], 'id'));
- }
- }
- if($type === 'coupon_data'){
- $activityInfo['coupon_data'] && $ids = array_merge($ids,array_column($activityInfo['coupon_data'], 'id'));
- }
- }
- return $ids??[0];
- }
- /** 优惠券逆向为活动工单+代理订单
- * @param $params
- * @param $serviceOrder
- * @return true
- * @throws \Exception
- */
- public static function ReverseCouponToActivity($params){
- try{
- $serviceOrder = ServiceWork::where('id',$params['id'])->where('property_activity_id',0)->findOrEmpty();
- if($serviceOrder->isEmpty()){
- return true;
- }
- // 查询优惠券id
- $coupon_ids = RechargeOrder::where('work_id',$params['id'])->where('coupon_id','>',0)->column('coupon_id');
- if(empty($coupon_ids)){
- return true;
- }
- $property_activity_id = UserCoupon::where('user_id',$serviceOrder->user_id)->whereIn('id',$coupon_ids)->where('property_activity_id','>',0)->value('property_activity_id');
- if(empty($property_activity_id)){
- return true;
- }
- $propertyActivity = PropertyActivity::findOrEmpty($property_activity_id);
- if($propertyActivity->isEmpty()){
- return true;
- }
- $user_info = User::findOrEmpty($serviceOrder->user_id);
- $propertyOrder = PropertyOrder::where('work_id',$serviceOrder->id)->findOrEmpty();
- if($propertyOrder->isEmpty()){
- // 生成代理单
- // householder_mobile property_head_id householder_name address remark order_status work_id
- $result = PropertyOrderLogic::add([
- 'property_head_id' => $propertyActivity['property_head_id'],
- 'householder_mobile' => $user_info['mobile'],
- 'householder_name' => $user_info['real_name'],
- 'address' => $serviceOrder->address,
- 'remark' => '优惠券逆向活动工单',
- 'order_status' => 3,
- 'work_id' => $serviceOrder->id,
- ]);
- if($result === false){
- throw new \Exception('优惠券逆向活动工单失败');
- }
- $serviceOrder->property_activity_id = $property_activity_id;
- $serviceOrder->save();
- }
- return true;
- }catch(\Exception $e){
- throw new \Exception($e->getMessage());
- }
- }
- }
|