209 lines
7.0 KiB
PHP
209 lines
7.0 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Model;
|
||
|
||
|
||
|
||
use Hyperf\Database\Model\Collection;
|
||
use Hyperf\Database\Model\Relations\HasOne;
|
||
use Hyperf\Snowflake\Concern\Snowflake;
|
||
|
||
/**
|
||
* @property int $product_id 主键id
|
||
* @property int $product_platform_id 处方平台商品id
|
||
* @property int $product_status 商品状态(1:正常 2:下架)
|
||
* @property int $is_delete 是否删除(0:否 1:是)
|
||
* @property int $prescription_num 处方可开具的数量
|
||
* @property string $product_name 商品名称
|
||
* @property string $common_name 商品通用名
|
||
* @property string $product_price 商品价格
|
||
* @property string $mnemonic_code 商品助记码(首字母简拼)
|
||
* @property int $product_type 药品类型(0:未知 1:中成药 2:西药)
|
||
* @property string $product_platform_code 处方平台商品编码
|
||
* @property string $product_pharmacy_code 第三方药店商品编码
|
||
* @property string $product_cover_img 商品封面图
|
||
* @property string $product_spec 商品规格
|
||
* @property string $license_number 批准文号
|
||
* @property string $manufacturer 生产厂家
|
||
* @property string $single_unit 单次剂量(例:1次1包)
|
||
* @property string $single_use 单次用法(例:口服)
|
||
* @property string $packaging_unit 基本包装单位(例:盒/瓶)
|
||
* @property string $frequency_use 使用频率(例:1天3次)
|
||
* @property string $available_days 可用天数(3)
|
||
* @property string $product_remarks 商品备注
|
||
* @property \Carbon\Carbon $created_at 创建时间
|
||
* @property \Carbon\Carbon $updated_at 修改时间
|
||
* @property-read ProductPlatformAmount|null $ProductPlatformAmount
|
||
*/
|
||
class Product extends Model
|
||
{
|
||
use Snowflake;
|
||
|
||
/**
|
||
* The table associated with the model.
|
||
*/
|
||
protected ?string $table = 'product';
|
||
|
||
/**
|
||
* The attributes that are mass assignable.
|
||
*/
|
||
protected array $fillable = ['product_id', 'product_platform_id', 'product_status', 'is_delete', 'prescription_num', 'product_name', 'common_name', 'product_price', 'mnemonic_code', 'product_type', 'product_platform_code', 'product_pharmacy_code', 'product_cover_img', 'product_spec', 'license_number', 'manufacturer', 'single_unit', 'single_use', 'packaging_unit', 'frequency_use', 'available_days', 'product_remarks', 'created_at', 'updated_at'];
|
||
|
||
protected string $primaryKey = "product_id";
|
||
|
||
/**
|
||
* 关联库存表
|
||
*/
|
||
public function ProductPlatformAmount(): HasOne
|
||
{
|
||
return $this->hasOne(ProductPlatformAmount::class, 'product_platform_id','product_platform_id');
|
||
}
|
||
|
||
/**
|
||
* 获取信息-单条
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return object|null
|
||
*/
|
||
public static function getOne(array $params, array $fields = ['*']): object|null
|
||
{
|
||
return self::where($params)->first($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取库存信息-单条
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return object|null
|
||
*/
|
||
public static function getWithAmountOne(array $params, array $fields = ['*']): object|null
|
||
{
|
||
return self::with([
|
||
'ProductPlatformAmount'
|
||
])
|
||
->where($params)
|
||
->first($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取数据-多
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return Collection|array
|
||
*/
|
||
public static function getList(array $params = [], array $fields = ['*']): Collection|array
|
||
{
|
||
return self::where($params)->get($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取关键字搜索列表
|
||
* @param array $params
|
||
* @param string $keyword
|
||
* @param array $fields
|
||
* @return Collection|array
|
||
*/
|
||
public static function getSearchKeywordList(array $params = [],string $keyword = '',array $fields = ['*']): Collection|array
|
||
{
|
||
return self::with([
|
||
'ProductPlatformAmount:amount_id,product_platform_id,stock'
|
||
])
|
||
->when($keyword, function ($query, $keyword) {
|
||
$query->where(function ($query) use ($keyword) {
|
||
$query->orwhere("product_name", 'like', '%' . $keyword . '%');
|
||
$query->orwhere("common_name", 'like', '%' . $keyword . '%');
|
||
$query->orwhere("mnemonic_code", 'like', '%' . $keyword . '%');
|
||
});
|
||
})
|
||
->where($params)
|
||
->get($fields);
|
||
}
|
||
|
||
/**
|
||
* 修改
|
||
* @param array $params
|
||
* @param array $data
|
||
* @return int
|
||
*/
|
||
public static function edit(array $params = [], array $data = []): int
|
||
{
|
||
return self::where($params)->update($data);
|
||
}
|
||
|
||
/**
|
||
* 获取数量
|
||
* @param array $params
|
||
* @return int
|
||
*/
|
||
public static function getCount(array $params): int
|
||
{
|
||
return self::where($params)->count();
|
||
}
|
||
|
||
/**
|
||
* 获取列表-分页
|
||
* @param array $params 条件
|
||
* @param array $fields 字段
|
||
* @param int|null $page 页码
|
||
* @param int|null $per_page 每页个数
|
||
* @return array
|
||
*/
|
||
public static function getPage(array $params, array $fields = ["*"], int $page = null, ?int $per_page = 10): array
|
||
{
|
||
$raw = self::where($params)->paginate($per_page, $fields, "page", $page);
|
||
$data = array();
|
||
$data['current_page'] = $raw->currentPage();// 当前页码
|
||
$data['total'] = $raw->total();//数据总数
|
||
$data['data'] = $raw->items();//数据
|
||
$data['per_page'] = $raw->perPage();//每页个数
|
||
$data['last_page'] = $raw->lastPage();//最后一页
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 获取列表-分页
|
||
* @param array $params 条件
|
||
* @param string $keyword 搜索条件
|
||
* @param array $fields 字段
|
||
* @param int|null $page 页码
|
||
* @param int|null $per_page 每页个数
|
||
* @return array
|
||
*/
|
||
public static function getWithAmountPage(array $params,string $keyword, array $fields = ["*"], int $page = null, ?int $per_page = 10): array
|
||
{
|
||
$raw = self::with([
|
||
'ProductPlatformAmount:amount_id,product_platform_id,stock'
|
||
])
|
||
->when($keyword, function ($query, $keyword) {
|
||
$query->where(function ($query) use ($keyword) {
|
||
$query->orwhere("product_name", 'like', '%' . $keyword . '%');
|
||
$query->orwhere("common_name", 'like', '%' . $keyword . '%');
|
||
$query->orwhere("mnemonic_code", 'like', '%' . $keyword . '%');
|
||
});
|
||
})
|
||
->where($params)->paginate($per_page, $fields, "page", $page);
|
||
|
||
$data = array();
|
||
$data['current_page'] = $raw->currentPage();// 当前页码
|
||
$data['total'] = $raw->total();//数据总数
|
||
$data['data'] = $raw->items();//数据
|
||
$data['per_page'] = $raw->perPage();//每页个数
|
||
$data['last_page'] = $raw->lastPage();//最后一页
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 新增
|
||
* @param array $data
|
||
* @return \Hyperf\Database\Model\Model|Product
|
||
*/
|
||
public static function addProduct(array $data): \Hyperf\Database\Model\Model|Product
|
||
{
|
||
return self::create($data);
|
||
}
|
||
}
|