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,$citys=[]) { // 写入数据表 $data = $this->query(); if( $citys ) $data = $data->join('product_city','product_city.product_id','=','product.id')->whereIn('product_city.city_id',$citys); $data = $data->whereIn('product.id',$productIds)->where([['product.status','=',0]])->get(['product.id','product.name as product_name','product.thumb as product_thumb','product.spec as sku_attr_names','product.price','product.stock','product.status','product.business_id','product.quota','product.min_quota','product.sku_min_quota_and','product.quota_start','product.quota_end','product.tag_scope','product.tag_exclude'])->toArray(); // 列表 $list = []; // 循环处理 foreach ($data as $value) { // 重组数据 $list[$value['id']] = $value; } // 返回结果 return $list; } }