123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\model\card;
- use app\api\model\Goods;
- use app\common\model\card\UserRiceCard as UserRiceCardModel;
- use app\api\service\User as UserService;
- use app\common\library\helper;
- use app\common\model\GoodsCategoryRel;
- use think\facade\Db;
- use app\api\model\card\UserRiceCardDelivery as UserRiceDeliveryModel;
- /**
- * 米卡订单模型
- * Class Goods
- * @package app\api\model
- */
- class UserRiceCard extends UserRiceCardModel
- {
- protected $append = ['type_text','image_text','state_text'];
- //状态
- public function getStateTextAttr($value,$data){
- if($this->frozen_state==1){
- return 3;//已冻结
- }
- if($data['type']==2&&$this->balance==0){
- return 2;//已使用
- }
-
- if($data['type']==3){
- $delivery = UserRiceDeliveryModel::where('user_rice_card_id',$this->id)->find();
- if($delivery){
- return 2;
- }
- $now = Date("Y-m-d H:i:s",time());
- if($now>$data['expire_time']){
- return 4;
- }
- }
- $coupon = UserRiceCard::where("parent_id",$this->id)->where('user_id','>',0)->find();
- if($coupon){
- return 1;//已转赠
- }
- return 0;
- }
- /**
- * 获取米卡列表
- * @param int|null $limit 获取的数量
- * @param bool $onlyReceive 只显示可领取
- * @return \think\Collection
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList(int $user_id, int $type = 1,int $limit = 150){
- // 查询构造器
- $query = $this->getNewQuery();
- // 只显示可领取(未过期,未发完)的优惠券
- $query->where('user_id',$user_id);
- $query->where("mk.is_delete",0);
- $now = Date("Y-m-d H:i:s",time());
- if($type==1){
- $query->where('effect_state','in',[0,1])->where("expire_time",">",$now)->where('balance','>',0)->where("frozen_state",0);
- }
- if($type==2){
-
- $query->where(function($query)use($now){
- $query->whereOr('effect_state','in',[2]);
- $query->whereOr("frozen_state",1);
- $query->whereOr('balance','=',0);
- $query->whereOr("expire_time","<",$now);
-
- });
- }
- if($type==1){
- $order = ['id' => 'desc'];
- }
- if($type==2){
- $order = ['expire_time'=>'desc'];
- }
- //查询数量
- $limit > 0 && $query->limit($limit);
- $couponList = $query->with(['delivery'])->alias('mk')
- ->order($order)
- ->select();
-
-
- return $couponList;
- }
- //米卡未被领取撤回
- public function index(){
- $t = 86400;
- // $t = 120;
- //整卡转赠
- $list = UserRiceCardModel::field("id")->where('effect_state',0)->select();
- // return $list;
- $idarr = [];
- foreach($list as $row){
- $idarr[] = $row['id'];
- }
- foreach($idarr as $id){
- $coupon = UserRiceCardModel::where('parent_id',$id)->find();
- if($coupon){
- $add_time = strtotime($coupon['create_time']) + $t;//加一天的时间
- $now = time();
- //过期了没领
- if($now>$add_time){
- $coupon->effect_state = 2;//已失效
- $coupon->save();//该卡改为已失效
- $pcouponModel = UserRiceCardModel::where('id',$coupon->parent_id)->where('effect_state',0)->find();
- $pcouponModel->effect_state = 1;//原卡改为生效中,
- //可用数量改为原来的值
- $pcouponModel->save();
- }
- }
- }
- echo 'success';
- echo 't:'.$t;
- }
-
- /**
- * 验证商品是否可使用现金卡(登录用户)
- */
- public function validUserCashRiceCard($goodsId, $userInfo = false)
- {
- $goods = Goods::detail($goodsId);
- if (empty($goods) || !$userInfo) {
- return false;
- }
- // 查询用户可用现金卡
- $list = $this->with(['riceCardGoodsExcept'])->where('user_id', $userInfo['user_id'])
- ->where('activation_state', 1)
- ->where('effect_state', 1)
- ->where("expire_time",">",date("Y-m-d H:i:s", time()))
- ->where('frozen_state', 0)
- ->where('is_delete', 0)
- ->select();
- if (empty($list)) {
- return false;
- }
- foreach ($list as $item) {
- // 判断抵扣商品类目和除外商品
- $exceptGoodsIds = helper::getArrayColumn($item['riceCardGoodsExcept'], 'goods_id');
- if (empty($item['dk_cat_ids'])) { // 全部类目
- if (!in_array($goodsId, $exceptGoodsIds)) {
- return true;
- }
- } else { // 指定类目
- $dk_cat_ids = explode(',', $item['dk_cat_ids']);
- $goodsIds = GoodsCategoryRel::whereIn('category_id', $dk_cat_ids)->column('goods_id');
- if (in_array($goodsId, $goodsIds) && !in_array($goodsId, $exceptGoodsIds)) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 获取有效米卡详情
- */
- public static function getValidRiceCardDetail($riceCardId)
- {
- return self::with(['riceCardGoodsExcept'])->where('id', $riceCardId)
- ->where('activation_state', 1)
- // ->where('effect_state', 1)
- // ->where('frozen_state', 0)
- ->where('is_delete', 0)
- ->find();
- }
- //生成随机数
- public function randSecrets(){
- $num = 100;
- $arr = [];
- for($i=0;$i<$num;$i++){
- $arr[] = mt_rand(10000000,99999999);
- }
- return array_unique($arr);
- }
- //生成随机码
- public function createCode(){
- $coupon_code =[];
- $randArray = $this->randSecrets();
- $secrets = UserRiceCardModel::whereIn('coupon_code',$randArray)->select();
- foreach($secrets as $s){
- $coupon_code[] = $s->coupon_code;
- }
- $avaiableArray = array_diff($randArray,$coupon_code);
- $avaiableArray = array_values($avaiableArray);
- }
- /**
- * 获取我的可用米卡列表
- * @param $user_id
- * @return UserRiceCard[]|array|\think\Collection
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author: zjwhust
- * @Time: 2021/12/28 16:10
- */
- public static function getUserRiceCardList($user_id){
- $list = self::with(['riceCardGoodsExcept'])
- ->where([
- 'user_id'=>$user_id,//用户ID
- // 'type'=>2,//米卡类型 1-电子套餐卡 2-电子现金卡 3-电子实物兑换卡 4-实体现金卡
- 'activation_state'=> 1,//激活状态 0 未激活,1已激活
- 'effect_state'=> 1,//生效 状态 0:转赠中 1:生效 2已失效 默认生效 当转赠成功的时侯,变成已失效
- 'frozen_state'=> 0,//冻结状态 0 未冻结 1 已冻结
- 'is_delete'=> 0,//删除状态 0 未删除 1 已删除
- ])->where('balance','>',0)//可用余额大于0//米卡类型 1-电子套餐卡 2-电子现金卡 3-电子实物兑换卡 4-实体现金卡
- ->where('type','in',[2,4])//米卡类型 1-电子套餐卡 2-电子现金卡 3-电子实物兑换卡 4-实体现金卡
- ->order('balance','desc')
- ->select();
- return $list;
- }
- }
|