UserRiceCard.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model\card;
  13. use app\api\model\Goods;
  14. use app\common\model\card\UserRiceCard as UserRiceCardModel;
  15. use app\api\service\User as UserService;
  16. use app\common\library\helper;
  17. use app\common\model\GoodsCategoryRel;
  18. use think\facade\Db;
  19. use app\api\model\card\UserRiceCardDelivery as UserRiceDeliveryModel;
  20. /**
  21. * 米卡订单模型
  22. * Class Goods
  23. * @package app\api\model
  24. */
  25. class UserRiceCard extends UserRiceCardModel
  26. {
  27. protected $append = ['type_text','image_text','state_text'];
  28. //状态
  29. public function getStateTextAttr($value,$data){
  30. if($this->frozen_state==1){
  31. return 3;//已冻结
  32. }
  33. if($data['type']==2&&$this->balance==0){
  34. return 2;//已使用
  35. }
  36. if($data['type']==3){
  37. $delivery = UserRiceDeliveryModel::where('user_rice_card_id',$this->id)->find();
  38. if($delivery){
  39. return 2;
  40. }
  41. $now = Date("Y-m-d H:i:s",time());
  42. if($now>$data['expire_time']){
  43. return 4;
  44. }
  45. }
  46. $coupon = UserRiceCard::where("parent_id",$this->id)->where('user_id','>',0)->find();
  47. if($coupon){
  48. return 1;//已转赠
  49. }
  50. return 0;
  51. }
  52. /**
  53. * 获取米卡列表
  54. * @param int|null $limit 获取的数量
  55. * @param bool $onlyReceive 只显示可领取
  56. * @return \think\Collection
  57. * @throws BaseException
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function getList(int $user_id, int $type = 1,int $limit = 150){
  63. // 查询构造器
  64. $query = $this->getNewQuery();
  65. // 只显示可领取(未过期,未发完)的优惠券
  66. $query->where('user_id',$user_id);
  67. $query->where("mk.is_delete",0);
  68. $now = Date("Y-m-d H:i:s",time());
  69. if($type==1){
  70. $query->where('effect_state','in',[0,1])->where("expire_time",">",$now)->where('balance','>',0)->where("frozen_state",0);
  71. }
  72. if($type==2){
  73. $query->where(function($query)use($now){
  74. $query->whereOr('effect_state','in',[2]);
  75. $query->whereOr("frozen_state",1);
  76. $query->whereOr('balance','=',0);
  77. $query->whereOr("expire_time","<",$now);
  78. });
  79. }
  80. if($type==1){
  81. $order = ['id' => 'desc'];
  82. }
  83. if($type==2){
  84. $order = ['expire_time'=>'desc'];
  85. }
  86. //查询数量
  87. $limit > 0 && $query->limit($limit);
  88. $couponList = $query->with(['delivery'])->alias('mk')
  89. ->order($order)
  90. ->select();
  91. return $couponList;
  92. }
  93. //米卡未被领取撤回
  94. public function index(){
  95. $t = 86400;
  96. // $t = 120;
  97. //整卡转赠
  98. $list = UserRiceCardModel::field("id")->where('effect_state',0)->select();
  99. // return $list;
  100. $idarr = [];
  101. foreach($list as $row){
  102. $idarr[] = $row['id'];
  103. }
  104. foreach($idarr as $id){
  105. $coupon = UserRiceCardModel::where('parent_id',$id)->find();
  106. if($coupon){
  107. $add_time = strtotime($coupon['create_time']) + $t;//加一天的时间
  108. $now = time();
  109. //过期了没领
  110. if($now>$add_time){
  111. $coupon->effect_state = 2;//已失效
  112. $coupon->save();//该卡改为已失效
  113. $pcouponModel = UserRiceCardModel::where('id',$coupon->parent_id)->where('effect_state',0)->find();
  114. $pcouponModel->effect_state = 1;//原卡改为生效中,
  115. //可用数量改为原来的值
  116. $pcouponModel->save();
  117. }
  118. }
  119. }
  120. echo 'success';
  121. echo 't:'.$t;
  122. }
  123. /**
  124. * 验证商品是否可使用现金卡(登录用户)
  125. */
  126. public function validUserCashRiceCard($goodsId, $userInfo = false)
  127. {
  128. $goods = Goods::detail($goodsId);
  129. if (empty($goods) || !$userInfo) {
  130. return false;
  131. }
  132. // 查询用户可用现金卡
  133. $list = $this->with(['riceCardGoodsExcept'])->where('user_id', $userInfo['user_id'])
  134. ->where('activation_state', 1)
  135. ->where('effect_state', 1)
  136. ->where("expire_time",">",date("Y-m-d H:i:s", time()))
  137. ->where('frozen_state', 0)
  138. ->where('is_delete', 0)
  139. ->select();
  140. if (empty($list)) {
  141. return false;
  142. }
  143. foreach ($list as $item) {
  144. // 判断抵扣商品类目和除外商品
  145. $exceptGoodsIds = helper::getArrayColumn($item['riceCardGoodsExcept'], 'goods_id');
  146. if (empty($item['dk_cat_ids'])) { // 全部类目
  147. if (!in_array($goodsId, $exceptGoodsIds)) {
  148. return true;
  149. }
  150. } else { // 指定类目
  151. $dk_cat_ids = explode(',', $item['dk_cat_ids']);
  152. $goodsIds = GoodsCategoryRel::whereIn('category_id', $dk_cat_ids)->column('goods_id');
  153. if (in_array($goodsId, $goodsIds) && !in_array($goodsId, $exceptGoodsIds)) {
  154. return true;
  155. }
  156. }
  157. }
  158. return false;
  159. }
  160. /**
  161. * 获取有效米卡详情
  162. */
  163. public static function getValidRiceCardDetail($riceCardId)
  164. {
  165. return self::with(['riceCardGoodsExcept'])->where('id', $riceCardId)
  166. ->where('activation_state', 1)
  167. // ->where('effect_state', 1)
  168. // ->where('frozen_state', 0)
  169. ->where('is_delete', 0)
  170. ->find();
  171. }
  172. //生成随机数
  173. public function randSecrets(){
  174. $num = 100;
  175. $arr = [];
  176. for($i=0;$i<$num;$i++){
  177. $arr[] = mt_rand(10000000,99999999);
  178. }
  179. return array_unique($arr);
  180. }
  181. //生成随机码
  182. public function createCode(){
  183. $coupon_code =[];
  184. $randArray = $this->randSecrets();
  185. $secrets = UserRiceCardModel::whereIn('coupon_code',$randArray)->select();
  186. foreach($secrets as $s){
  187. $coupon_code[] = $s->coupon_code;
  188. }
  189. $avaiableArray = array_diff($randArray,$coupon_code);
  190. $avaiableArray = array_values($avaiableArray);
  191. }
  192. /**
  193. * 获取我的可用米卡列表
  194. * @param $user_id
  195. * @return UserRiceCard[]|array|\think\Collection
  196. * @throws \app\common\exception\BaseException
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. * @author: zjwhust
  201. * @Time: 2021/12/28 16:10
  202. */
  203. public static function getUserRiceCardList($user_id){
  204. $list = self::with(['riceCardGoodsExcept'])
  205. ->where([
  206. 'user_id'=>$user_id,//用户ID
  207. // 'type'=>2,//米卡类型 1-电子套餐卡 2-电子现金卡 3-电子实物兑换卡 4-实体现金卡
  208. 'activation_state'=> 1,//激活状态 0 未激活,1已激活
  209. 'effect_state'=> 1,//生效 状态 0:转赠中 1:生效 2已失效 默认生效 当转赠成功的时侯,变成已失效
  210. 'frozen_state'=> 0,//冻结状态 0 未冻结 1 已冻结
  211. 'is_delete'=> 0,//删除状态 0 未删除 1 已删除
  212. ])->where('balance','>',0)//可用余额大于0//米卡类型 1-电子套餐卡 2-电子现金卡 3-电子实物兑换卡 4-实体现金卡
  213. ->where('type','in',[2,4])//米卡类型 1-电子套餐卡 2-电子现金卡 3-电子实物兑换卡 4-实体现金卡
  214. ->order('balance','desc')
  215. ->select();
  216. return $list;
  217. }
  218. }