data['distributionProportion'] = config('distributionProportion'); $this->data['distributionProportionUser'] = config('distributionProportionUser'); } /** * 列表 * * @return Json|View * @throws Exception */ public function index() { if ($this->request->isPost()) { $repo = new CouponMain(); $repo = CouponRepository::getInstance($repo); $keyword = $this->request->param('keyword/s', ''); $on_shelf = $this->request->param('on_shelf',CouponMain::on_shelf_off); $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 = []; $orders = ['a.sort' => 'desc', "a.id" => "desc"]; if (in_array($on_shelf, [CouponMain::COMMON_ON, CouponMain::COMMON_OFF])) { $whereMap[] = ['a.on_shelf', '=', $on_shelf]; } if (!empty($start_time)) { $whereMap[] = ['a.start_time', '>= TIME' , $start_time]; } if (!empty($end_time)) { $whereMap[] = ['a.end_time', '<= TIME', $end_time]; } if (!empty($keyword)) { $whereMap[] = ['a.name|b.business_name', 'like', "%" . $keyword . "%"]; } $list =$repo->couponMainList($whereMap,$page,$size,$orders,$this->auth); $time = time(); $list['list']->each(function ($item) use ($time) { if (strtotime($item->start_time) > $time) { $item->state_text = '未开始'; } else if ((strtotime($item->start_time) < $time) && (strtotime($item->end_time) > $time)) { $item->state_text = '进行中'; } else { $item->state_text = '已过期'; } //渠道商名称 if(isset($item->business) && $item->business && !empty($item->business->agency_code)){ $item->agency_text = MemberModel::whereRaw("FIND_IN_SET('".MemberModel::ANENT_ROLE_ID."',roles)" ) ->where("business_code",$item->business->agency_code)->value("nickname",""); }else{ $item->agency_text = ''; } }); return $this->json(0, 'success', $list); } return $this->view(); } /** * 上架状态 0上架 1下架 * */ 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, "签到券不存在"); } if (!in_array($on_shelf, [CouponMain::COMMON_OFF, CouponMain::COMMON_ON])) { return $this->json(4001, "状态错误"); } $coupon->save(["on_shelf" => $on_shelf]); return $this->json(); } /** * 持有详细 * * @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) { $query ->field("code,business_name,business_subtitle,type") ->with('category'); }, "couponType"])->where("id", $id)->find(); if (empty($coupon)) { return $this->error("签到券不存在"); } $this->data["coupon"] = $coupon; return $this->view(); } /** * 发布签到券 * * @return Json|View * @throws Exception */ public function release() { if ($this->request->isPost()) { $data = input("item/a", []); //$usingRule = input("using_rule/a", []); $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); $time = time(); if (empty($business)) { return $this->json(4001, '商家不存在'); } if($business->enable != BusinessModel::COMMON_OFF){ return $this->json(4001, "商家已禁用"); } // 指定时间段内发布的签到券个数 $TimeListModeReleaseCount = BusinessRepository::getInstance() ->getTimeListModeReleaseCount($business["code"],$business["time_limit_start"],$business["time_limit_end"]); $businessModel = $business["model"]; if($business["model"] == BusinessModel::model_time_limit){ //如果到了时间 或者发布数量到达规定 更改商家模式 if( strtotime($business["time_limit_end"] ) < $time || (($TimeListModeReleaseCount) >= $business["time_limit_release_count"]) ){ $businessModel = BusinessModel::model_ordinary; } } $validate = new CouponRelease(); //普通商家要验证扣除金额 if (!$validate->scene(($businessModel == BusinessModel::model_ordinary) ? "ordinary" : "")->check($data)) { return $this->json(4001, $validate->getError()); } //$usingRuleValidate = new CouponUsingRule(); //if (!$usingRuleValidate->check($usingRule)) { // return $this->json(4001, $usingRuleValidate->getError()); //} //持有限量 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}"); } } //检查类型 if(CouponTypeModel::checkType($data['type']) !== true){ return $this->json(4001, '签到券类型不存在'); } $type = CouponRepository::getInstance()->getCouponTypeAll(); $type = array_column($type->toArray(), null, "id"); $data['type_name'] = $type[$data['type']]['name']; $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']; $data['commission_agency'] = $this->data["distributionProportion"]["agency"]; $data['commission_admin'] = $this->data["distributionProportion"]["admin"]; $data['commission_consumer'] = $this->data["distributionProportion"]["consumer"]; $data['commission_dis_admin'] = $this->data["distributionProportionUser"]["admin"]; $data['commission_dis_distribution'] = $this->data["distributionProportionUser"]["distribution"]; $data['commission_dis_distributed'] = $this->data["distributionProportionUser"]["distributed"]; $totalCommission = $data['commission_agency'] + $data['commission_admin'] + $data['commission_consumer']; if ($totalCommission != 100) { return $this->json(5002, "分配比例总和不等于100"); } $totalCommissionDis = $data['commission_dis_admin'] + $data['commission_dis_distribution'] + $data['commission_dis_distributed']; if ($totalCommissionDis != 100) { return $this->json(5002, "(开启分销)分配比例总和不等于100"); } //保留两位小数 $data['deduction_money'] = floor($data['deduction_money'] * 100) / 100; //如果开启分销 添加操作 处理扣除金额 if($data["is_distribution"] == CouponMain::COMMON_ON ){ if($business->is_assign == BusinessModel::COMMON_ON){ return $this->json(5002, "商家已经指派了渠道商,不能开启分销"); } }else{ if($business->is_assign == BusinessModel::COMMON_OFF){ return $this->json(5002, "商家没有指派渠道商,不能关闭分销"); } } //if($data['commission_dis_distribution'] < $data['commission_dis_distributed']){ //检测【分销者】部分是否最少0.3元 $distributionMoney = ($data['commission_dis_distribution'] / 100) * $data['deduction_money']; if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) { return $this->json(4002, "开启分销:【领券分销者】红包不足" . CouponMain::min_redpack_money . "元,扣除金额最低" . (CouponMain::min_redpack_money * (100 / ($data['commission_dis_distribution']))) . "元"); } //}else{ //检测【核销分销者】部分是否最少0.3元 $distributedMoney = ($data['commission_dis_distributed'] / 100) * $data['deduction_money']; if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) { return $this->json(4002, "开启分销:【核销分销者】红包不足" . CouponMain::min_redpack_money . "元,扣除金额最低" . (CouponMain::min_redpack_money * (100 / ($data['commission_dis_distributed']))) . "元"); } //} //if($data['commission_agency'] < $data['commission_consumer']){ //检测【渠道商】部分是否最少0.3元 $agencyMoney = ($data['commission_agency'] / 100) * $data['deduction_money']; if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) { return $this->json(4002, "关闭分销:【渠道商】红包不足" . CouponMain::min_redpack_money . "元,扣除金额最低" . (CouponMain::min_redpack_money * (100 / ($data['commission_agency']))) . "元"); } //}else{ //检测【消费者】部分是否最少0.3元 $consumerMoney = ($data['commission_consumer'] / 100) * $data['deduction_money']; if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) { return $this->json(4002, "关闭分销:【消费者】红包不足" . CouponMain::min_redpack_money . "元,扣除金额最低" . (CouponMain::min_redpack_money * (100 / ($data['commission_consumer']))) . "元"); } //} $totalMoney = $data['deduction_money'] * $data['count']; //未领取的签到券 $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($business["code"]); // if( // $business["model"] == BusinessModel::model_ordinary // || // ( // $business["model"] == BusinessModel::model_time_limit // && // ( // strtotime($business["time_limit_end"]) < $time // || // $TimeListModeReleaseCount >= $business["time_limit_release_count"] // ) // ) // ){ // if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) { // return $this->json(4001, '商家余额不足'); // } // } if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) { return $this->json(4001, '商家余额不足'); } $date = date("Y-m-d H:i:s"); $data['create_time'] = $date; $data['update_time'] = $date; Db::startTrans(); try { //CouponRepository::getInstance()->releaseCouponMain($data, $totalMoney, $usingRule); CouponRepository::getInstance()->releaseCouponMain($data); if($business["model"] == BusinessModel::model_time_limit){ //如果到了时间 或者发布数量到达规定 更改商家模式 if( strtotime($business["time_limit_end"] ) < $time || (($TimeListModeReleaseCount+1) >= $business["time_limit_release_count"]) ){ $business->save(["model"=>BusinessModel::model_ordinary]); } } 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->auth["roles"],$this->auth["business_code"]); $this->data['type'] = CouponRepository::getInstance()->getCouponTypeAll(); return $this->view('add'); } /** * 修改 * * @return Json|View * @throws Exception */ public function edit() { $couponMain = CouponMain::findOne(["id" => input("id/d", 0)], []); if ($this->request->isPost()) { $data = input("item/a", []); //$usingRule = input("using_rule/a", []); if (empty($couponMain)) { return $this->json(4001, "签到券不存在"); } if (!isset($couponMain->business) || empty($couponMain->business)) { return $this->json(4001, "签到券所属商家不存在"); } $validate = new CouponRelease(); if (!$validate->scene("edit")->check($data)) { return $this->json(4001, $validate->getError()); } //$usingRuleValidate = new CouponUsingRule(); //if (!$usingRuleValidate->check($usingRule)) { // return $this->json(4001, $usingRuleValidate->getError()); //} $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); if (empty($business)) { return $this->json(4001, '商家不存在'); } //检查类型 if(CouponTypeModel::checkType($data['type']) !== true){ return $this->json(4001, '签到券类型不存在'); } $type = CouponRepository::getInstance()->getCouponTypeAll(); $type = array_column($type->toArray(), null, "id"); $data['type_name'] = $type[$data['type']]['name']; //更新经纬度 $data['business_type'] = $business['type']; $data['business_name'] = $business['business_name']; $data['lng'] = $business['lng']; $data['lat'] = $business['lat']; $data['commission_agency'] = input("item.commission_agency/d", 0,"abs"); $data['commission_admin'] = input("item.commission_admin/d", 0,"abs"); $data['commission_consumer'] = input("item.commission_consumer/d", 0,"abs"); $data['commission_dis_admin'] = input("item.commission_dis_admin/d", 0,"abs"); $data['commission_dis_distribution'] = input("item.commission_dis_distribution/d", 0,"abs"); $data['commission_dis_distributed'] = input("item.commission_dis_distributed/d", 0,"abs"); $totalCommission = $data['commission_agency'] + $data['commission_admin'] + $data['commission_consumer']; if ($totalCommission != 100) { return $this->json(5002, "分配比例总和不等于100"); } $totalCommissionDis = $data['commission_dis_admin'] + $data['commission_dis_distribution'] + $data['commission_dis_distributed']; if ($totalCommissionDis != 100) { return $this->json(5002, "(开启分销)分配比例总和不等于100"); } //如果开启分销 if($data["is_distribution"] == CouponMain::COMMON_ON ){ if($business['is_assign'] == BusinessModel::COMMON_ON){ return $this->json(5002, "商家已经指派了渠道商,不能开启分销"); } }else{ if($business['is_assign'] == BusinessModel::COMMON_OFF){ return $this->json(5002, "商家没有指派渠道商,不能关闭分销"); } } //if($data['commission_dis_distribution'] < $data['commission_dis_distributed']){ //检测【领券分销者】部分是否最少0.3元 $distributionMoney = ($data['commission_dis_distribution']/100) * $couponMain['deduction_money']; if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) { return $this->json(4002, "开启分销:【领券分销者】红包不足" .CouponMain::min_redpack_money ."元,【领券分销者】占比最低" . (round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2)) . "%" ); } //}else{ //检测【核销分销者】部分是否最少0.3元 $distributedMoney = ($data['commission_dis_distributed']/100) * $couponMain['deduction_money']; if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) { return $this->json(4002, "开启分销:【核销分销者】红包不足" .CouponMain::min_redpack_money ."元,【核销分销者】占比最低" . (round((CouponMain::min_redpack_money / ($data['deduction_money'])) * 100, 2)) . "%"); } //} //if($data['commission_consumer'] < $data['commission_agency']){ //检测 【消费者】部分是否最少0.3元 $consumerMoney = ($data['commission_consumer']/100) * $couponMain['deduction_money']; if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) { return $this->json(4002, "关闭分销:【消费者】红包不足" .CouponMain::min_redpack_money ."元,【消费者】占比最低" . (round((CouponMain::min_redpack_money / ($couponMain['deduction_money'])) * 100, 2)) . "%"); } //}else{ //检测 【渠道商】部分是否最少0.3元 $agencyMoney = ($data['commission_agency']/100) * $couponMain['deduction_money']; if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) { return $this->json(4002, "关闭分销:【渠道商】红包不足" .CouponMain::min_redpack_money ."元,【渠道商】占比最低" . (round((CouponMain::min_redpack_money / ($couponMain['deduction_money'])) * 100, 2)) . "%"); } //} $data['update_time'] = date("Y-m-d H:i:s"); Db::startTrans(); try { $couponMain->save($data); //$couponMain->usingRule->save($usingRule); CouponRepository::getInstance()->getModel()->where("coupon_id",$couponMain->id)->update(["end_time"=>$data["end_time"]." 00:00:00"]); Db::commit(); return $this->json(); } catch (RepositoryException $e) { Db::rollback(); return $this->json(5001, "发布失败" . $e->getMessage()); } catch (Exception $e) { Db::rollback(); return $this->json(5002, "发布失败" . $e->getMessage()); } } if (empty($couponMain)) { return $this->error("签到券不存在"); } $this->data['business'] = BusinessRepository::getInstance()->getBusinessAll($this->auth["roles"],$this->auth["business_code"]); $this->data['type'] = CouponRepository::getInstance()->getCouponTypeAll(); $this->data['item'] = $couponMain; return $this->view(); } /** * 下载小程序二维码 * */ public function downloadQrCode() { $id = input("id/d"); $couponMain = CouponMain::findById($id); if(empty($couponMain)){ return $this->json(4001,"签到券不存在"); } $qrData = $this->request->domain() . "/coupon_info?id={$id}&business_code={$couponMain['business_code']}" ; $w = 3000;//尺寸 $logoImg = app()->getRootPath().'public/static/images/icon-logo.jpg'; $result = Builder::create() ->writer(new PngWriter()) ->writerOptions([]) ->data($qrData) ->encoding(new Encoding('UTF-8')) ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) ->size($w) ->margin(10) ->roundBlockSizeMode(new RoundBlockSizeModeMargin()) ->logoPath($logoImg) ->logoResizeToHeight(ceil($w/6)) ->logoResizeToWidth(ceil($w/6)) ->logoPunchoutBackground(true) ->build(); header( "Content-type: image/jpg" ); $path = "/storage/coupon/{$id}.jpg"; $result->saveToFile( public_path() . $path); return download(public_path() . $path,$couponMain['name']); } /** * 设置分销开关 * * */ public function setDistribution() { $ids = input("ids/a", []); $isDistribution = input("is_distribution/d"); $couponMains = CouponMain::findList([["id", "in", $ids]], [],1,0,function ($q){ return $q->with("business"); }); foreach ($couponMains["list"] as $key=> $item) { if (!isset($item->business) || empty($item->business)) { return $this->json(4001,"【{$item["name"]}】所属商家不存在"); } //如果开启分销操作 if ($isDistribution == CouponMain::COMMON_ON) { if($item->business->is_assign == BusinessModel::COMMON_ON){ return $this->json(4001, "【{$item["name"]}】所属商家【{$item->business->business_name}】已指派渠道商不能开启分销"); } }else{ if($item->business->is_assign == BusinessModel::COMMON_OFF){ return $this->json(4001, "【{$item["name"]}】所属商家【{$item->business->business_name}】未指派渠道商不能关闭分销"); } } if($item['commission_dis_distribution'] < $item['commission_dis_distributed'] ){ //检测【领券分销者】部分是否最少0.3元 $distributionMoney = ($item['commission_dis_distribution'] / 100) * $item['deduction_money']; if ($distributionMoney != 0 && $distributionMoney < CouponMain::min_redpack_money) { return $this->json(4002, "【{$item['name']}】开启分销后:【领券分销者】红包不足" . CouponMain::min_redpack_money . "元,【领券分销者】占比最低" . (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) . "%" ); } }else{ //检测【核销分销者】部分是否最少0.3元 $distributedMoney = ($item['commission_dis_distributed'] / 100) * $item['deduction_money']; if ($distributedMoney != 0 && $distributedMoney < CouponMain::min_redpack_money) { return $this->json(4002, "【{$item['name']}】开启分销后:【核销分销者】红包不足" . CouponMain::min_redpack_money . ",【核销分销者】占比最低" . (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) . "%"); } } if($item['commission_agency'] < $item['commission_consumer']){ //检测【渠道商】部分是否最少0.3元 $agencyMoney = ($item['commission_agency'] / 100) * $item['deduction_money']; if ($agencyMoney != 0 && $agencyMoney < CouponMain::min_redpack_money) { return $this->json(4002, "【{$item['name']}】开启分销后:【渠道商】红包不足" . CouponMain::min_redpack_money . "元,【渠道商】占比最低" . (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) . "%" ); } }else{ //检测【消费者】部分是否最少0.3元 $consumerMoney = ($item['commission_consumer'] / 100) * $item['deduction_money']; if ($consumerMoney != 0 && $consumerMoney < CouponMain::min_redpack_money) { return $this->json(4002, "【{$item['name']}】开启分销后:【消费者】红包不足" . CouponMain::min_redpack_money . ",【消费者】占比最低" . (round((CouponMain::min_redpack_money / ($item['deduction_money'])) * 100, 2)) . "%"); } } } Db::startTrans(); try { CouponMain::whereIn("id",$ids)->update(["is_distribution"=>$isDistribution]); Db::commit(); return $this->json(); } catch (Exception $e) { Db::rollback(); return $this->json(5001, "设置失败".$e->getMessage()); } } }