MGoodsCategory.Class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * 商品基础数据分类模型
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/10/30
  7. * Time: 17:15
  8. */
  9. namespace JinDouYun\Model\GoodsCategory;
  10. use JinDouYun\Controller\Common\Logger;
  11. use JinDouYun\Model\Customer\MCustomer;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use Mall\Framework\Core\StatusCode;
  15. use JinDouYun\Cache\GoodsBasicRelevant;
  16. use JinDouYun\Cache\OverviewCache;
  17. use JinDouYun\Dao\GoodsCategory\DGoodsCategory;
  18. use JinDouYun\Model\GoodsManage\MGoodsBasic;
  19. class MGoodsCategory
  20. {
  21. private $objDGoodsCategory;
  22. private $onlineUserId;
  23. private $onlineEnterpriseId;
  24. private $objGoodsBasicRelevantCache;
  25. private $objOverviewCache;
  26. /**
  27. * MGoodsCategory constructor.
  28. * @param $onlineUserId
  29. * @param $onlineEnterpriseId
  30. * @throws \Exception
  31. */
  32. public function __construct($onlineUserId, $onlineEnterpriseId)
  33. {
  34. $this->onlineUserId = $onlineUserId;
  35. $this->onlineEnterpriseId = $onlineEnterpriseId;
  36. $this->objDGoodsCategory = new DGoodsCategory('default');
  37. $this->objOverviewCache = new OverviewCache();
  38. $this->objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->onlineEnterpriseId);
  39. $this->objDGoodsCategory->setTable($this->objDGoodsCategory->get_Table() . '_' . $onlineEnterpriseId);
  40. }
  41. /**
  42. * 添加分类
  43. * @param $params
  44. * @return ResultWrapper
  45. * @throws \Exception
  46. */
  47. public function addCategory($params)
  48. {
  49. //验证分类名称唯一
  50. /*$cacheResult = $this->objGoodsBasicRelevantCache->getCategoryIdByName($params['title']);
  51. if ($cacheResult) {
  52. return ResultWrapper::fail('分类名称已存在', ErrorCode::$paramError);
  53. }*/
  54. $dbResult = $this->objDGoodsCategory->insert($params);
  55. if ($dbResult === false) {
  56. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  57. }
  58. $this->objGoodsBasicRelevantCache->cacheCategoryIdRelationName($params['title'], $dbResult);
  59. return ResultWrapper::success($dbResult);
  60. }
  61. /**
  62. * 获取指定id分类的详细信息
  63. * @param $id
  64. * @return ResultWrapper
  65. * @throws \Exception
  66. */
  67. public function getCategoryInfoById($id)
  68. {
  69. $dbResult = $this->objDGoodsCategory->get_by('id', $id);
  70. if ($dbResult === false) {
  71. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  72. }
  73. $pid = $dbResult['pid'];
  74. $sql = 'select title as ptitle from qianniao_goods_category_'.$this->onlineEnterpriseId.' WHERE id = '.$pid;
  75. $ptitle = $this->objDGoodsCategory->query($sql);
  76. if ($ptitle === false) {
  77. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  78. }
  79. foreach ($ptitle as $key => $vlue){
  80. $dbResult['ptitle'] = array_shift($vlue);
  81. }
  82. return ResultWrapper::success($dbResult);
  83. }
  84. /**
  85. * 编辑分类信息
  86. * @param $params
  87. * @return ResultWrapper
  88. * @throws \Exception
  89. */
  90. public function editCategory($params)
  91. {
  92. if (empty($params['id'])) {
  93. return ResultWrapper::fail('没有指定要修改分类id', ErrorCode::$paramError);
  94. }
  95. $updateCategoryId = $params['id'];
  96. unset($params['id']);
  97. if (in_array($updateCategoryId, explode(',', $params['link']))) {
  98. return ResultWrapper::fail('请选择其他分类', ErrorCode::$paramError);
  99. }
  100. $dbResult = $this->objDGoodsCategory->update($params, $updateCategoryId);
  101. if ($dbResult === false) {
  102. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  103. }
  104. $this->objGoodsBasicRelevantCache->deleteCategoryKeyById($updateCategoryId);
  105. $this->objGoodsBasicRelevantCache->cacheCategoryIdRelationName($params['title'], $updateCategoryId);
  106. return ResultWrapper::success($dbResult);
  107. }
  108. /**
  109. * 分类的显示和隐藏
  110. * @param $params
  111. * @return ResultWrapper
  112. * @throws \Exception
  113. */
  114. public function updateCategoryStatus($params)
  115. {
  116. $dbResult = $this->objDGoodsCategory->update(['enableStatus' => $params['enableStatus']], $params['id']);
  117. if ($dbResult === false) {
  118. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  119. } else {
  120. return ResultWrapper::success($dbResult);
  121. }
  122. }
  123. /**
  124. * 获取所有基础商品分类的列表
  125. * @param $selectParams
  126. * @param $fields
  127. * @return ResultWrapper
  128. * @throws \Exception
  129. */
  130. public function getAllCategory($selectParams = [], $fields = '*')
  131. {
  132. $selectParams['deleteStatus'] = StatusCode::$standard;
  133. //分配屏蔽客户类型
  134. if(isset($selectParams['userCenterId']) && !empty($selectParams['userCenterId'])){
  135. //查询当前登录客户类型
  136. $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  137. $modelResult = $objMCustomer->getCustomerData(['userCenterId' => $selectParams['userCenterId']],'type',true);
  138. if(!$modelResult->isSuccess()){
  139. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  140. }
  141. $customer = $modelResult->getData();
  142. unset($modelResult);
  143. if(!empty($customer) && !empty($customer['type'])){
  144. $selectParams['notCustomerType'] = $customer['type'];
  145. }
  146. }
  147. unset($selectParams['userCenterId']);
  148. $whereSql = '';
  149. foreach($selectParams as $key => $value){
  150. if($key == 'notCustomerType'){
  151. $whereSql .= (empty($whereSql) ? '' : ' and ').'( not FIND_IN_SET('.$value.','.$key.') or '.$key.' is null)';
  152. }else{
  153. $whereSql .= (empty($whereSql) ? '' : ' and ').$key.' = '.$value;
  154. }
  155. }
  156. //添加select条件
  157. $dbResult = $this->objDGoodsCategory->select($whereSql, $fields, 'sort DESC,createTime desc');
  158. if ($dbResult === false) {
  159. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  160. }
  161. $return = [
  162. 'data' => arr2tree(self::formatBasicNum($dbResult)),
  163. ];
  164. return ResultWrapper::success($return);
  165. }
  166. /**
  167. * 获取所有基础商品分类的列表(小程序)
  168. * @param $selectParams
  169. * @param $fields
  170. * @return ResultWrapper
  171. * @throws \Exception
  172. * sql select * FROM qianniao_goods_category_64 where id in (SELECT DISTINCT b.categoryPath as goodsIds from qianniao_goods_64 as g LEFT JOIN qianniao_goods_basic_64 as b on g.basicGoodsId=b.id where b.deleteStatus = 5 and b.enableStatus = 5 and g.enableStatus = 5 and g.deleteStatus = 5) and enableStatus = 5 and deleteStatus = 5 and ( not FIND_IN_SET(1,notCustomerType) or notCustomerType is null) ORDER BY sort DESC,createTime desc
  173. */
  174. public function apiGetAllCategory($selectParams = [], $fields = '*')
  175. {
  176. $selectParams['deleteStatus'] = StatusCode::$standard;
  177. //分配屏蔽客户类型
  178. if(isset($selectParams['userCenterId']) && !empty($selectParams['userCenterId'])){
  179. //查询当前登录客户类型
  180. $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  181. $modelResult = $objMCustomer->getCustomerData(['userCenterId' => $selectParams['userCenterId']],'type',true);
  182. if(!$modelResult->isSuccess()){
  183. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  184. }
  185. $customer = $modelResult->getData();
  186. unset($modelResult);
  187. if(!empty($customer) && !empty($customer['type'])){
  188. $selectParams['notCustomerType'] = $customer['type'];
  189. }
  190. }
  191. unset($selectParams['userCenterId']);
  192. $whereSql = '';
  193. foreach($selectParams as $key => $value){
  194. if($key == 'notCustomerType'){
  195. $whereSql .= (empty($whereSql) ? '' : ' and ').'( not FIND_IN_SET('.$value.','.$key.') or '.$key.' is null)';
  196. }else{
  197. $whereSql .= (empty($whereSql) ? '' : ' and ').$key.' = '.$value;
  198. }
  199. }
  200. $sql = 'select id,pid,sort,createTime FROM '.$this->objDGoodsCategory->get_Table().' where id in (SELECT DISTINCT b.categoryId as goodsIds from qianniao_goods_'.$this->onlineEnterpriseId.' as g LEFT JOIN qianniao_goods_basic_'.$this->onlineEnterpriseId.' as b on g.basicGoodsId=b.id where b.deleteStatus = '.StatusCode::$standard.' and b.enableStatus = '.StatusCode::$standard.' and g.enableStatus = '.StatusCode::$standard.' and g.deleteStatus = '.StatusCode::$standard.') and '.$whereSql;
  201. //添加select条件
  202. $dbResult = $this->objDGoodsCategory->query($sql);
  203. if ($dbResult === false) {
  204. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  205. }
  206. if( empty($dbResult) ){
  207. return ResultWrapper::success(['data'=>[]]);
  208. }
  209. $categoryIds = [];
  210. foreach ($dbResult as $categoryIdsKey => $categoryIdsValue){
  211. array_push($categoryIds,$dbResult[$categoryIdsKey]['id']);
  212. array_push($categoryIds,$dbResult[$categoryIdsKey]['pid']);
  213. }
  214. unset($dbResult);
  215. $categoryIds = implode(',',$categoryIds);
  216. $categoryIdsSql = 'select * from '.$this->objDGoodsCategory->get_Table().' where id in ('.$categoryIds.')'.' ORDER BY sort DESC,createTime desc';
  217. $dbResult = $this->objDGoodsCategory->query($categoryIdsSql);
  218. if ($dbResult === false) {
  219. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  220. }
  221. $return = [
  222. 'data' => arr2tree(self::formatBasicNum($dbResult)),
  223. ];
  224. return ResultWrapper::success($return);
  225. }
  226. /**
  227. * 删除分类
  228. * @param $id
  229. * @return ResultWrapper
  230. * @throws \Exception
  231. */
  232. public function delCategory($id)
  233. {
  234. if (empty($id)) {
  235. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  236. }
  237. $dbResult = $this->objDGoodsCategory->count(['pid' => $id, 'deleteStatus' => StatusCode::$standard]);
  238. if ($dbResult === false) {
  239. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  240. }
  241. if ($dbResult > 0) {
  242. return ResultWrapper::fail("分类下有子分类", ErrorCode::$dberror);
  243. }
  244. $dbResult = $this->objDGoodsCategory->update(['deleteStatus' => StatusCode::$delete], $id);
  245. if ($dbResult === false) {
  246. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  247. }
  248. foreach ($id as $categoryId) {
  249. $this->objGoodsBasicRelevantCache->deleteCategoryKeyById($categoryId);
  250. }
  251. return ResultWrapper::success($dbResult);
  252. }
  253. /**
  254. * 根据分类id获取分类名称
  255. * @param $selectParams
  256. * @return ResultWrapper
  257. * @throws \Exception
  258. */
  259. public function getCategoryTitleByIds($selectParams)
  260. {
  261. $dbResult = $this->objDGoodsCategory->select($selectParams, 'id,title','sort desc,createTime desc');
  262. if ($dbResult === false) {
  263. Logger::logs(E_USER_ERROR,'sql error',__CLASS__,__LINE__,$this->objDGoodsCategory->error());
  264. return ResultWrapper::fail($this->objDGoodsCategory->error(), ErrorCode::$dberror);
  265. }
  266. return ResultWrapper::success($dbResult);
  267. }
  268. /**
  269. * 统计分类下的商品数量
  270. * @param $data
  271. * @return mixed
  272. * @throws \Exception
  273. */
  274. private function formatBasicNum($data)
  275. {
  276. $objMGoodsBasic = new MGoodsBasic($this->onlineUserId, $this->onlineEnterpriseId);
  277. foreach ($data as $key => &$val) {
  278. $val['code'] = createCode(StatusCode::$code['category']['prefix'], $val['id'], StatusCode::$code['brand']['length']);
  279. $dbResult = $objMGoodsBasic->getBasicTotalByIds($val['id']);
  280. if (!$dbResult->isSuccess()) {
  281. return $data;
  282. }
  283. $goodsBasicData = $dbResult->getData();
  284. if (isset($goodsBasicData['num'])) {
  285. $val['goodsBasicTotal'] = $goodsBasicData['num'];
  286. } else {
  287. $val['goodsBasicTotal'] = 0;
  288. }
  289. }
  290. return $data;
  291. }
  292. public function todayCategoryRanking($ranking = 1, $shopId = null)
  293. {
  294. if ($ranking == 1) {
  295. $result = $this->objOverviewCache->getRanking($this->onlineEnterpriseId, 'categoryRanking', $shopId);
  296. } else {
  297. $result = $this->objOverviewCache->getSalesMoneyRanking($this->onlineEnterpriseId, 'categoryRanking', $shopId);
  298. }
  299. if (empty($result)) {
  300. return [];
  301. }
  302. $total = array_sum($result);
  303. //拼接categoryId
  304. $categoryIds = [];
  305. $i = 1;
  306. foreach ($result as $categoryId => $v) {
  307. $categoryIds[] = $categoryId;
  308. $i++;
  309. if ($i >= 8) {
  310. break;
  311. }
  312. }
  313. $categoryResult = self::getCategoryTitleByIds($categoryIds);
  314. $categoryNameData = [];
  315. if ($categoryResult->isSuccess()) {
  316. $categoryResult = $categoryResult->getData();
  317. foreach ($categoryResult as $categoryData) {
  318. $categoryNameData[$categoryData['id']] = $categoryData['title'];
  319. }
  320. }
  321. //拼接返回数据
  322. $return = [];
  323. $i = 1;
  324. foreach ($result as $categoryId => $v) {
  325. $rank = [];
  326. $rank['id'] = $i;
  327. $rank['categoryName'] = isset($categoryNameData[$categoryId]) ? $categoryNameData[$categoryId] : '';
  328. $rank['numberOrMoney'] = $v;
  329. $rank['percent'] = $v ? (float)bcmul(bcdiv($v, $total), 100, 2) : 0;
  330. $return[] = $rank;
  331. $i++;
  332. if ($i >= 8) {
  333. break;
  334. }
  335. }
  336. return $return;
  337. }
  338. }