123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\common\model\card;
- use app\common\model\BaseModel;
- use app\common\model\UploadFile;
- /**
- * 米卡模型
- * @package app\common\model
- */
- class RiceCard extends BaseModel
- {
- // 米卡类型
- const TYPE = [1 => '电子套餐卡', 2 => '电子现金卡', 3 => '实体实物兑换卡',4=>'实体现金卡'];
- const PACKAGE_CARD = 1; // 电子套餐卡
- const CASH_CARD = 2; // 电子现金卡
- const EXCHANGE_CARD = 3; // 实体实物兑换卡
- const CASH_CARD_ENTITY = 4;//实体现金卡
- protected $name = 'rice_card';
- protected $append = ['type_text','image_text'];
- public function getTypeTextAttr($value, $data)
- {
- return self::TYPE[$data['type']] ?? '';
- }
- /**
- * 详情富文本:HTML实体转换回普通字符
- * @param $value
- * @return string
- */
- public function getContentAttr($value)
- {
- return htmlspecialchars_decode($value);
- }
- public function getImageTextAttr($value){
- return UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_id)->ali_url??'';
- }
- /**
- * 一对多关联 米卡可兑换商品表
- */
- public function riceCardGoods()
- {
- return $this->hasMany('RiceCardGoods', 'card_id')->where('is_except', RiceCardGoods::EXCEPT_NO)
- ->order('id', 'desc');
- }
- /**
- * 一对多关联 米卡除外商品表
- */
- public function riceCardGoodsExcept()
- {
- return $this->hasMany('RiceCardGoods', 'card_id')->where('is_except', RiceCardGoods::EXCEPT_YES)
- ->order('id', 'desc');
- }
- /**
- * 一对多关联 米卡面额表
- */
- public function riceCardPriceValues()
- {
- return $this->hasMany('RiceCardPriceValues', 'card_id')->order('face_value', 'asc');
- }
- public function image()
- {
- $model = "app\\common\\model\\UploadFile";
- return $this->belongsTo($model, 'image_id', 'file_id');
- }
- /**
- * 总库存=剩余库存+销量
- */
- public function getTotalStockAttr($value, $data) {
- return $data['stock'] + $data['sale_num'];
- }
- public function getStatusTextAttr($value, $data) {
- $text = str_replace([0,1], ['已下架', '上架中'], $data['status']);
- return $text;
- }
- /**
- * 获取记录
- * @param int $id
- * @param array $with
- * @return static
- */
- public static function detail(int $id, array $with = [])
- {
- return static::get($id, $with);
- }
- /**
- * 更新米卡数据
- * @param $orderIds
- * @param $data
- * @return false|int
- */
- public function onUpdate($card_id, $data)
- {
- return static::updateBase($data, [['id', '=', $card_id]]);
- }
- //更新数据
- public function updateRiceCard(array $data){
- return (new RiceCard)->updateAll($data);
-
- }
-
- }
|