123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php namespace App\Models;
- use App\Models\Traits\MultipUpdate;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- /**
- * 产品模型
- *
- */
- class Product extends Model
- {
- use HasFactory,MultipUpdate;
- // 与模型关联的表名
- protected $table = 'product';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['id'=>$id])->update($data);
- // 返回结果
- return $result;
- }
- /**
- * 编码转id
- *
- * @param string $code 编码
- *
- */
- public function codeToId($code){
- return intval(str_ireplace('klcp','',$code));
- }
- /**
- * id转编码
- *
- * @param int $id 编码
- *
- */
- public function idToCode($id){
- return 'klcp'. str_pad($id, 9, '0', STR_PAD_LEFT);;
- }
- /**
- * 描述文件更新
- *
- */
- public function updateDesc($id,$desc)
- {
- // 路径
- $path = public_path('uploads/product/detail/');
- // 如果路径不存在
- if( !is_dir($path) ) mkdir($path,0755,true);
- // 写入文件
- $result = file_put_contents($path.$id.'.html',$desc);
- // 返回结果
- return $result;
- }
- /**
- * 描述文件更新
- *
- */
- public function getDesc($id)
- {
- // 路径
- $path = public_path('uploads/product/detail/');
- // 如果路径不存在
- if( !is_dir($path) ) mkdir($path,0755,true);
- // 不存在文件
- if( !file_exists($path.$id.'.html') ) file_put_contents($path.$id.'.html','');
- // 写入文件
- $result = (string) file_get_contents($path.$id.'.html');
- // 返回结果
- return $result;
- }
- /**
- * 获取产品列表
- *
- * @param array $productIds 产品ID列表
- *
- */
- public function pluckList($productIds){
- // 查询数据
- $data = $this->query()->where([['status','=',0]])->whereIn('id',$productIds)->get(['id','name','price','market_price','stock'])->toArray();
- // 以id为主重组
- $list = [];
- // 循环
- foreach ($data as $value) {
- $list[$value['id']] = $value;
- }
- // 返回结果
- return $list;
- }
- /**
- * 库存更新
- *
- * @param int $productId 产品ID
- * @param int $incr 增减数量
- */
- public function updateStock($productId,$incr=1){
- // 返回结果
- return $this->query()->where([['id','=',$productId]])->update(['stock'=>DB::raw('stock+'.$incr)]);
- }
- /**
- * 获取产品信息
- *
- * @param int $id 产品ID
- * @param string $field 指定字段
- *
- */
- public function getOne($id,$field=''){
- // 返回结果
- $one = $this->query()->where([['id','=',$id]])->first(['id','name','spec','price','market_price','stock']);
- // 获取数据
- $one = $one ? $one->toArray() : [];
- // 返回值
- return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
- }
- /**
- * 获取多条数据
- *
- */
- public function getListByIds($productIds)
- {
- // 写入数据表
- $data = $this->query()->whereIn('id',$productIds)->where([['status','=',0]])->get(['id','name as product_name','thumb as product_thumb','spec as sku_attr_names','price','stock','status','business_id','quota','min_quota','quota_start','quota_end','tag_scope','tag_exclude'])->toArray();
- // 列表
- $list = [];
- // 循环处理
- foreach ($data as $value) {
- // 重组数据
- $list[$value['id']] = $value;
- }
- // 返回结果
- return $list;
- }
- }
|