RiceCard.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\common\model\card;
  3. use app\common\model\BaseModel;
  4. use app\common\model\UploadFile;
  5. /**
  6. * 米卡模型
  7. * @package app\common\model
  8. */
  9. class RiceCard extends BaseModel
  10. {
  11. // 米卡类型
  12. const TYPE = [1 => '电子套餐卡', 2 => '电子现金卡', 3 => '实体实物兑换卡',4=>'实体现金卡'];
  13. const PACKAGE_CARD = 1; // 电子套餐卡
  14. const CASH_CARD = 2; // 电子现金卡
  15. const EXCHANGE_CARD = 3; // 实体实物兑换卡
  16. const CASH_CARD_ENTITY = 4;//实体现金卡
  17. protected $name = 'rice_card';
  18. protected $append = ['type_text','image_text'];
  19. public function getTypeTextAttr($value, $data)
  20. {
  21. return self::TYPE[$data['type']] ?? '';
  22. }
  23. /**
  24. * 详情富文本:HTML实体转换回普通字符
  25. * @param $value
  26. * @return string
  27. */
  28. public function getContentAttr($value)
  29. {
  30. return htmlspecialchars_decode($value);
  31. }
  32. public function getImageTextAttr($value){
  33. return UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_id)->ali_url??'';
  34. }
  35. /**
  36. * 一对多关联 米卡可兑换商品表
  37. */
  38. public function riceCardGoods()
  39. {
  40. return $this->hasMany('RiceCardGoods', 'card_id')->where('is_except', RiceCardGoods::EXCEPT_NO)
  41. ->order('id', 'desc');
  42. }
  43. /**
  44. * 一对多关联 米卡除外商品表
  45. */
  46. public function riceCardGoodsExcept()
  47. {
  48. return $this->hasMany('RiceCardGoods', 'card_id')->where('is_except', RiceCardGoods::EXCEPT_YES)
  49. ->order('id', 'desc');
  50. }
  51. /**
  52. * 一对多关联 米卡面额表
  53. */
  54. public function riceCardPriceValues()
  55. {
  56. return $this->hasMany('RiceCardPriceValues', 'card_id')->order('face_value', 'asc');
  57. }
  58. public function image()
  59. {
  60. $model = "app\\common\\model\\UploadFile";
  61. return $this->belongsTo($model, 'image_id', 'file_id');
  62. }
  63. /**
  64. * 总库存=剩余库存+销量
  65. */
  66. public function getTotalStockAttr($value, $data) {
  67. return $data['stock'] + $data['sale_num'];
  68. }
  69. public function getStatusTextAttr($value, $data) {
  70. $text = str_replace([0,1], ['已下架', '上架中'], $data['status']);
  71. return $text;
  72. }
  73. /**
  74. * 获取记录
  75. * @param int $id
  76. * @param array $with
  77. * @return static
  78. */
  79. public static function detail(int $id, array $with = [])
  80. {
  81. return static::get($id, $with);
  82. }
  83. /**
  84. * 更新米卡数据
  85. * @param $orderIds
  86. * @param $data
  87. * @return false|int
  88. */
  89. public function onUpdate($card_id, $data)
  90. {
  91. return static::updateBase($data, [['id', '=', $card_id]]);
  92. }
  93. //更新数据
  94. public function updateRiceCard(array $data){
  95. return (new RiceCard)->updateAll($data);
  96. }
  97. }