<?php
namespace app\common\model\wow;
use app\common\model\BaseModel;
class Shoping extends BaseModel
{
protected $name = 'wow_shoping';
protected $updateTime = false;
protected $alias = 'shoping';
public function goods()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsTo("app\\{$module}\\model\\Goods");
}
public function user()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsTo("app\\{$module}\\model\\User");
}
public static function detail($id, $with = ['goods.image.file', 'user'])
{
return static::get($id, $with);
}
public function add($userId, $goodsIds)
{
$newGoodsIds = $this->getFilterGoodsIds($userId, $goodsIds);
if (empty($newGoodsIds)) {
return false;
}
$saveData = [];
foreach ($newGoodsIds as $goodsId) {
$saveData[] = [
'goods_id' => $goodsId,
'user_id' => $userId,
'wxapp_id' => self::$wxapp_id,
];
}
return $this->isUpdate(false)->saveAll($saveData);
}
public function setDelete()
{
return $this->save(['is_delete' => 1]);
}
private function getFilterGoodsIds($userId, $newGoodsIds)
{
$alreadyGoodsId = $this->where('user_id', '=', $userId)
->where('is_delete', '=', 0)
->column('goods_id');
return array_diff($newGoodsIds, $alreadyGoodsId);
}
}