GoodsCategoryService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\GoodsCategoryModel;
  13. use App\Models\GoodsModel;
  14. use App\Services\BaseService;
  15. /**
  16. * 商品分类管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class GoodsCategoryService
  20. * @package App\Services\Api
  21. */
  22. class GoodsCategoryService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * GoodsCategoryService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new GoodsCategoryModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 列表数据
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15, $field = '')
  54. {
  55. $where = ['a.mark' => 1];
  56. $field = $field? $field : 'lev_a.id,lev_a.merch_id,lev_a.pid,lev_a.name,lev_a.icon,lev_a.status';
  57. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  58. $order = 'id desc';
  59. if($sortType == 1){
  60. $order = 'lev_a.sort desc, lev_a.id desc';
  61. }
  62. $list = $this->model->from('goods_category as a')
  63. ->where($where)
  64. ->where(function ($query) use ($params) {
  65. $status = isset($params['status']) ? $params['status'] : 0;
  66. $status = $status > 0 ? $status : 1;
  67. if ($status > 0) {
  68. $query->where('a.status', $status);
  69. }
  70. // 商户
  71. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  72. if ($merchId > 0) {
  73. $query->where('a.merch_id', $merchId);
  74. }
  75. })
  76. ->where(function ($query) use ($params) {
  77. $keyword = isset($params['kw']) ? $params['kw'] : '';
  78. if ($keyword) {
  79. $query->where('a.name', 'like', "%{$keyword}%");
  80. }
  81. })
  82. ->selectRaw($field)
  83. ->orderByRaw($order)
  84. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  85. $list = $list ? $list->toArray() : [];
  86. if ($list) {
  87. foreach ($list['data'] as &$item) {
  88. $item['icon'] = isset($item['icon']) && $item['icon'] ? get_image_url($item['icon']) : '';
  89. }
  90. }
  91. return [
  92. 'pageSize' => $pageSize,
  93. 'total' => isset($list['total']) ? $list['total'] : 0,
  94. 'list' => isset($list['data']) ? $list['data'] : []
  95. ];
  96. }
  97. /**
  98. * 添加编辑
  99. * @return array
  100. * @since 2020/11/11
  101. * @author laravel开发员
  102. */
  103. public function edit()
  104. {
  105. // 请求参数
  106. $data = request()->all();
  107. // 图标
  108. $icon = isset($data['icon']) ? trim($data['icon']) : '';
  109. if ($icon && strpos($icon, "temp")) {
  110. $data['icon'] = save_image($icon, 'images');
  111. } else if ($icon) {
  112. $data['icon'] = str_replace(IMG_URL, "", $data['icon']);
  113. }
  114. return parent::edit($data); // TODO: Change the autogenerated stub
  115. }
  116. /**
  117. * 封存/解封
  118. * @param $goodsId
  119. */
  120. public function lock($goodsId)
  121. {
  122. $params = request()->all();
  123. $status = isset($params['status']) ? $params['status'] : 2;
  124. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  125. if (empty($goods)) {
  126. $this->error = 2061;
  127. return false;
  128. }
  129. if ($this->model->where(['id' => $goodsId])->update(['status' => $status, 'update_time' => time(), 'remark' => '店长封存'])) {
  130. $this->error = 1002;
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * 修改信息
  137. * @param $goodsId
  138. * @return bool
  139. */
  140. public function modify($id)
  141. {
  142. $params = request()->all();
  143. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  144. if (empty($info)) {
  145. $this->error = 2061;
  146. return false;
  147. }
  148. $data = [
  149. 'name' => isset($params['name']) ? $params['name'] : '',
  150. 'type' => isset($params['type']) ? $params['type'] : 0,
  151. 'status' => isset($params['status']) ? $params['status'] : 0,
  152. 'update_time' => time(),
  153. ];
  154. $icon = isset($data['icon']) ? trim($data['icon']) : '';
  155. if ($icon && strpos($icon, "temp")) {
  156. $data['icon'] = save_image($icon, 'images');
  157. } else if ($icon) {
  158. $data['icon'] = str_replace(IMG_URL, "", $data['icon']);
  159. }
  160. if ($this->model->where(['id' => $id])->update($data)) {
  161. $this->error = 1008;
  162. return true;
  163. }
  164. $this->error = 1009;
  165. return false;
  166. }
  167. /**
  168. * 删除
  169. * @return array
  170. */
  171. public function delete()
  172. {
  173. // 参数
  174. $param = request()->all();
  175. $ids = getter($param, "id");
  176. if (empty($ids)) {
  177. return message("记录ID不能为空", false);
  178. }
  179. $ids = is_array($ids) ? $ids : [$ids];
  180. if(GoodsModel::whereIn('cate_id', $ids)->count()){
  181. $this->error = 2541;
  182. return false;
  183. }
  184. $result = parent::delete(); // TODO: Change the autogenerated stub
  185. $success = isset($result['success'])? $result['success'] : false;
  186. return $success? true : false;
  187. }
  188. }