123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace app\common\model\card;
- use app\common\model\UploadFile;
- use app\common\model\BaseModel;
- use app\common\model\card\UserRiceCardConsume;
- use app\common\model\card\UserRiceCardDelivery;
- /**
- * 我的米卡模型
- * @package app\common\model
- */
- class UserRiceCard extends BaseModel
- {
- // 米卡类型
- const TYPE = [1 => '电子套餐卡', 2 => '电子现金卡', 3 => '电子实物兑换卡'];
- const PACKAGE_CARD = 1; // 电子套餐卡
- const CASH_CARD = 2; // 电子现金卡
- const EXCHANGE_CARD = 3; // 电子实物兑换卡
- protected $name = 'user_rice_card';
-
- public function getTypeTextAttr(){
- return '电子现金卡';
- // return self::TYPE[$this->type] ?? '';
-
- }
- /**
- * 一对多关联 米卡除外商品表
- */
- public function riceCardGoodsExcept()
- {
- return $this->hasMany('UserRiceCardGoods', 'user_rice_card_id')->where('is_except', UserRiceCardGoods::EXCEPT_YES);
- }
- /**
- * 一对多关联 米卡可兑换商品表
- */
- public function riceCardGoods()
- {
- return $this->hasMany('UserRiceCardGoods', 'user_rice_card_id')->where('is_except', UserRiceCardGoods::EXCEPT_NO);
- }
- /**
- * 一对多关联 消费记录
- */
- public function consume(){
- return $this->hasMany('UserRiceCardConsume', 'user_rice_card_id')->order('id', 'desc');
- }
- //配送详情
- public function delivery(){
- return $this->hasOne('UserRiceCardDelivery','user_rice_card_id');
- }
- public function getImageTextAttr($value){
- return UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_id)->ali_url??'';
- }
- /**
- * 获取记录
- * @param int $where
- * @param array $with
- * @return static
- */
- public static function detail($where, array $with = [])
- {
- return static::get($where, $with);
- }
- /**
- * 增加冻结金额
- * @param int $id 自增ID
- * @param float $amount 增加的金额
- * @return mixed
- */
- public static function setIncByField(int $id, $field, float $amount)
- {
- return (new static)->setInc($id, $field, $amount);
- }
- /**
- * 减少冻结金额
- * @param int $id 自增ID
- * @param float $amount 减少的金额
- * @return mixed
- */
- public static function setDecByField(int $id, $field , float $amount)
- {
- return (new static)->setDec($id, $field, $amount);
- }
- /**
- * 多字段增减
- * @param array|int|bool $where 支持自增ID 也支持where语句
- * @param array $incData //自增字段
- * @param array $decData //自减字段
- * @param array $data //正常update字段
- * @return mixed
- */
- public static function setIncDecByField($where, $incData , $decData, $data=[])
- {
- return (new static)->setIncDec($where, $incData, $decData, $data);
- }
- //米卡未被领取撤回事件
- public function revokeEvent($minute){
- $t = $minute*60;
- $deadtime = time()-$t;
- //整卡转赠
- $list = UserRiceCard::field("id")->where('effect_state',0)->where('create_time','<',Date("Y-m-d H:i:s",$deadtime))->select();
- // return $list;
- $idarr = [];
- foreach($list as $row){
- $idarr[] = $row['id'];
- }
- foreach($idarr as $id){
- $coupon = UserRiceCard::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 = UserRiceCard::where('id',$coupon->parent_id)->where('effect_state',0)->find();
- $pcouponModel->effect_state = 1;//原卡改为生效中,
- //可用数量改为原来的值
- $pcouponModel->save();
- }
- }
- }
- }
- }
|