Goods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace app\admin\controller\shopro\commission;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. /**
  9. * 分销商管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Goods extends Backend
  14. {
  15. /**
  16. * Goods模型对象
  17. * @var \app\admin\model\shopro\goods\Goods
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\shopro\goods\Goods;
  24. $this->view->assign("typeList", $this->model->getTypeList());
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = false;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. // list($where, $sort, $order, $offset, $limit) = $this->buildparams('title');
  47. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  48. $sort = $sort == 'price' ? 'convert(`price`, DECIMAL(10, 2))' : $sort;
  49. $order = $this->request->get("order", "DESC");
  50. $offset = $this->request->get("offset", 0);
  51. $limit = $this->request->get("limit", 0);
  52. $activity_type = $this->request->get("activity_type", 'all'); // 活动类型
  53. $total = $this->buildSearchOrder()->count();
  54. // 构建查询数据条件
  55. $list = $this->buildSearchOrder();
  56. $subsql = \app\admin\model\shopro\goods\SkuPrice::where('status', 'up')->field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
  57. $goodsTableName = $this->model->getQuery()->getTable();
  58. // 关联规格表,获取总库存
  59. $list = $list->join([$subsql => 'w'], $goodsTableName . '.id = w.sku_goods_id', 'left');
  60. // 关联查询当前商品的活动,一个商品可能存在多条活动记录,使用 group_concat 搜集所有活动类型,关联条件 只有 find_in_set 会存在一个商品出现多次,所以使用 group
  61. $actSubSql = \app\admin\model\shopro\activity\Activity::where('starttime', '<=', time())->where('endtime', '>=', time())->buildSql();
  62. $list = $list->join([$actSubSql => 'act'], "(find_in_set(" . $goodsTableName . ".id, act.goods_ids) or act.goods_ids = '')", 'left');
  63. // 关联查询当前商品是否设置有积分
  64. $scoreSubSql = \app\admin\model\shopro\app\ScoreSkuPrice::field("'score' as app_type, goods_id as score_goods_id")->group('score_goods_id')->buildSql();
  65. $list = $list->join([$scoreSubSql => 'score'], $goodsTableName . '.id = score.score_goods_id', 'left');
  66. // 关闭 sql mode 的 ONLY_FULL_GROUP_BY
  67. $oldModes = closeStrict(['ONLY_FULL_GROUP_BY']);
  68. $list = $list->field("$goodsTableName.*, w.*,score.*,group_concat(act.type) as activity_type, act.goods_ids")
  69. ->group('id')
  70. ->orderRaw($sort . ' ' . $order)
  71. ->limit($offset, $limit)
  72. ->select();
  73. // 恢复 sql mode
  74. recoverStrict($oldModes);
  75. foreach ($list as $row) {
  76. $row->commission = \app\admin\model\shopro\commission\Goods::get($row->id);
  77. $row->visible(['id', 'type', 'activity_id', 'activity_type', 'is_sku', 'app_type', 'title', 'status', 'weigh', 'category_ids', 'image', 'price', 'likes', 'views', 'sales', 'stock', 'show_sales', 'dispatch_type', 'updatetime', 'commission']);
  78. }
  79. $list = collection($list)->toArray();
  80. $result = array("total" => $total, "rows" => $list);
  81. if ($this->request->get("page_type") == 'select') {
  82. return json($result);
  83. }
  84. return $this->success('操作成功', null, $result);
  85. }
  86. return $this->view->fetch();
  87. }
  88. /**
  89. * 编辑
  90. */
  91. public function edit($ids = null)
  92. {
  93. if ($this->request->isPost()) {
  94. $params = $this->request->post();
  95. $goods_ids = explode(',', $params['ids']);
  96. unset($params['ids']);
  97. if ($params) {
  98. Db::startTrans();
  99. try {
  100. foreach($goods_ids as $goods_id) {
  101. $commissionGoods = \app\admin\model\shopro\commission\Goods::get($goods_id);
  102. if($commissionGoods) {
  103. $commissionGoods->allowField(true)->save($params);
  104. }else {
  105. $params['goods_id'] = $goods_id;
  106. \app\admin\model\shopro\commission\Goods::insert($params);
  107. }
  108. }
  109. Db::commit();
  110. } catch (ValidateException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (PDOException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. $this->success('编辑成功');
  121. }
  122. $this->error(__('Parameter %s can not be empty', ''));
  123. }else{
  124. if(!$ids) {
  125. $ids = $this->request->get('id');
  126. }
  127. $row = $this->model->where('id', 'in', $ids)->select();
  128. if (!$row) {
  129. $this->error(__('No Results were found'));
  130. }
  131. if(count($row) == 1) {
  132. $row[0]->commission = \app\admin\model\shopro\commission\Goods::get($row[0]->id);
  133. $row[0]->sku_price = \app\admin\model\shopro\goods\SkuPrice::all(['goods_id' => $row[0]->id]);
  134. }
  135. $this->assignconfig('row', $row);
  136. $this->assignconfig('defaultCommissionConfig', \app\admin\model\shopro\commission\Config::where([
  137. 'name' => ['in', ['commission_level', 'self_buy', 'commission_price_type', 'commission_event']]
  138. ])->column('value', 'name'));
  139. return $this->view->fetch();
  140. }
  141. }
  142. //参与分销
  143. public function commission_status()
  144. {
  145. $params = $this->request->post();
  146. \app\admin\model\shopro\commission\Goods::where('goods_id', 'in', $params['ids'])->update([
  147. 'status' => $params['status']
  148. ]);
  149. $this->success('更新成功');
  150. }
  151. // 构建查询条件
  152. private function buildSearchOrder()
  153. {
  154. $goods_title = $this->request->get("goods_title", ''); // 关键字
  155. $status = $this->request->get("status", 'all');
  156. $goods_type = $this->request->get("goods_type", 'all');
  157. $activity_type = $this->request->get("activity_type", 'all');
  158. $app_type = $this->request->get("app_type", 'all');
  159. $min_price = $this->request->get("min_price", "");
  160. $max_price = $this->request->get("max_price", "");
  161. $category_id = $this->request->get('category_id', 0);
  162. $self_rules = $this->request->get('self_rules', 'all');
  163. $commission_goods_status = $this->request->get('commission_goods_status', 'all');
  164. $tableName = $this->model->getQuery()->getTable();
  165. $tableName = $tableName . '.';
  166. $goods = $this->model;
  167. if ($goods_title) {
  168. // 模糊搜索字段
  169. $searcharr = ['title'];
  170. foreach ($searcharr as $k => &$v) {
  171. $v = stripos($v, ".") === false ? $tableName . $v : $v;
  172. }
  173. unset($v);
  174. $goods = $goods->where(function ($query) use ($searcharr, $goods_title, $tableName) {
  175. $query->where(implode("|", $searcharr), "LIKE", "%{$goods_title}%");
  176. });
  177. }
  178. if ($goods_type != 'all') {
  179. $goods = $goods->where($tableName . 'type', $goods_type);
  180. }
  181. $goods_ids = [];
  182. // 活动
  183. if ($activity_type != 'all') {
  184. // 同一请求,会组装两次请求条件,缓存 10 秒
  185. $activities = \app\admin\model\shopro\activity\Activity::cache(10)->where('type', $activity_type)->column('goods_ids');
  186. foreach ($activities as $key => $goods_id) {
  187. $ids = explode(',', $goods_id);
  188. $goods_ids = array_merge($goods_ids, $ids);
  189. }
  190. }
  191. // 积分
  192. if ($app_type == 'score') {
  193. $score_goods_ids = \app\admin\model\shopro\app\ScoreSkuPrice::cache(10)->group('goods_id')->column('goods_id');
  194. $goods_ids = array_merge($goods_ids, $score_goods_ids);
  195. }
  196. $goods_ids = array_filter(array_unique($goods_ids));
  197. if ($goods_ids) {
  198. $goods = $goods->where($tableName . 'id', 'in', $goods_ids);
  199. } else {
  200. if ($activity_type != 'all' || $app_type != 'all') {
  201. // 搜了活动,但是 goods_ids 为空,这时候搜索结果应该为空
  202. $goods = $goods->where($tableName . 'id', 'in', $goods_ids);
  203. }
  204. }
  205. // 价格
  206. if ($min_price != '') {
  207. $goods = $goods->where('convert(`price`, DECIMAL(10, 2)) >= ' . round($min_price, 2));
  208. }
  209. if ($max_price != '') {
  210. $goods = $goods->where('convert(`price`, DECIMAL(10, 2)) <= ' . round($max_price, 2));
  211. }
  212. // 商品状态
  213. if ($status != 'all') {
  214. $goods = $goods->where('status', $status);
  215. }
  216. if(isset($category_id) && $category_id != 0) {
  217. $category_ids = [];
  218. // 查询分类所有子分类,包括自己
  219. $category_ids = \addons\shopro\model\Category::getCategoryIds($category_id);
  220. $goods = $goods->where(function ($query) use ($category_ids) {
  221. // 所有子分类使用 find_in_set or 匹配,亲测速度并不慢
  222. foreach($category_ids as $key => $category_id) {
  223. $query->whereOrRaw("find_in_set($category_id, category_ids)");
  224. }
  225. });
  226. }
  227. // 分销规则
  228. if ($self_rules != 'all' && ($commission_goods_status == 'all' || $commission_goods_status == 1)) {
  229. // 这肯定是参与的,或者全部
  230. $goods = $goods->whereExists(function ($query) use ($self_rules, $tableName) {
  231. $commissionGoodsName = (new \app\admin\model\shopro\commission\Goods())->getQuery()->getTable();
  232. $query = $query->table($commissionGoodsName)->where($commissionGoodsName . '.goods_id=' . $tableName . 'id');
  233. $query = $query->where('self_rules', $self_rules);
  234. return $query;
  235. });
  236. }
  237. // 查询参与分销的
  238. if ($commission_goods_status != 'all') {
  239. $goods = $goods->where(function ($query) use ($commission_goods_status, $tableName) {
  240. $query->whereExists(function ($query) use ($commission_goods_status, $tableName) {
  241. $commissionGoodsName = (new \app\admin\model\shopro\commission\Goods())->getQuery()->getTable();
  242. $query = $query->table($commissionGoodsName)->where($commissionGoodsName . '.goods_id=' . $tableName . 'id');
  243. $query = $query->where('status', $commission_goods_status);
  244. return $query;
  245. });
  246. // 查询不参与分销的(存在 commission goods 并且 status = 1 或者 不存在 commission_goods)
  247. if ($commission_goods_status == 0) {
  248. $query = $query->whereOr(function ($query) use ($commission_goods_status, $tableName) {
  249. $query = $query->whereNotExists(function ($query) use ($commission_goods_status, $tableName) {
  250. $commissionGoodsName = (new \app\admin\model\shopro\commission\Goods())->getQuery()->getTable();
  251. $query = $query->table($commissionGoodsName)->where($commissionGoodsName . '.goods_id=' . $tableName . 'id');
  252. return $query;
  253. });
  254. return $query;
  255. });
  256. }
  257. return $query;
  258. });
  259. }
  260. return $goods;
  261. }
  262. }