123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- <?php
- namespace app\api\logic;
-
- use app\common\basics\Logic;
- use app\common\enum\FootprintEnum;
- use app\common\model\coupon\Coupon;
- use app\common\model\coupon\CouponGoods;
- use app\common\model\coupon\CouponList;
- use app\common\model\goods\Goods;
- use app\common\model\Cart;
- use app\common\model\goods\GoodsItem;
-
- class CouponLogic extends Logic
- {
- public static function getCouponList($get)
- {
-
- $where = [
- ['del', '=', 0],
- ['status', '=', 1],
- ['send_time_start', '<=', time()],
- ['send_time_end', '>=', time()],
- ['get_type', '=', 1],
- ];
-
- if(isset($get['goods_id'])) {
- $shop_id = Goods::where('id', $get['goods_id'])->value('shop_id');
- $where[] = ['shop_id', '=', $shop_id];
- }
-
- if(isset($get['shop_id'])) {
- $where[] = ['shop_id', '=', $get['shop_id']];
- }
-
-
- if(isset($get['type']) && !empty($get['type'])) {
- switch ($get['type']) {
- case 'shop':
- $where[] = ['shop_id', '<>', 0];
- break;
- case 'platform':
- $where[] = ['shop_id', '=', 0];
- break;
- }
- }
-
- $lists = Coupon::field(true)
- ->with('shop')
- ->where($where)
- ->select()
- ->toArray();
-
-
- foreach($lists as $key => &$item) {
- if($item['send_total_type'] == 2) {
-
- $already_issue = CouponList::where(['del'=>0, 'coupon_id'=>$item['id']])->count();
- if($already_issue >= $item['send_total']) {
-
- unset($lists[$key]);
-
- continue;
- }
- }
-
-
- $item['money'] = clearZero($item['money']);
- $item['condition_money'] = clearZero($item['condition_money']);
-
- $item['coupon_name'] = $item['shop']['name'].$item['name'];
-
- switch($item['condition_type']) {
- case 1:
- $item['condition_type_desc'] = '无门槛';
- break;
- case 2:
- $item['condition_type_desc'] = "满{$item['condition_money']}减{$item['money']}";
- break;
- }
-
- switch($item['use_time_type']) {
- case 1:
- $item['user_time_desc'] = date('Y-m-d H:i:s',$item['use_time_start']). ' 至 ' . date('Y-m-d H:i:s', $item['use_time_end']);
- break;
- case 2:
- $item['user_time_desc'] = "领取当天{$item['use_time']}天内可用";
- break;
- case 3:
- $item['user_time_desc'] = "领取次日{$item['use_time']}天内可用";
- break;
- }
-
- switch($item['use_goods_type']) {
- case 1:
- $item['use_scene_desc'] = $item['shop']['name'].'全店通用';
- break;
- case 2:
- $item['use_scene_desc'] = $item['shop']['name'].'部分商品可用';
- if(isset($get['goods_id'])) {
- $goodsArr = CouponGoods::where(['coupon_id'=>$item['id'], 'goods_id'=>$get['goods_id']])->select()->toArray();
- if(!$goodsArr) {
-
- unset($lists[$key]);
- }
- }
- break;
- case 3:
- $item['use_scene_desc'] = $item['shop']['name'].'部分商品不可用';
- if(isset($get['goods_id'])) {
- $goodsArr = CouponGoods::where(['coupon_id'=>$item['id'], 'goods_id'=>$get['goods_id']])->select()->toArray();
- if($goodsArr) {
-
- unset($lists[$key]);
- }
- }
- break;
- }
-
- $count = CouponList::where([
- 'del' => 0,
- 'user_id' => $get['user_id'],
- 'coupon_id' => $item['id']
- ])->count();
- $item['is_get'] = $count ? 1: 0;
-
- switch ($item['get_num_type']) {
- case 1:
-
-
- $item['can_continue_get'] = $item['is_get'];
- break;
- case 2:
-
-
-
-
- $item['can_continue_get'] = $item['is_get'] ? (int) ($count < $item['get_num']) : 0;
- break;
- case 3:
-
- $today_count = CouponList::whereTime('create_time', 'today')->where([
- 'del' => 0,
- 'user_id' => $get['user_id'],
- 'coupon_id' => $item['id']
- ])->count();
-
-
-
- $item['can_continue_get'] = $item['is_get'] ? (int) ($today_count < $item['get_num']) : 0;
- break;
- }
- }
-
-
- $count = count($lists);
- $index = ($get['page_no'] -1) * $get['page_size'];
- $lists = array_slice($lists, $index, $get['page_size']);
-
- return [
- 'count' => $count,
- 'lists' => $lists,
- 'page_no' => $get['page_no'],
- 'page_size' => $get['page_size'],
- 'more' => is_more($count, $get['page_no'], $get['page_size']),
- ];
- }
-
- public static function getCoupon($post)
- {
- try{
-
- $where = [
- ['del', '=', 0],
- ['status', '=', 1],
- ['send_time_start', '<=', time()],
- ['send_time_end', '>=', time()],
- ['id', '=', $post['coupon_id']],
- ];
-
- $coupon = Coupon::where($where)->findOrEmpty();
- if($coupon->isEmpty()) {
- throw new \think\Exception('优惠券已失效');
- }
-
- if($coupon['send_total_type'] == 2) {
-
- $already_issue = CouponList::where(['del'=>0, 'coupon_id'=>$coupon['id']])->count();
- if($already_issue >= $coupon['send_total']) {
- throw new \think\Exception('优惠券已领取完了');
- }
- }
-
- switch($coupon['get_num_type']) {
- case 2:
-
- $already_get = CouponList::where(['del'=>0, 'coupon_id'=>$coupon['id'], 'user_id'=>$post['user_id']])->count();
- if($already_get >= $coupon['get_num']) {
- throw new \think\Exception('您已超过领取次数');
- }
- break;
- case 3:
- $today_date = date('Y-m-d', time());
- $today_time = strtotime($today_date.' 00:00:00');
-
- $already_today_get = CouponList::where([
- ['del', '=', 0],
- ['coupon_id', '=', $coupon['id']],
- ['user_id', '=', $post['user_id']],
- ['create_time', '>=', $today_time],
- ])->count();
- if($already_today_get >= $coupon['get_num']) {
- throw new \think\Exception('今天您已超过领取次数');
- }
- break;
- }
-
- $time = time();
- $addData = [
- 'user_id' => $post['user_id'],
- 'coupon_id' => $post['coupon_id'],
- 'coupon_code' => create_coupon_code(),
- 'status' => 0,
- 'create_time' => $time,
- 'update_time' => $time,
- 'del' => 0
- ];
- CouponList::create($addData);
-
-
- event('Footprint', [
- 'type' => FootprintEnum::RECEIVE_COUPON,
- 'user_id' => $post['user_id'],
- 'foreign_id' => $post['coupon_id']
- ]);
-
- return true;
- }catch(\Exception $e){
- self::$error = $e->getMessage();
- return false;
- }
- }
-
- public static function myCouponList($get)
- {
-
- $whereOne = [
- ['cl.del', '=', 0],
- ['cl.user_id', '=', $get['user_id']]
- ];
- $fieldOne = 'cl.*, c.name,c.use_time_type,c.use_time_start,c.use_time_end,c.use_time as coupon_use_time,s.name as shop_name,c.condition_type,c.condition_money,c.money,c.use_goods_type,c.shop_id';
- $count_list = CouponList::alias('cl')
- ->leftJoin('coupon c', 'c.id=cl.coupon_id')
- ->leftJoin('shop s', 's.id=c.shop_id')
- ->field($fieldOne)
- ->where($whereOne)
- ->order('id', 'desc')
- ->select()
- ->toArray();
-
- foreach($count_list as &$item) {
- $item['is_expired'] = 0;
- switch($item['use_time_type']) {
- case 1:
- if($item['use_time_end'] <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- case 2:
- $days = '+'.$item['coupon_use_time'].' day';
- $expired_time = strtotime($days, $item['create_time']);
- if($expired_time <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- case 3:
- $days = '+'.($item['coupon_use_time']+1).' day';
- $expired_time = strtotime($days, $item['create_time']);
- if($expired_time <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- }
- }
-
- $used_array = array_filter($count_list, function($item) {
- return $item['status'] == 1;
- });
- $valid_array = array_filter($count_list, function($item) {
- return $item['status'] == 0 && $item['is_expired'] == 0;
- });
- $expired_array = array_filter($count_list, function($item) {
- return $item['status'] == 0 && $item['is_expired'] == 1;
- });
- $used = count($used_array);
- $valid = count($valid_array);
- $expired = count($expired_array);
-
-
- if(isset($get['type'])) {
- switch($get['type']) {
- case 'used':
- $count_list = array_filter($count_list, function($item) {
- return $item['status'] == 1;
- });
- break;
- case 'valid':
- $count_list = array_filter($count_list, function($item) {
- return $item['status'] == 0 && $item['is_expired'] == 0;
- });
- break;
- case 'expired':
- $count_list = array_filter($count_list, function($item) {
- return $item['status'] == 0 && $item['is_expired'] == 1;
- });
- break;
- }
- }else{
- $count_list = array_filter($count_list, function($item) {
- return $item['status'] == 0 && $item['is_expired'] == 0;
- });
- }
-
- $count_list = self::formatInfo($count_list);
-
-
- $count = count($count_list);
- $index = ($get['page_no'] -1) * $get['page_size'];
- $lists = array_slice($count_list, $index, $get['page_size']);
- $expand = [
- 'used' => $used,
- 'valid' => $valid,
- 'expired' => $expired
- ];
-
- return [
- 'count' => $count,
- 'expand' => $expand,
- 'lists' => $lists,
- 'page_no' => $get['page_no'],
- 'page_size' => $get['page_size'],
- 'more' => is_more($count, $get['page_no'], $get['page_size']),
- ];
- }
-
-
-
- public static function formatInfo($coupon_list)
- {
- foreach($coupon_list as &$item) {
-
- $item['money'] = clearZero($item['money']);
- $item['condition_money'] = clearZero($item['condition_money']);
-
- $item['coupon_name'] = $item['shop_name'].$item['name'];
-
- switch($item['condition_type']) {
- case 1:
- $item['condition_type_desc'] = '无门槛';
- break;
- case 2:
- $item['condition_type_desc'] = "满{$item['condition_money']}减{$item['money']}";
- break;
- }
-
- switch($item['use_time_type']) {
-
- case 1:
- $item['user_time_desc'] = date('Y-m-d H:i:s',$item['use_time_start']). ' 至 ' . date('Y-m-d H:i:s', $item['use_time_end']);
- break;
-
- case 2:
- $item['user_time_desc'] = "有效期至 " . date('Y-m-d H:i:s', $item['create_time'] + $item['coupon_use_time'] * 86400);
- break;
-
- case 3:
- $item['user_time_desc'] = "有效期至 " . date('Y-m-d 23:59:59', $item['create_time'] + $item['coupon_use_time'] * 86400);
- break;
- }
-
- switch($item['use_goods_type']) {
- case 1:
- $item['use_scene_desc'] = $item['shop_name'].'全店通用';
- $item['use_goods_desc'] = '所有商品可用';
- break;
- case 2:
- $item['use_scene_desc'] = $item['shop_name'].'部分商品可用';
- $goods = CouponGoods::alias('cg')
- ->field('g.name as goods_name')
- ->leftJoin('goods g', 'g.id=cg.goods_id')
- ->where(['coupon_id'=>$item['coupon_id']])
- ->select()
- ->toArray();
- $item['use_goods_desc'] = count($goods) ? '仅限 ' : '';
- $item['use_goods_desc'] .= implode('、', array_column($goods, 'goods_name'));
- $item['use_goods_desc'] .= ' 商品可用';
- break;
- case 3:
- $item['use_scene_desc'] = $item['shop_name'].'部分商品不可用';
- $goods = CouponGoods::alias('cg')
- ->field('g.name as goods_name')
- ->leftJoin('goods g', 'g.id=cg.goods_id')
- ->where(['coupon_id'=>$item['coupon_id']])
- ->select()
- ->toArray();
- $item['use_goods_desc'] = count($goods) ? '仅限 ' : '';
- $item['use_goods_desc'] .= implode('、', array_column($goods, 'goods_name'));
- $item['use_goods_desc'] .= ' 之外的商品可用';
- break;
- }
- }
- return $coupon_list;
- }
-
-
-
- public static function calcMoney($buy_type, $get)
- {
- if($buy_type == 'buy') {
- $price = GoodsItem::where('id', $get['item_id'])->value('price');
- return $price * $get['num'];
- }
- if($buy_type == 'cart') {
- $total = 0;
- $buyGoodsArr = Cart::alias('c')
- ->leftJoin('goods_item gi', 'gi.id=c.item_id')
- ->field('c.item_id,c.goods_num,gi.price')
- ->where([
- ['id', 'in', $get['param']],
- ['selected', '=', '1']
- ])->select()->toArray();
- foreach($buyGoodsArr as $item) {
- $total += $item['price'] * $item['goods_num'];
- }
- return $total;
- }
- }
-
-
-
- public static function getGoodsInfo($goods)
- {
- $buyGoodsArr = [];
- $buyMoney = 0;
- foreach($goods as $item) {
- $buyGoodsArr[] = $item['goods_id'];
- $price = GoodsItem::where('id', $item['item_id'])->value('price');
- $buyMoney += $item['num'] * $price;
- }
- return [
- 'buyGoodsArr' => $buyGoodsArr,
- 'buyMoney' => $buyMoney,
- ];
- }
-
-
-
- public static function getBuyCouponList($post)
- {
-
-
-
- $whereOne = [
- ['cl.del', '=', 0],
- ['cl.user_id', '=', $post['user_id']],
- ['s.id', '=', $post['shop_id']],
- ];
- $fieldOne = 'cl.*, c.name,c.use_time_type,c.use_time_start,c.use_time_end,c.use_time as coupon_use_time,s.name as shop_name,c.condition_type,c.condition_money,c.money,c.use_goods_type';
- $count_list = CouponList::alias('cl')
- ->leftJoin('coupon c', 'c.id=cl.coupon_id')
- ->leftJoin('shop s', 's.id=c.shop_id')
- ->field($fieldOne)
- ->where($whereOne)
- ->order('id', 'desc')
- ->select()
- ->toArray();
-
-
- $tempData = [];
- foreach($count_list as $item) {
- $item['is_expired'] = 0;
- switch($item['use_time_type']) {
- case 1:
- if($item['use_time_end'] <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- case 2:
- $days = '+'.$item['coupon_use_time'].' day';
- $expired_time = strtotime($days, $item['create_time']);
- if($expired_time <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- case 3:
- $days = '+'.($item['coupon_use_time']+1).' day';
- $expired_time = strtotime($days, $item['create_time']);
- if($expired_time <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- }
- $tempData[] = $item;
- }
-
-
- $count_list = array_filter($tempData, function($item) {
- return $item['status'] == 0 && $item['is_expired'] == 0;
- });
-
-
- $goodsInfo = self::getGoodsInfo($post['goods']);
- $buyGoodsArr = $goodsInfo['buyGoodsArr'];
- $buyMoney = $goodsInfo['buyMoney'];
-
- $suit = [];
- $unSuit = [];
- foreach($count_list as $key => $item) {
- $flag = true;
-
- switch($item['use_goods_type']) {
- case 2:
- $goods = CouponGoods::alias('cg')
- ->where(['coupon_id'=>$item['coupon_id']])
- ->column('cg.goods_id');
- foreach($buyGoodsArr as $subItem) {
- if(!in_array($subItem, $goods)) {
- $flag = false;
- break;
- }
- }
- break;
- case 3:
- $goods = CouponGoods::alias('cg')
- ->where(['coupon_id'=>$item['coupon_id']])
- ->column('cg.goods_id');
- foreach($buyGoodsArr as $subItem) {
- if(in_array($subItem, $goods)) {
- $flag = false;
- break;
- }
- }
- break;
- }
-
-
- switch($item['condition_type']) {
- case 2:
- if($buyMoney < $item['condition_money']) {
-
- $flag = false;
- }
- break;
- }
-
-
- if (true == $flag && $item['use_time_type'] == 1) {
- $item['use_time_start'] > time() && $flag = false;
- }
-
-
- if (true == $flag && $item['use_time_type'] == 3) {
- $today = date('Y-m-d 00:00:00', $item['create_time']);
-
- $effectiveStartTime = strtotime($today) + 24 * 60 * 60;
- $effectiveStartTime >= time() && $flag = false;
- }
-
- if($flag) {
-
- $suit[] = $item;
- }else{
-
- $unSuit[] = $item;
- }
- }
-
- $suit = self::formatInfo($suit);
- $unSuit = self::formatInfo($unSuit);
-
- return [
- 'suit' => $suit,
- 'un_suit' => $unSuit,
- ];
- }
-
-
-
- public static function getShopCouponList($post)
- {
-
- $whereOne = [
- ['cl.del', '=', 0],
- ['cl.user_id', '=', $post['user_id']],
- ['s.id', '=', $post['shop_id']],
- ];
- $fieldOne = 'cl.*, c.name,c.use_time_type,c.use_time_start,c.use_time_end,c.use_time as coupon_use_time,s.name as shop_name,c.condition_type,c.condition_money,c.money,c.use_goods_type';
- $count_list = CouponList::alias('cl')
- ->leftJoin('coupon c', 'c.id=cl.coupon_id')
- ->leftJoin('shop s', 's.id=c.shop_id')
- ->field($fieldOne)
- ->where($whereOne)
- ->order('id', 'desc')
- ->select()
- ->toArray();
-
-
- foreach($count_list as &$item) {
- $item['is_expired'] = 0;
- switch($item['use_time_type']) {
- case 1:
- if($item['use_time_end'] <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- case 2:
- $days = '+'.$item['coupon_use_time'].' day';
- $expired_time = strtotime($days, $item['create_time']);
- if($expired_time <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- case 3:
- $days = '+'.($item['coupon_use_time']+1).' day';
- $expired_time = strtotime($days, $item['create_time']);
- if($expired_time <= time()) {
- $item['is_expired'] = 1;
- }
- break;
- }
- }
-
-
- $count_list = array_filter($count_list, function($item) {
- return $item['status'] == 0 && $item['is_expired'] == 0;
- });
-
-
- $goodsInfo = self::getGoodsInfo($post['goods']);
- $buyGoodsArr = $goodsInfo['buyGoodsArr'];
- $buyMoney = $goodsInfo['buyMoney'];
-
- $suit = [];
- $unSuit = [];
- foreach($count_list as $key => $item) {
- $flag = true;
-
- switch($item['use_goods_type']) {
- case 2:
- $goods = CouponGoods::alias('cg')
- ->where(['coupon_id'=>$item['coupon_id']])
- ->column('cg.goods_id');
- foreach($buyGoodsArr as $subItem) {
- if(!in_array($subItem, $goods)) {
- $flag = false;
- break;
- }
- }
- break;
- case 3:
- $goods = CouponGoods::alias('cg')
- ->where(['coupon_id'=>$item['coupon_id']])
- ->column('cg.goods_id');
- foreach($buyGoodsArr as $subItem) {
- if(in_array($subItem, $goods)) {
- $flag = false;
- break;
- }
- }
- break;
- }
-
-
- switch($item['condition_type']) {
- case 2:
- if($buyMoney < $item['condition_money']) {
-
- $flag = false;
- }
- break;
- }
- if($flag) {
-
- $suit[] = $item;
- }else{
-
- $unSuit[] = $item;
- }
- }
-
- $suit = self::formatInfo($suit);
- $unSuit = self::formatInfo($unSuit);
- return [
- 'suit' => $suit,
- 'un_suit' => $unSuit,
- ];
- }
- }
|