123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace JinDouYun\Model\GoodsManage;
- use JinDouYun\Cache\SpecCache;
- use JinDouYun\Dao\GoodsManage\DSpec;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- /**
- * 多规格
- * Class MSpec
- * @package JinDouYun\Model\GoodsManage
- */
- class MSpec
- {
- /**
- * 用户ID
- * @var
- */
- private $onlineUserId;
- /**
- * 企业ID
- * @var
- */
- private $onlineEnterpriseId;
- /**
- * 规格名表
- * @var DSpec
- */
- private $objDSpec;
- /**
- * 规格缓存
- * @var SpecCache
- */
- private $objSpecCache;
- /**
- * MSpec constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- * @throws \Exception
- */
- public function __construct($onlineUserId, $onlineEnterpriseId)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- $this->objDSpec = new DSpec('default');
- $this->objDSpec->setTable($this->objDSpec->get_Table() . '_' . $onlineEnterpriseId);
- $this->objSpecCache = new SpecCache($this->onlineEnterpriseId);
- }
- /**
- * 获取所有规格
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAll($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $selectParams['pid'] = 0;//属性名
- //$selectParams['hidden'] = StatusCode::$standard;//正常显示的
- $dbResult = $this->objDSpec->select($selectParams, 'id,specName', 'createTime DESC', $limit, $offset);
- if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- $total = $this->objDSpec->count($selectParams);
- $formatRes = self::formatChild($dbResult);
- if (!$formatRes->isSuccess()) {
- return ResultWrapper::fail($formatRes->getData(), $formatRes->getErrorCode());
- }
- $return = [
- 'data' => $formatRes->getData(),
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 格式化规格属性值
- * @param $data
- * @return ResultWrapper
- */
- public function formatChild($data)
- {
- if (empty($data)) return ResultWrapper::success($data);
- $dbResult = $this->objDSpec->select(
- ['pid' => array_column($data, 'id'), 'deleteStatus' => StatusCode::$standard],
- 'id,specName,pid',
- 'createTime DESC'
- );
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- }
- $mapping = [];
- foreach ($dbResult as $value) {
- $value['type'] = StatusCode::$delete;//属性值
- $mapping[$value['pid']][] = $value;
- }
- foreach ($data as &$spec) {
- $spec['type'] = StatusCode::$standard;//属性名
- $spec['child'] = isset($mapping[$spec['id']]) ? $mapping[$spec['id']] : [];
- }
- return ResultWrapper::success($data);
- }
- /**
- * 添加规格
- * @param $data
- * @return ResultWrapper
- */
- public function add($data)
- {
- /**$cache = $this->objSpecCache->getIdBySpecName($data['specName']);
- * if ($cache > 0) {
- * return ResultWrapper::fail('名称已存在', ErrorCode::$paramError);
- * }**/
- // 查询新增的属性是否已经存在
- $dbResult = $this->objDSpec->get([
- 'specName' => $data['specName'],
- 'pid' => $data['pid'],
- 'deleteStatus' => StatusCode::$standard,
- ]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- }
- if (!empty($dbResult)){
- return ResultWrapper::fail('名称已存在',ErrorCode::$dberror);
- }
-
- $dbResult = $this->objDSpec->insert([
- 'specName' => $data['specName'],
- 'pid' => $data['pid']
- ]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- }
- //写缓存
- $cacheResult = $this->objSpecCache->cacheSpecNameRelId($data['specName'], $dbResult);
- if ($cacheResult === false) {
- return ResultWrapper::fail('属性写入redis失败', ErrorCode::$redisWriteError);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 编辑
- * @param $data
- * @return ResultWrapper
- */
- public function edit($data)
- {
- $dbResult = $this->objDSpec->update(['specName' => $data['specName'], 'updateTime' => time()], ['id' => $data['id']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- }
- //改缓存
- $this->objSpecCache->delSpecNameRelId($data['id']);
- $this->objSpecCache->cacheSpecNameRelId($data['specName'], $data['id']);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 删除
- * @param $id
- * @return ResultWrapper
- */
- public function del($id)
- {
- $pid = $this->objDSpec->get_field('pid', ['id' => $id]);
- if ($pid === false) {
- return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- }
- if ($pid == 0) {
- //是属性名
- $count = $this->objDSpec->count(['pid' => $id, 'deleteStatus' => StatusCode::$standard]);
- if ($count > 0) {
- return ResultWrapper::fail('请先删除属性值', ErrorCode::$paramError);
- }
- }
- $dbResult = $this->objDSpec->update(['deleteStatus' => StatusCode::$delete], $id);
- if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- $this->objSpecCache->delSpecNameRelId($id);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 详情
- * @param $id
- * @return ResultWrapper
- */
- public function info($id)
- {
- $dbResult = $this->objDSpec->get_by('id', $id, 'id,specName,pid');
- if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取属性名
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllSpecName($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $where = [
- 'pid' => 0,
- 'hidden' => StatusCode::$standard,//正常的
- ];
- $dbResult = $this->objDSpec->select(
- $where,
- 'id,specName',
- 'createTime DESC', $limit, $offset);
- if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- $total = $this->objDSpec->count($where);
- $return = [
- 'data' => $dbResult,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /*
- * pid => specValue
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllSpecValByPid($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['hidden'] = StatusCode::$standard;
- $dbResult = $this->objDSpec->select($selectParams, 'id,specName', 'createTime DESC', $limit, $offset);
- if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- $total = $this->objDSpec->count($selectParams);
- $return = [
- 'data' => $dbResult,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 添加自定义属性
- * @param $data
- * @return ResultWrapper
- */
- public function defineSpec($data)
- {
- $dbResult = $this->objDSpec->get(['pid' => $data['pid'], 'specName' => $data['specName']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- }
- if (!empty($dbResult)) {
- //数据库中存在
- return ResultWrapper::success([
- 'specName' => $dbResult['specName'],
- 'id' => (string)$dbResult['id'],
- 'isHave' => true
- ]);
- }
- $id = $this->objDSpec->insert([
- 'specName' => $data['specName'],
- 'pid' => $data['pid'],
- 'hidden' => $data['hidden']
- ]);
- if ($id === false) {
- return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
- }
- $this->objSpecCache->cacheSpecNameRelId($data['specName'], $id);
- return ResultWrapper::success(['specName' => $data['specName'], 'id' => $id,'isHave'=>false]);
- }
- }
|