CategoryServer.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\server\intelligence;
  3. use app\admin\model\IntelligenceCategory;
  4. use app\admin\model\IntelligenceProduct;
  5. class CategoryServer
  6. {
  7. /**
  8. * Notes:获取智能产品分类列表
  9. * @param string $keywords
  10. * @param int $page
  11. * @param int $limit
  12. * @return array
  13. * User: ZQ
  14. * Date: 2022/9/13
  15. */
  16. public static function getCategoryList(int $page, int $limit, string $keywords)
  17. {
  18. [$list, $count] = IntelligenceCategory::getCategoryList($page, $limit, $keywords, 0);
  19. if (!empty($list)){
  20. foreach ($list as $k => $v){
  21. $list[$k]['category_create_time'] = date('Y-m-d H:i:s',$v['category_create_time']);
  22. if (!empty($v['category_update_time'])){
  23. $list[$k]['category_update_time'] = date('Y-m-d H:i:s',$v['category_update_time']);
  24. }
  25. if (empty($keywords)){
  26. [$data,$counts] = IntelligenceCategory::getCategoryList($page, $limit, $keywords, $v['category_id']);
  27. if (!empty($data)){
  28. foreach ($data as $key => $val){
  29. $data[$key]['category_create_time'] = date('Y-m-d H:i:s',$val['category_create_time']);
  30. if (!empty($val['category_update_time'])){
  31. $data[$key]['category_update_time'] = date('Y-m-d H:i:s',$val['category_update_time']);
  32. }
  33. }
  34. }
  35. $list[$k]['children'] = $data;
  36. }
  37. }
  38. }
  39. return compact('list', 'page', 'limit', 'count');
  40. }
  41. /**
  42. * Notes:获取所有智能产品
  43. * @return array
  44. * User: ZQ
  45. * Date: 2022/9/19
  46. */
  47. public static function getCategoryAll($type)
  48. {
  49. $data = IntelligenceCategory::categoryAll(0);
  50. if ($type == 1){
  51. if (!empty($data)){
  52. foreach ($data as $k => $v){
  53. $data[$k]['children'] = IntelligenceCategory::categoryAll($v['value']);
  54. }
  55. }
  56. }
  57. return $data;
  58. }
  59. /**
  60. * Notes:修改产品
  61. * @param string $category_name
  62. * @param int $category_id
  63. * @param int $category_pid
  64. * @return int
  65. * User: ZQ
  66. * Date: 2022/9/3
  67. */
  68. public static function updateCategory($category_id, $category_name, $category_pid)
  69. {
  70. IntelligenceCategory::affairBegin();
  71. try {
  72. $where = [];
  73. $where['category_id'] = $category_id;
  74. $data = [];
  75. if (!empty($category_name)){
  76. $data['category_name'] = $category_name;
  77. }
  78. if ($category_pid == 0){
  79. $data['category_pid'] = 0;
  80. }else{
  81. if (!empty($category_pid)){
  82. $data['category_pid'] = end($category_pid);
  83. }
  84. }
  85. if ($data['category_pid'] != 0){
  86. $data['category_level'] = 2;
  87. }else{
  88. $data['category_level'] = 1;
  89. }
  90. $data['category_update_time'] = time();
  91. $result = IntelligenceCategory::where($where)->update($data);
  92. if ($result !== false){
  93. IntelligenceCategory::affairCommit();
  94. return true;
  95. }
  96. throw new \Exception('操作失败!');
  97. }catch (\Exception $exception){
  98. IntelligenceCategory::affairRollback();
  99. throw new \Exception($exception->getMessage(), 500);
  100. }
  101. }
  102. /**
  103. * Notes:删除产品
  104. * @param int $category_id
  105. * @return int
  106. * User: ZQ
  107. * Date: 2022/9/13
  108. */
  109. public static function delCategory($category_id)
  110. {
  111. IntelligenceCategory::affairBegin();
  112. try {
  113. $where = [];
  114. $where['category_id'] = $category_id;
  115. $data['category_del'] = 1;
  116. $result = IntelligenceCategory::where($where)->update($data);
  117. if (!empty($result)){
  118. IntelligenceCategory::affairCommit();
  119. return true;
  120. }else{
  121. return false;
  122. }
  123. }catch (\Exception $exception){
  124. IntelligenceCategory::affairRollback();
  125. throw new \Exception($exception->getMessage(), 500);
  126. }
  127. }
  128. /**
  129. * Notes: 添加产品
  130. * @param string $category_name
  131. * @param int $category_pid
  132. * @return int
  133. * User: ZQ
  134. * Date: 2022/9/13
  135. */
  136. public static function insertCategory(string $category_name, $category_pid)
  137. {
  138. IntelligenceCategory::affairBegin();
  139. try {
  140. $data = [];
  141. $data['category_name'] = $category_name;
  142. $data['category_pid'] = end($category_pid);
  143. if ($data['category_pid'] == 0){
  144. $data['category_level'] = 1;
  145. }else{
  146. $data['category_level'] = 2;
  147. }
  148. $data['category_create_time'] = time();
  149. $result = IntelligenceCategory::insertGetId($data);
  150. if (!empty($result)){
  151. IntelligenceCategory::affairCommit();
  152. return $result;
  153. }
  154. throw new \Exception('操作失败!');
  155. }catch (\Exception $exception){
  156. IntelligenceCategory::affairRollback();
  157. throw new \Exception($exception->getMessage(), 500);
  158. }
  159. }
  160. /**
  161. * Notes:查询产品信息
  162. * @param int $category_id
  163. * @return array
  164. * User: ZQ
  165. * Date: 2022/9/13
  166. */
  167. public static function categoryInfo($category_id)
  168. {
  169. $where = [];
  170. $where['category_id'] = $category_id;
  171. $result = IntelligenceCategory::where($where)->first();
  172. if (!empty($result)){
  173. $result['category_create_time'] = date('Y-m-d H:i:s',$result['category_create_time']);
  174. if (!empty($result['product_update_time'])){
  175. $result['category_update_time'] = date('Y-m-d H:i:s',$result['category_update_time']);
  176. }
  177. }
  178. return $result;
  179. }
  180. }