MSpec.Class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace JinDouYun\Model\GoodsManage;
  3. use JinDouYun\Cache\SpecCache;
  4. use JinDouYun\Dao\GoodsManage\DSpec;
  5. use Mall\Framework\Core\ErrorCode;
  6. use Mall\Framework\Core\ResultWrapper;
  7. use Mall\Framework\Core\StatusCode;
  8. /**
  9. * 多规格
  10. * Class MSpec
  11. * @package JinDouYun\Model\GoodsManage
  12. */
  13. class MSpec
  14. {
  15. /**
  16. * 用户ID
  17. * @var
  18. */
  19. private $onlineUserId;
  20. /**
  21. * 企业ID
  22. * @var
  23. */
  24. private $onlineEnterpriseId;
  25. /**
  26. * 规格名表
  27. * @var DSpec
  28. */
  29. private $objDSpec;
  30. /**
  31. * 规格缓存
  32. * @var SpecCache
  33. */
  34. private $objSpecCache;
  35. /**
  36. * MSpec constructor.
  37. * @param $onlineUserId
  38. * @param $onlineEnterpriseId
  39. * @throws \Exception
  40. */
  41. public function __construct($onlineUserId, $onlineEnterpriseId)
  42. {
  43. $this->onlineUserId = $onlineUserId;
  44. $this->onlineEnterpriseId = $onlineEnterpriseId;
  45. $this->objDSpec = new DSpec('default');
  46. $this->objDSpec->setTable($this->objDSpec->get_Table() . '_' . $onlineEnterpriseId);
  47. $this->objSpecCache = new SpecCache($this->onlineEnterpriseId);
  48. }
  49. /**
  50. * 获取所有规格
  51. * @param $selectParams
  52. * @return ResultWrapper
  53. */
  54. public function getAll($selectParams)
  55. {
  56. $limit = $selectParams['limit'];
  57. unset($selectParams['limit']);
  58. $offset = $selectParams['offset'];
  59. unset($selectParams['offset']);
  60. $selectParams['deleteStatus'] = StatusCode::$standard;
  61. $selectParams['pid'] = 0;//属性名
  62. //$selectParams['hidden'] = StatusCode::$standard;//正常显示的
  63. $dbResult = $this->objDSpec->select($selectParams, 'id,specName', 'createTime DESC', $limit, $offset);
  64. if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  65. $total = $this->objDSpec->count($selectParams);
  66. $formatRes = self::formatChild($dbResult);
  67. if (!$formatRes->isSuccess()) {
  68. return ResultWrapper::fail($formatRes->getData(), $formatRes->getErrorCode());
  69. }
  70. $return = [
  71. 'data' => $formatRes->getData(),
  72. 'total' => ($total) ? intval($total) : 0,
  73. ];
  74. return ResultWrapper::success($return);
  75. }
  76. /**
  77. * 格式化规格属性值
  78. * @param $data
  79. * @return ResultWrapper
  80. */
  81. public function formatChild($data)
  82. {
  83. if (empty($data)) return ResultWrapper::success($data);
  84. $dbResult = $this->objDSpec->select(
  85. ['pid' => array_column($data, 'id'), 'deleteStatus' => StatusCode::$standard],
  86. 'id,specName,pid',
  87. 'createTime DESC'
  88. );
  89. if ($dbResult === false) {
  90. return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  91. }
  92. $mapping = [];
  93. foreach ($dbResult as $value) {
  94. $value['type'] = StatusCode::$delete;//属性值
  95. $mapping[$value['pid']][] = $value;
  96. }
  97. foreach ($data as &$spec) {
  98. $spec['type'] = StatusCode::$standard;//属性名
  99. $spec['child'] = isset($mapping[$spec['id']]) ? $mapping[$spec['id']] : [];
  100. }
  101. return ResultWrapper::success($data);
  102. }
  103. /**
  104. * 添加规格
  105. * @param $data
  106. * @return ResultWrapper
  107. */
  108. public function add($data)
  109. {
  110. /**$cache = $this->objSpecCache->getIdBySpecName($data['specName']);
  111. * if ($cache > 0) {
  112. * return ResultWrapper::fail('名称已存在', ErrorCode::$paramError);
  113. * }**/
  114. // 查询新增的属性是否已经存在
  115. $dbResult = $this->objDSpec->get([
  116. 'specName' => $data['specName'],
  117. 'pid' => $data['pid'],
  118. 'deleteStatus' => StatusCode::$standard,
  119. ]);
  120. if ($dbResult === false) {
  121. return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  122. }
  123. if (!empty($dbResult)){
  124. return ResultWrapper::fail('名称已存在',ErrorCode::$dberror);
  125. }
  126. $dbResult = $this->objDSpec->insert([
  127. 'specName' => $data['specName'],
  128. 'pid' => $data['pid']
  129. ]);
  130. if ($dbResult === false) {
  131. return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  132. }
  133. //写缓存
  134. $cacheResult = $this->objSpecCache->cacheSpecNameRelId($data['specName'], $dbResult);
  135. if ($cacheResult === false) {
  136. return ResultWrapper::fail('属性写入redis失败', ErrorCode::$redisWriteError);
  137. }
  138. return ResultWrapper::success($dbResult);
  139. }
  140. /**
  141. * 编辑
  142. * @param $data
  143. * @return ResultWrapper
  144. */
  145. public function edit($data)
  146. {
  147. $dbResult = $this->objDSpec->update(['specName' => $data['specName'], 'updateTime' => time()], ['id' => $data['id']]);
  148. if ($dbResult === false) {
  149. return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  150. }
  151. //改缓存
  152. $this->objSpecCache->delSpecNameRelId($data['id']);
  153. $this->objSpecCache->cacheSpecNameRelId($data['specName'], $data['id']);
  154. return ResultWrapper::success($dbResult);
  155. }
  156. /**
  157. * 删除
  158. * @param $id
  159. * @return ResultWrapper
  160. */
  161. public function del($id)
  162. {
  163. $pid = $this->objDSpec->get_field('pid', ['id' => $id]);
  164. if ($pid === false) {
  165. return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  166. }
  167. if ($pid == 0) {
  168. //是属性名
  169. $count = $this->objDSpec->count(['pid' => $id, 'deleteStatus' => StatusCode::$standard]);
  170. if ($count > 0) {
  171. return ResultWrapper::fail('请先删除属性值', ErrorCode::$paramError);
  172. }
  173. }
  174. $dbResult = $this->objDSpec->update(['deleteStatus' => StatusCode::$delete], $id);
  175. if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  176. $this->objSpecCache->delSpecNameRelId($id);
  177. return ResultWrapper::success($dbResult);
  178. }
  179. /**
  180. * 详情
  181. * @param $id
  182. * @return ResultWrapper
  183. */
  184. public function info($id)
  185. {
  186. $dbResult = $this->objDSpec->get_by('id', $id, 'id,specName,pid');
  187. if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  188. return ResultWrapper::success($dbResult);
  189. }
  190. /**
  191. * 获取属性名
  192. * @param $selectParams
  193. * @return ResultWrapper
  194. */
  195. public function getAllSpecName($selectParams)
  196. {
  197. $limit = $selectParams['limit'];
  198. unset($selectParams['limit']);
  199. $offset = $selectParams['offset'];
  200. unset($selectParams['offset']);
  201. $where = [
  202. 'pid' => 0,
  203. 'hidden' => StatusCode::$standard,//正常的
  204. ];
  205. $dbResult = $this->objDSpec->select(
  206. $where,
  207. 'id,specName',
  208. 'createTime DESC', $limit, $offset);
  209. if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  210. $total = $this->objDSpec->count($where);
  211. $return = [
  212. 'data' => $dbResult,
  213. 'total' => ($total) ? intval($total) : 0,
  214. ];
  215. return ResultWrapper::success($return);
  216. }
  217. /*
  218. * pid => specValue
  219. * @param $selectParams
  220. * @return ResultWrapper
  221. */
  222. public function getAllSpecValByPid($selectParams)
  223. {
  224. $limit = $selectParams['limit'];
  225. unset($selectParams['limit']);
  226. $offset = $selectParams['offset'];
  227. unset($selectParams['offset']);
  228. $selectParams['hidden'] = StatusCode::$standard;
  229. $dbResult = $this->objDSpec->select($selectParams, 'id,specName', 'createTime DESC', $limit, $offset);
  230. if ($dbResult === false) return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  231. $total = $this->objDSpec->count($selectParams);
  232. $return = [
  233. 'data' => $dbResult,
  234. 'total' => ($total) ? intval($total) : 0,
  235. ];
  236. return ResultWrapper::success($return);
  237. }
  238. /**
  239. * 添加自定义属性
  240. * @param $data
  241. * @return ResultWrapper
  242. */
  243. public function defineSpec($data)
  244. {
  245. $dbResult = $this->objDSpec->get(['pid' => $data['pid'], 'specName' => $data['specName']]);
  246. if ($dbResult === false) {
  247. return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  248. }
  249. if (!empty($dbResult)) {
  250. //数据库中存在
  251. return ResultWrapper::success([
  252. 'specName' => $dbResult['specName'],
  253. 'id' => (string)$dbResult['id'],
  254. 'isHave' => true
  255. ]);
  256. }
  257. $id = $this->objDSpec->insert([
  258. 'specName' => $data['specName'],
  259. 'pid' => $data['pid'],
  260. 'hidden' => $data['hidden']
  261. ]);
  262. if ($id === false) {
  263. return ResultWrapper::fail($this->objDSpec->error(), ErrorCode::$dberror);
  264. }
  265. $this->objSpecCache->cacheSpecNameRelId($data['specName'], $id);
  266. return ResultWrapper::success(['specName' => $data['specName'], 'id' => $id,'isHave'=>false]);
  267. }
  268. }