GoodsCategoryService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.name,lev_a.type,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. $type = isset($params['type']) ? $params['type'] : 0;
  66. if ($type > 0) {
  67. $query->where('a.type', $type);
  68. }
  69. $status = isset($params['status']) ? $params['status'] : 0;
  70. $status = $status > 0 ? $status : 1;
  71. if ($status > 0) {
  72. $query->where('a.status', $status);
  73. }
  74. // 商户
  75. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  76. if ($merchId > 0) {
  77. $query->where('a.merch_id', $merchId);
  78. }
  79. })
  80. ->where(function ($query) use ($params) {
  81. $keyword = isset($params['kw']) ? $params['kw'] : '';
  82. if ($keyword) {
  83. $query->where('a.name', 'like', "%{$keyword}%");
  84. }
  85. })
  86. ->selectRaw($field)
  87. ->orderByRaw($order)
  88. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  89. $list = $list ? $list->toArray() : [];
  90. if ($list) {
  91. foreach ($list['data'] as &$item) {
  92. $item['icon'] = isset($item['icon']) && $item['icon'] ? get_image_url($item['icon']) : '';
  93. }
  94. }
  95. return [
  96. 'pageSize' => $pageSize,
  97. 'total' => isset($list['total']) ? $list['total'] : 0,
  98. 'list' => isset($list['data']) ? $list['data'] : []
  99. ];
  100. }
  101. /**
  102. * 添加编辑
  103. * @return array
  104. * @since 2020/11/11
  105. * @author laravel开发员
  106. */
  107. public function edit()
  108. {
  109. // 请求参数
  110. $data = request()->all();
  111. // 图标
  112. $icon = isset($data['icon']) ? trim($data['icon']) : '';
  113. if ($icon && strpos($icon, "temp")) {
  114. $data['icon'] = save_image($icon, 'images');
  115. } else if ($icon) {
  116. $data['icon'] = str_replace(IMG_URL, "", $data['icon']);
  117. }
  118. return parent::edit($data); // TODO: Change the autogenerated stub
  119. }
  120. /**
  121. * 封存/解封
  122. * @param $goodsId
  123. */
  124. public function lock($goodsId)
  125. {
  126. $params = request()->all();
  127. $status = isset($params['status']) ? $params['status'] : 2;
  128. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  129. if (empty($goods)) {
  130. $this->error = 2061;
  131. return false;
  132. }
  133. if ($this->model->where(['id' => $goodsId])->update(['status' => $status, 'update_time' => time(), 'remark' => '店长封存'])) {
  134. $this->error = 1002;
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * 修改信息
  141. * @param $goodsId
  142. * @return bool
  143. */
  144. public function modify($id)
  145. {
  146. $params = request()->all();
  147. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  148. if (empty($info)) {
  149. $this->error = 2061;
  150. return false;
  151. }
  152. $data = [
  153. 'name' => isset($params['name']) ? $params['name'] : '',
  154. 'type' => isset($params['type']) ? $params['type'] : 0,
  155. 'status' => isset($params['status']) ? $params['status'] : 0,
  156. 'update_time' => time(),
  157. ];
  158. $icon = isset($data['icon']) ? trim($data['icon']) : '';
  159. if ($icon && strpos($icon, "temp")) {
  160. $data['icon'] = save_image($icon, 'images');
  161. } else if ($icon) {
  162. $data['icon'] = str_replace(IMG_URL, "", $data['icon']);
  163. }
  164. if ($this->model->where(['id' => $id])->update($data)) {
  165. $this->error = 1008;
  166. return true;
  167. }
  168. $this->error = 1009;
  169. return false;
  170. }
  171. /**
  172. * 删除
  173. * @return array
  174. */
  175. public function delete()
  176. {
  177. // 参数
  178. $param = request()->all();
  179. $ids = getter($param, "id");
  180. if (empty($ids)) {
  181. return message("记录ID不能为空", false);
  182. }
  183. $ids = is_array($ids) ? $ids : [$ids];
  184. if(GoodsModel::whereIn('cate_id', $ids)->count()){
  185. $this->error = 2541;
  186. return false;
  187. }
  188. $result = parent::delete(); // TODO: Change the autogenerated stub
  189. $success = isset($result['success'])? $result['success'] : false;
  190. return $success? true : false;
  191. }
  192. }