MGoodsBrand.Class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * 商品品牌数据模型
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/5
  7. * Time: 11:21
  8. */
  9. namespace JinDouYun\Model\GoodsManage;
  10. use JinDouYun\Controller\Common\Logger;
  11. use JinDouYun\Dao\GoodsManage\DGoodsBasic;
  12. use Mall\Framework\Core\ErrorCode;
  13. use JinDouYun\Cache\GoodsBasicRelevant;
  14. use JinDouYun\Model\Purchase\MSupplier;
  15. use JinDouYun\Cache\GoodsBasicCache;
  16. use JinDouYun\Dao\GoodsManage\DGoodsBrand;
  17. use Mall\Framework\Core\ResultWrapper;
  18. use Mall\Framework\Core\StatusCode;
  19. class MGoodsBrand
  20. {
  21. private $objDGoodsBrand;
  22. private $onlineUserId;
  23. private $onlineEnterpriseId;
  24. private $objGoodsBasicRelevantCache;
  25. /**
  26. * MGoodsBrand constructor.
  27. * @param $onlineUserId
  28. * @param $onlineEnterpriseId
  29. * @throws \Exception
  30. */
  31. public function __construct($onlineUserId, $onlineEnterpriseId)
  32. {
  33. $this->onlineUserId = $onlineUserId;
  34. $this->onlineEnterpriseId = $onlineEnterpriseId;
  35. $this->objDGoodsBrand = new DGoodsBrand('default');
  36. $this->objDGoodsBrand->setTable($this->objDGoodsBrand->get_Table() . '_' . $onlineEnterpriseId);
  37. $this->objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->onlineEnterpriseId);
  38. }
  39. /**
  40. * @param $id (企业id)
  41. * @throws \Exception
  42. */
  43. public function goodsBrandSubTable($id)
  44. {
  45. $this->objDGoodsBrand->setTable('qianniao_goods_brand_' . $id);
  46. }
  47. /**
  48. * 添加品牌
  49. * @param $params
  50. * @return ResultWrapper
  51. * @throws \Exception
  52. */
  53. public function addBrand($params)
  54. {
  55. $this->objDGoodsBrand->beginTransaction();
  56. $dbResult = $this->objDGoodsBrand->insert($params);
  57. if ($dbResult === false) {
  58. $this->objDGoodsBrand->rollBack();
  59. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  60. }
  61. $code = createCode(StatusCode::$code['brand']['prefix'], $dbResult, StatusCode::$code['brand']['length']);
  62. $dbResult = $this->objDGoodsBrand->update(['code'=>$code],$dbResult);
  63. if ($dbResult === false){
  64. $this->objDGoodsBrand->rollBack();
  65. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  66. }
  67. $this->objGoodsBasicRelevantCache->cacheBrandIdRelationName($params['title'], $dbResult);
  68. $this->objDGoodsBrand->commit();
  69. return ResultWrapper::success($dbResult);
  70. }
  71. /**
  72. * 编辑品牌
  73. * @param $params
  74. * @return ResultWrapper
  75. * @throws \Exception
  76. */
  77. public function editBrand($params)
  78. {
  79. if (empty($params['id'])) {
  80. return ResultWrapper::fail('没有指定要修改品牌id', ErrorCode::$paramError);
  81. }
  82. $updateBrandId = $params['id'];
  83. unset($params['id']);
  84. $dbResult = $this->objDGoodsBrand->update($params, $updateBrandId);
  85. if ($dbResult === false) {
  86. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  87. }
  88. $this->objGoodsBasicRelevantCache->deleteBrandKeyById($updateBrandId);
  89. $this->objGoodsBasicRelevantCache->cacheBrandIdRelationName($params['title'], $updateBrandId);
  90. return ResultWrapper::success($dbResult);
  91. }
  92. /**
  93. * 获取指定id的品牌信息
  94. * @param $params
  95. * @return ResultWrapper
  96. * @throws \Exception
  97. */
  98. public function getBrandInfoById($id)
  99. {
  100. $dbResult = $this->objDGoodsBrand->get_by('id', $id);
  101. if ($dbResult === false) {
  102. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  103. } else {
  104. $formatResult = self::formatSupplier($dbResult);
  105. if (!empty($formatResult)) {
  106. $dbResult = $formatResult[0];
  107. }
  108. return ResultWrapper::success($dbResult);
  109. }
  110. }
  111. /**
  112. * 品牌的显示和隐藏
  113. * @param $params
  114. * @return ResultWrapper
  115. * @throws \Exception
  116. */
  117. public function updateBrandStatus($params)
  118. {
  119. $dbResult = $this->objDGoodsBrand->update(['enableStatus' => $params['enableStatus']], $params['id']);
  120. if ($dbResult === false) {
  121. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  122. } else {
  123. return ResultWrapper::success($dbResult);
  124. }
  125. }
  126. /**
  127. * 获取所有品牌列表
  128. * @param $selectParams
  129. * @return ResultWrapper
  130. * @throws \Exception
  131. */
  132. public function getAllBrand($selectParams)
  133. {
  134. $limit = $selectParams['limit'];
  135. unset($selectParams['limit']);
  136. $offset = $selectParams['offset'];
  137. unset($selectParams['offset']);
  138. $whereSql = ' WHERE deleteStatus = ' . StatusCode::$standard;
  139. if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])) {
  140. $whereSql .= ' AND (title LIKE "%' . $selectParams['keyword'] . '%" OR code LIKE "%'.$selectParams['keyword'].'%")';
  141. }
  142. if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])) {
  143. $whereSql .= ' AND id in (SELECT brandId from qianniao_goods_basic_'.$this->onlineEnterpriseId.' where FIND_IN_SET('.$selectParams['categoryId'].',categoryPath) and brandid != 0)';
  144. }
  145. $sql = 'SELECT * FROM '.$this->objDGoodsBrand->get_Table().$whereSql.' ORDER BY sort DESC LIMIT '.$offset.' , '.$limit;
  146. $totalSql = 'SELECT COUNT(1) as count FROM '.$this->objDGoodsBrand->get_Table().$whereSql;
  147. // $where = 'deleteStatus=' . StatusCode::$standard;
  148. // if (isset($selectParams['keyword'])) {
  149. // $where .= ' AND (title LIKE "%' . $selectParams['keyword'] . '%" OR code LIKE "%'.$selectParams['keyword'].'%")';
  150. // }
  151. $dbResult = $this->objDGoodsBrand->query($sql);
  152. $dbTotalResult = $this->objDGoodsBrand->query($totalSql);
  153. if ($dbResult === false) {
  154. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  155. }
  156. if ($dbTotalResult === false) {
  157. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  158. }
  159. // $total = $this->objDGoodsBrand->count($where);
  160. $return = [
  161. 'data' => self::formatSupplier(self::format($dbResult)),
  162. 'total' => $dbTotalResult[0]['count'],
  163. ];
  164. return ResultWrapper::success($return);
  165. }
  166. /**
  167. * 获取所有品牌列表(小程序)
  168. * @param $selectParams
  169. * @return ResultWrapper
  170. * @throws \Exception
  171. * sql SELECT * FROM qianniao_goods_brand_64 WHERE deleteStatus = 5 AND id in (select b.brandId from qianniao_goods_basic_64 as b LEFT JOIN qianniao_goods_64 as g on b.id=g.basicGoodsId where b.enableStatus = 5 and FIND_IN_SET(1,b.categoryPath) and b.brandid != 0) ORDER BY sort DESC LIMIT 0 , 10
  172. */
  173. public function apiGetAllBrand($selectParams)
  174. {
  175. $limit = $selectParams['limit'];
  176. unset($selectParams['limit']);
  177. $offset = $selectParams['offset'];
  178. unset($selectParams['offset']);
  179. $whereSql = ' WHERE deleteStatus = ' . StatusCode::$standard;
  180. if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])) {
  181. $whereSql .= ' AND (title LIKE "%' . $selectParams['keyword'] . '%" OR code LIKE "%'.$selectParams['keyword'].'%")';
  182. }
  183. if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])) {
  184. $whereSql .= ' AND id in (select b.brandId from qianniao_goods_basic_'.$this->onlineEnterpriseId.' as b LEFT JOIN qianniao_goods_'.$this->onlineEnterpriseId.' as g on b.id=g.basicGoodsId where g.enableStatus = '.StatusCode::$standard.' and b.enableStatus = '.StatusCode::$standard.' and FIND_IN_SET('.$selectParams['categoryId'].',b.categoryPath) and b.brandid != 0)';
  185. }
  186. $sql = 'SELECT * FROM '.$this->objDGoodsBrand->get_Table().$whereSql.' ORDER BY sort DESC LIMIT '.$offset.' , '.$limit;
  187. $totalSql = 'SELECT COUNT(1) as count FROM '.$this->objDGoodsBrand->get_Table().$whereSql;
  188. // $where = 'deleteStatus=' . StatusCode::$standard;
  189. // if (isset($selectParams['keyword'])) {
  190. // $where .= ' AND (title LIKE "%' . $selectParams['keyword'] . '%" OR code LIKE "%'.$selectParams['keyword'].'%")';
  191. // }
  192. $dbResult = $this->objDGoodsBrand->query($sql);
  193. $dbTotalResult = $this->objDGoodsBrand->query($totalSql);
  194. if ($dbResult === false) {
  195. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  196. }
  197. if ($dbTotalResult === false) {
  198. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  199. }
  200. // $total = $this->objDGoodsBrand->count($where);
  201. $return = [
  202. 'data' => self::formatSupplier(self::format($dbResult)),
  203. 'total' => $dbTotalResult[0]['count'],
  204. ];
  205. return ResultWrapper::success($return);
  206. }
  207. /**
  208. * 删除分类
  209. * @param $id
  210. * @return ResultWrapper
  211. * @throws \Exception
  212. */
  213. public function delBrand($id)
  214. {
  215. if (empty($id)) {
  216. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  217. }
  218. $dbResult = $this->objDGoodsBrand->update(['deleteStatus' => StatusCode::$delete], $id);
  219. if ($dbResult === false) {
  220. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  221. }
  222. foreach ($id as $brandId) {
  223. $this->objGoodsBasicRelevantCache->deleteBrandKeyById($brandId);
  224. }
  225. return ResultWrapper::success($dbResult);
  226. }
  227. /**
  228. * 格式化数据
  229. * @param $data
  230. * @param $enterpriseId
  231. * @return mixed
  232. * @throws \Exception
  233. */
  234. public function format($data)
  235. {
  236. //$objGoodsBasicCache = new GoodsBasicCache($this->onlineEnterpriseId);
  237. $allBrandMap = self::getBrandBasicTotal(array_column($data,'id'));
  238. foreach ($data as $key => $value) {
  239. //$data[$key]['goodsBasicTotal'] = $objGoodsBasicCache->getBrandGoodsBasicNum($value['id']);
  240. $data[$key]['goodsBasicTotal'] = isset($allBrandMap[$value['id']]) ? $allBrandMap[$value['id']] : 0;
  241. $data[$key]['code'] = createCode(StatusCode::$code['brand']['prefix'], $value['id'], StatusCode::$code['brand']['length']);
  242. }
  243. return $data;
  244. }
  245. /**
  246. * Doc: (des="")
  247. * User: XMing
  248. * Date: 2020/7/24
  249. * Time: 11:25 上午
  250. * @param array $ids
  251. * @return array
  252. */
  253. public function getBrandBasicTotal(array $ids)
  254. {
  255. $sql = 'SELECT count(id) as num,brandId FROM qianniao_goods_basic_'.$this->onlineEnterpriseId.' WHERE deleteStatus = '.StatusCode::$standard.' GROUP BY brandId';
  256. $dbResult = $this->objDGoodsBrand->query($sql);
  257. if ($dbResult === false){
  258. return [];
  259. }
  260. $map = [];
  261. foreach ($dbResult as $row){
  262. $map[$row['brandId']] = $row['num'];
  263. }
  264. return $map;
  265. }
  266. /**
  267. * 根据品牌id获取品牌名称
  268. * @param $selectParams
  269. * @param $enterpriseId
  270. * @return ResultWrapper
  271. * @throws \Exception
  272. */
  273. public function getBrandTitleByBrandId($selectParams)
  274. {
  275. $dbResult = $this->objDGoodsBrand->select($selectParams, 'id,title');
  276. if ($dbResult === false) {
  277. Logger::logs(E_USER_ERROR,'sql error',__CLASS__,__LINE__,$this->objDGoodsBrand->error());
  278. return ResultWrapper::fail($this->objDGoodsBrand->error(), ErrorCode::$dberror);
  279. }
  280. return ResultWrapper::success($dbResult);
  281. }
  282. /**
  283. * 格式化数据
  284. * @param $data
  285. * @return array
  286. * @throws \Exception
  287. */
  288. private function formatSupplier($data)
  289. {
  290. if (isset($data['id'])) {
  291. $dimension = $data;
  292. unset($data);
  293. $data[] = $dimension;
  294. }
  295. $allSupplierIds = [];
  296. foreach ($data as $key => $val) {
  297. if (strlen($val['supplier']) > 1) {
  298. foreach (explode(',', $val['supplier']) as $k => $v) {
  299. $allSupplierIds[] = $v;
  300. }
  301. } else {
  302. $allSupplierIds[] = $val['supplier'];
  303. }
  304. }
  305. $allSupplierIds = array_values(array_unique($allSupplierIds));
  306. $objMSupplier = new MSupplier($this->onlineUserId, $this->onlineEnterpriseId);
  307. $dbResult = $objMSupplier->getSupplierNameByIds($allSupplierIds);
  308. if (!$dbResult->isSuccess()) {
  309. return $data;
  310. }
  311. $supplierData = $dbResult->getData();
  312. //创建映射关系
  313. foreach ($supplierData as $key => $value) {
  314. $allSupplierData[$value['id']] = $value;
  315. }
  316. foreach ($data as $key => $value) {
  317. if (strlen($value['supplier']) > 1) {
  318. foreach (explode(',', $value['supplier']) as $k => $v) {
  319. $data[$key]['supplierTitle'][] = isset($allSupplierData[$v]['title']) ? $allSupplierData[$v]['title'] : 0;
  320. }
  321. } else {
  322. $data[$key]['supplierTitle'][] = isset($allSupplierData[$value['id']]['title']) ? $allSupplierData[$value['id']]['title'] : 0;
  323. }
  324. }
  325. return $data;
  326. }
  327. public function getBrandNameByIds($ids): ResultWrapper
  328. {
  329. if (empty($ids)){
  330. return ResultWrapper::success([]);
  331. }
  332. $lists = $this->objDGoodsBrand->select(['id' => array_values($ids)],'id,title');
  333. if ($lists === false){
  334. return ResultWrapper::fail($this->objDGoodsBrand->error,ErrorCode::$dberror);
  335. }
  336. $map = [];
  337. foreach ($lists as $list){
  338. $map[$list['id']] = $list['title'];
  339. }
  340. return ResultWrapper::success($map);
  341. }
  342. }