REAPI/app/channel/service/ProductService.php
2024-09-29 15:43:18 +08:00

113 lines
2.7 KiB
PHP

<?php
namespace app\channel\service;
use app\core\Service;
//use app\merchant\service\MerchantRelationService;
use app\merchant\service\ProductService as MerchantRelationService;
/**
* 商品服务
* Class ChannelService
* @package app\channel\service
*/
class ProductService extends Service
{
/**
* 设置默认操作表
* @var string
*/
public $table = 'channel_product';
# 获取所有产品
public function getAll()
{
$where['status'] = 1;
$where['is_deleted'] = 0;
$data = $this->db()->where($where)->select();
return $data;
}
# 获取商品信息
public function getInfo($pid)
{
$where['id'] = $pid;
//$where['status'] = 1;
//$where['is_deleted'] = 0;
$data = $this->db()->where($where)->find();
return $data;
}
# 获取缓存
public function get($mid, $key, $get = true)
{
if (!$get) {
return false;
}
$cache_key = 'productv2_' . $mid . '_' . $key;
$data = !$get ? false : $this->cache($cache_key);
if (!$data) {
if (is_numeric($key) && $key > 0) {
$data = $this->getInfo($key);
if ($data) {
$this->cache($cache_key, $data);
}
} else {
$relation = MerchantRelationService::instance()->getInfo($mid, $key);
if ($relation) {
$data = $this->getInfo($relation['pid']);
if ($data) {
$this->cache($cache_key, $data);
}
} else {
$this->cache($cache_key, 'del');
}
}
}
if ($data) {
if ($data['status'] != 1) {
return false;
}
if ($data['is_deleted'] != 0) {
return false;
}
}
return $data;
}
# 获取商品信息
public function getInfoByKey($key, $id = false, $cids = '')
{
$where['key'] = $key;
$where['status'] = 1;
$where['is_deleted'] = 0;
$db = $this->db()->where($where);
$raw = array();
if ($id) {
$raw[] = ' id != ' . $id;
}
if ($cids) {
$raw[] = ' cid in('.$cids.')';
}
if ($raw) {
$raw = implode(' and ' , $raw);
$db->whereRaw($raw);
}
$data = $db->order('sort desc,id desc')->select()->toArray();
return $data;
}
# 根据关键字获得
public function getByKey($key, $id, $cids = '')
{
return $this->getInfoByKey($key, $id, $cids);
}
}