UserRiceCard.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\common\model\card;
  3. use app\common\model\UploadFile;
  4. use app\common\model\BaseModel;
  5. use app\common\model\card\UserRiceCardConsume;
  6. use app\common\model\card\UserRiceCardDelivery;
  7. /**
  8. * 我的米卡模型
  9. * @package app\common\model
  10. */
  11. class UserRiceCard extends BaseModel
  12. {
  13. // 米卡类型
  14. const TYPE = [1 => '电子套餐卡', 2 => '电子现金卡', 3 => '电子实物兑换卡'];
  15. const PACKAGE_CARD = 1; // 电子套餐卡
  16. const CASH_CARD = 2; // 电子现金卡
  17. const EXCHANGE_CARD = 3; // 电子实物兑换卡
  18. protected $name = 'user_rice_card';
  19. public function getTypeTextAttr(){
  20. return '电子现金卡';
  21. // return self::TYPE[$this->type] ?? '';
  22. }
  23. /**
  24. * 一对多关联 米卡除外商品表
  25. */
  26. public function riceCardGoodsExcept()
  27. {
  28. return $this->hasMany('UserRiceCardGoods', 'user_rice_card_id')->where('is_except', UserRiceCardGoods::EXCEPT_YES);
  29. }
  30. /**
  31. * 一对多关联 米卡可兑换商品表
  32. */
  33. public function riceCardGoods()
  34. {
  35. return $this->hasMany('UserRiceCardGoods', 'user_rice_card_id')->where('is_except', UserRiceCardGoods::EXCEPT_NO);
  36. }
  37. /**
  38. * 一对多关联 消费记录
  39. */
  40. public function consume(){
  41. return $this->hasMany('UserRiceCardConsume', 'user_rice_card_id')->order('id', 'desc');
  42. }
  43. //配送详情
  44. public function delivery(){
  45. return $this->hasOne('UserRiceCardDelivery','user_rice_card_id');
  46. }
  47. public function getImageTextAttr($value){
  48. return UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_id)->ali_url??'';
  49. }
  50. /**
  51. * 获取记录
  52. * @param int $where
  53. * @param array $with
  54. * @return static
  55. */
  56. public static function detail($where, array $with = [])
  57. {
  58. return static::get($where, $with);
  59. }
  60. /**
  61. * 增加冻结金额
  62. * @param int $id 自增ID
  63. * @param float $amount 增加的金额
  64. * @return mixed
  65. */
  66. public static function setIncByField(int $id, $field, float $amount)
  67. {
  68. return (new static)->setInc($id, $field, $amount);
  69. }
  70. /**
  71. * 减少冻结金额
  72. * @param int $id 自增ID
  73. * @param float $amount 减少的金额
  74. * @return mixed
  75. */
  76. public static function setDecByField(int $id, $field , float $amount)
  77. {
  78. return (new static)->setDec($id, $field, $amount);
  79. }
  80. /**
  81. * 多字段增减
  82. * @param array|int|bool $where 支持自增ID 也支持where语句
  83. * @param array $incData //自增字段
  84. * @param array $decData //自减字段
  85. * @param array $data //正常update字段
  86. * @return mixed
  87. */
  88. public static function setIncDecByField($where, $incData , $decData, $data=[])
  89. {
  90. return (new static)->setIncDec($where, $incData, $decData, $data);
  91. }
  92. //米卡未被领取撤回事件
  93. public function revokeEvent($minute){
  94. $t = $minute*60;
  95. $deadtime = time()-$t;
  96. //整卡转赠
  97. $list = UserRiceCard::field("id")->where('effect_state',0)->where('create_time','<',Date("Y-m-d H:i:s",$deadtime))->select();
  98. // return $list;
  99. $idarr = [];
  100. foreach($list as $row){
  101. $idarr[] = $row['id'];
  102. }
  103. foreach($idarr as $id){
  104. $coupon = UserRiceCard::where('parent_id',$id)->find();
  105. if($coupon){
  106. $add_time = strtotime($coupon['create_time']) + $t;//加一天的时间
  107. $now = time();
  108. //过期了没领
  109. if($now>$add_time){
  110. $coupon->effect_state = 2;//已失效
  111. $coupon->save();//该卡改为已失效
  112. $pcouponModel = UserRiceCard::where('id',$coupon->parent_id)->where('effect_state',0)->find();
  113. $pcouponModel->effect_state = 1;//原卡改为生效中,
  114. //可用数量改为原来的值
  115. $pcouponModel->save();
  116. }
  117. }
  118. }
  119. }
  120. }