| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app\controller\manager; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\exception\RepositoryException; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\model\CouponMain; | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | use app\model\Business as BusinessModel; | 
					
						
							| 
									
										
										
										
											2021-12-22 15:45:48 +08:00
										 |  |  | use app\model\Member as MemberModel; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use app\repository\BusinessRepository; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | use app\repository\CouponRepository; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use app\validate\CouponRelease; | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | use app\validate\CouponUsingRule; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | use think\facade\Config; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | use think\facade\Db; | 
					
						
							|  |  |  | use think\response\Json; | 
					
						
							|  |  |  | use think\response\View; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use think\facade\Config as CConfig; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 优惠券相关 | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | class Coupon extends Base | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     protected function initialize() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::initialize(); // TODO: Change the autogenerated stub
 | 
					
						
							|  |  |  |         CConfig::load('extra/distribution_proportion', 'distributionProportion'); | 
					
						
							|  |  |  |         $this->data['distributionProportion'] = config('distributionProportion'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 列表 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function index() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->request->isPost()) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |             $repo = new CouponMain(); | 
					
						
							|  |  |  |             $repo = CouponRepository::getInstance($repo); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             $keyword = $this->request->param('keyword/s', ''); | 
					
						
							| 
									
										
										
										
											2021-12-16 17:41:17 +08:00
										 |  |  |             $on_shelf = $this->request->param('on_shelf',CouponMain::on_shelf_off); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             $start_time = $this->request->param('start_time',); | 
					
						
							|  |  |  |             $end_time = $this->request->param('end_time'); | 
					
						
							|  |  |  |             $page = $this->request->param('page/d', 1); | 
					
						
							|  |  |  |             $size = $this->request->param('size/d', 30); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $whereMap = []; | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |             $orders = ['a.sort' => 'desc', "a.id" => "desc"]; | 
					
						
							|  |  |  |             if (in_array($on_shelf, [CouponMain::COMMON_ON, CouponMain::COMMON_OFF])) { | 
					
						
							|  |  |  |                 $whereMap[] = ['a.on_shelf', '=', $on_shelf]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (!empty($start_time)) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |                 $whereMap[] = ['a.start_time', '>= TIME' , $start_time]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (!empty($end_time)) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |                 $whereMap[] = ['a.end_time', '<= TIME', $end_time]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (!empty($keyword)) { | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |                 $whereMap[] = ['a.name|b.business_name', 'like', "%" . $keyword . "%"]; | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-23 10:23:10 +08:00
										 |  |  |             $list =$repo->couponMainList($whereMap,$page,$size,$orders,$this->auth); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             $time = time(); | 
					
						
							|  |  |  |             $list['list']->each(function ($item) use ($time) { | 
					
						
							|  |  |  |                 if (strtotime($item->start_time) > $time) { | 
					
						
							|  |  |  |                     $item->state_text = '<span >未开始</span>'; | 
					
						
							|  |  |  |                 } else if ((strtotime($item->start_time) < $time) && (strtotime($item->end_time) > $time)) { | 
					
						
							|  |  |  |                     $item->state_text = '<span  class="f_green">进行中</span>'; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $item->state_text = '<span  class="f_red">已过期</span>'; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-20 17:53:03 +08:00
										 |  |  |                 //渠道商名称
 | 
					
						
							| 
									
										
										
										
											2021-12-22 15:45:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 17:53:03 +08:00
										 |  |  |                 if(isset($item->business) && $item->business && !empty($item->business->agency_code)){ | 
					
						
							| 
									
										
										
										
											2021-12-22 15:45:48 +08:00
										 |  |  |                     $item->agency_text = MemberModel::whereRaw("FIND_IN_SET('".MemberModel::ANENT_ROLE_ID."',roles)"  ) | 
					
						
							|  |  |  |                         ->where("business_code",$item->business->agency_code)->value("nickname",""); | 
					
						
							| 
									
										
										
										
											2021-12-20 17:53:03 +08:00
										 |  |  |                 }else{ | 
					
						
							|  |  |  |                     $item->agency_text = ''; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             }); | 
					
						
							|  |  |  |             return $this->json(0, 'success', $list); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->view(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 上架状态 0上架 1下架 | 
					
						
							|  |  |  |      * */ | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |     public function shelf() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $id = input("id/d", 0); | 
					
						
							|  |  |  |         $on_shelf = input("on_shelf/d", 1); | 
					
						
							|  |  |  |         $model = new CouponMain(); | 
					
						
							|  |  |  |         $repo = CouponRepository::getInstance($model); | 
					
						
							|  |  |  |         $coupon = $repo->findById($id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($coupon)) { | 
					
						
							|  |  |  |             return $this->json(4001, "优惠券不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |         if (!in_array($on_shelf, [CouponMain::COMMON_OFF, CouponMain::COMMON_ON])) { | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |             return $this->json(4001, "状态错误"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |         $coupon->save(["on_shelf" => $on_shelf]); | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  |         return $this->json(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |      * 持有详细 | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function info() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->request->isPost()) { | 
					
						
							|  |  |  |             $model = new CouponMain(); | 
					
						
							|  |  |  |             $repo = CouponRepository::getInstance($model); | 
					
						
							|  |  |  |             $id = input("id/d"); | 
					
						
							|  |  |  |             $keyword = $this->request->param('keyword/s', ''); | 
					
						
							|  |  |  |             $page = $this->request->param('page/d', 1); | 
					
						
							|  |  |  |             $size = $this->request->param('size/d', 30); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $list = $repo->couponMainHasList($id, $keyword, $page, $size); | 
					
						
							|  |  |  |             $time = time(); | 
					
						
							|  |  |  |             $list["list"]->each(function ($item) use ($time) { | 
					
						
							|  |  |  |                 if (strtotime($item['end_time']) < $time) { | 
					
						
							|  |  |  |                     $item->time_state = '已过期'; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $item->time_state = '未过期'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             return $this->json(0, 'success', $list); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $id = input("id/d"); | 
					
						
							|  |  |  |         $model = new CouponMain(); | 
					
						
							|  |  |  |         $repo = CouponRepository::getInstance($model); | 
					
						
							|  |  |  |         $coupon = $repo->getModel()->with(["business" => function ($query) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $query | 
					
						
							|  |  |  |                 ->field("code,business_name,business_subtitle,type") | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |                 ->with('category'); | 
					
						
							|  |  |  |         }, "couponType"])->where("id", $id)->find(); | 
					
						
							|  |  |  |         if (empty($coupon)) { | 
					
						
							|  |  |  |             return $this->error("优惠券不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->data["coupon"] = $coupon; | 
					
						
							|  |  |  |         return $this->view(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 发布优惠券 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function release() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->request->isPost()) { | 
					
						
							|  |  |  |             $data = input("item/a", []); | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |             $usingRule = input("using_rule/a", []); | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (empty($business)) { | 
					
						
							|  |  |  |                 return $this->json(4001, '商家不存在'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-20 16:13:30 +08:00
										 |  |  |             if($business->enable != BusinessModel::COMMON_OFF){ | 
					
						
							|  |  |  |                 return $this->json(4001, "商家已禁用"); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $validate = new CouponRelease(); | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             //普通商家要验证扣除金额
 | 
					
						
							|  |  |  |             if (!$validate->scene($business["model"] ==BusinessModel::model_ordinary?"ordinary":"")->check($data)) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 return $this->json(4001, $validate->getError()); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $usingRuleValidate = new CouponUsingRule(); | 
					
						
							|  |  |  |             if (!$usingRuleValidate->check($usingRule)) { | 
					
						
							|  |  |  |                 return $this->json(4001, $usingRuleValidate->getError()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             //持有限量
 | 
					
						
							|  |  |  |             Config::load("extra/wechat","wechat"); | 
					
						
							|  |  |  |             $hasCouponMax = config("wechat.hasCouponMax")??0; | 
					
						
							|  |  |  |             if ($hasCouponMax > 0) { | 
					
						
							|  |  |  |                 $hasCouponCount = CouponRepository::getInstance()->getBusinessOnShelfOnCount($business->code); | 
					
						
							|  |  |  |                 if ($hasCouponCount > $hasCouponMax) { | 
					
						
							|  |  |  |                     return $this->json(4001, "商家持有商家优惠券不能超过{$hasCouponMax}"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 13:37:55 +08:00
										 |  |  |             $data['business_type']          = $business['type']; | 
					
						
							|  |  |  |             $data['business_name']          = $business['business_name']; | 
					
						
							|  |  |  |             $data['lng']                    = $business['lng']; | 
					
						
							|  |  |  |             $data['lat']                    = $business['lat']; | 
					
						
							|  |  |  |             $data['business_circle_id']     = $business['business_circle_id']; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $type = CouponRepository::getInstance()->getCouponTypeAll(); | 
					
						
							|  |  |  |             $type = array_column($type->toArray(), null, "id"); | 
					
						
							|  |  |  |             if (!isset($type[$data['type']])) { | 
					
						
							|  |  |  |                 return $this->json(4001, '优惠券类型不存在'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $data['commission_agency'] = input("item.commission_agency/d", 0); | 
					
						
							|  |  |  |             $data['commission_admin'] = input("item.commission_admin/d", 0); | 
					
						
							|  |  |  |             $data['commission_consumer'] = input("item.commission_consumer/d", 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $totalC = $data['commission_agency'] + $data['commission_admin'] + $data['commission_consumer']; | 
					
						
							|  |  |  |             if ($totalC != 100) { | 
					
						
							|  |  |  |                 return $this->json(5002, "分配比例总和不等于100"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             //保留两位小数
 | 
					
						
							|  |  |  |             $data['money'] = floor($data['money'] * 100) / 100; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $totalMoney = $data['money'] * $data['count']; | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             //未领取的优惠券
 | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($business["code"]); | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  |             if($business["model"] == BusinessModel::model_ordinary){ | 
					
						
							|  |  |  |                 if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) { | 
					
						
							|  |  |  |                     return $this->json(4001, '商家余额不足'); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data['type_name'] = $type[$data['type']]['name']; | 
					
						
							|  |  |  |             $date = date("Y-m-d H:i:s"); | 
					
						
							|  |  |  |             $data['create_time'] = $date; | 
					
						
							|  |  |  |             $data['update_time'] = $date; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             Db::startTrans(); | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |                 CouponRepository::getInstance()->releaseCouponMain($data, $totalMoney, $usingRule); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 Db::commit(); | 
					
						
							|  |  |  |                 return $this->json(); | 
					
						
							|  |  |  |             } catch (RepositoryException $e) { | 
					
						
							|  |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5001, "发布失败" . $e->getMessage()); | 
					
						
							|  |  |  |             } catch (\think\Exception $e) { | 
					
						
							|  |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5002, "发布失败" . $e->getMessage()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->data['business'] = BusinessRepository::getInstance()->getBusinessAll(); | 
					
						
							|  |  |  |         $this->data['type'] = CouponRepository::getInstance()->getCouponTypeAll(); | 
					
						
							|  |  |  |         return $this->view('add'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 修改 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Json|View | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function edit() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |         $couponMain = CouponMain::findOne(["id" => input("id/d", 0)], [], function ($q) { | 
					
						
							|  |  |  |             return $q->with("usingRule"); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |         if ($this->request->isPost()) { | 
					
						
							|  |  |  |             $data = input("item/a", []); | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |             $usingRule = input("using_rule/a", []); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             if (empty($couponMain)) { | 
					
						
							|  |  |  |                 return $this->json(4001, "优惠券不存在"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $validate = new CouponRelease(); | 
					
						
							|  |  |  |             if (!$validate->scene("edit")->check($data)) { | 
					
						
							|  |  |  |                 return $this->json(4001, $validate->getError()); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $usingRuleValidate = new CouponUsingRule(); | 
					
						
							|  |  |  |             if (!$usingRuleValidate->check($usingRule)) { | 
					
						
							|  |  |  |                 return $this->json(4001, $usingRuleValidate->getError()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); | 
					
						
							|  |  |  |             if (empty($business)) { | 
					
						
							|  |  |  |                 return $this->json(4001, '商家不存在'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             //更新经纬度
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |             $data['business_type'] = $business['type']; | 
					
						
							|  |  |  |             $data['business_name'] = $business['business_name']; | 
					
						
							|  |  |  |             $data['lng'] = $business['lng']; | 
					
						
							|  |  |  |             $data['lat'] = $business['lat']; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data['commission_agency'] = input("item.commission_agency/d", 0); | 
					
						
							|  |  |  |             $data['commission_admin'] = input("item.commission_admin/d", 0); | 
					
						
							|  |  |  |             $data['commission_consumer'] = input("item.commission_consumer/d", 0); | 
					
						
							|  |  |  |             $totalC = $data['commission_agency'] + $data['commission_admin'] + $data['commission_consumer']; | 
					
						
							|  |  |  |             if ($totalC != 100) { | 
					
						
							|  |  |  |                 return $this->json(5002, "分配比例总和不等于100"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data['update_time'] = date("Y-m-d H:i:s"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Db::startTrans(); | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 $couponMain->save($data); | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |                 $couponMain->usingRule->save($usingRule); | 
					
						
							| 
									
										
										
										
											2021-12-22 13:53:57 +08:00
										 |  |  |                 CouponRepository::getInstance()->getModel()->where("coupon_id",$couponMain->id)->update(["end_time"=>$data["end_time"]." 00:00:00"]); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 Db::commit(); | 
					
						
							|  |  |  |                 return $this->json(); | 
					
						
							|  |  |  |             } catch (RepositoryException $e) { | 
					
						
							|  |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5001, "发布失败" . $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |             } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |                 Db::rollback(); | 
					
						
							|  |  |  |                 return $this->json(5002, "发布失败" . $e->getMessage()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |         if (empty($couponMain)||empty($couponMain['usingRule'])) { | 
					
						
							|  |  |  |             return $this->error("优惠券不存在或关联属性不存在"); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         $this->data['business'] = BusinessRepository::getInstance()->getBusinessAll(); | 
					
						
							|  |  |  |         $this->data['type'] = CouponRepository::getInstance()->getCouponTypeAll(); | 
					
						
							|  |  |  |         $this->data['item'] = $couponMain; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->view(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:41:24 +08:00
										 |  |  | } |