Goods.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\controller;
  13. use app\common\exception\BaseException;
  14. use app\common\service\Export as ExportService;
  15. use app\store\model\Goods as GoodsModel;
  16. use app\store\model\GoodsSku;
  17. use app\store\model\GoodsSku as GoodsSkuModel;
  18. use app\store\model\member\MemberGoods;
  19. use app\store\model\user\Withdraw as WithdrawModel;
  20. use app\common\model\ActivityDiscount as ActivityDiscount;
  21. use app\common\enum\goods\SpecType as SpecTypeEnum;
  22. use app\store\model\groupbuylb\GroupBuyLbActivity;
  23. /**
  24. * 商品管理控制器
  25. * Class Goods
  26. * @package app\store\controller
  27. */
  28. class Goods extends Controller
  29. {
  30. /**
  31. * 商品列表
  32. * @return array
  33. * @throws \think\db\exception\DbException
  34. */
  35. public function list()
  36. {
  37. // 获取列表记录
  38. $model = new GoodsModel;
  39. $params = $this->request->param();
  40. $params['sortType'] = 'create_time';
  41. $list = $model->getList($params);
  42. return $this->renderSuccess(compact('list'));
  43. }
  44. //单规格商品列表
  45. public function listspec(){
  46. //获取列表记录
  47. $model = new GoodsModel;
  48. $params = $this->request->param();
  49. $params['sortType'] = 'create_time';
  50. $params['spec_type'] = SpecTypeEnum::SINGLE;
  51. $list = $model->getList($params);
  52. return $this->renderSuccess(compact('list'));
  53. }
  54. /**
  55. * 商品列表
  56. * @return array
  57. * @throws \think\db\exception\DbException
  58. */
  59. public function listdiscount()
  60. {
  61. $discount = new ActivityDiscount;
  62. $params = $this->request->param();
  63. $activity_id = $params['activity_id']??0;
  64. $good_id_array = $discount->getActivityGoodIdArray($activity_id);
  65. // 获取列表记录
  66. $model = new GoodsModel;
  67. $params['sortType'] = 'create_time';
  68. $params['not_goods_id_arr'] = $good_id_array;
  69. $list = $model->getList($params);
  70. return $this->renderSuccess(compact('list'));
  71. }
  72. //裂变商品搜索
  73. public function skuPickerGroupBuyList(){
  74. $model = new GoodsSkuModel;
  75. $activity = new GroupBuyLbActivity;
  76. $params = $this->request->param();
  77. $activity_id = $params['activity_id']??0;
  78. $good_id_array = $activity->getActivityGoodIdArray($activity_id);
  79. $params['not_goods_id_arr'] = $good_id_array;
  80. $list = $model->getPickerList($params);
  81. return $this->renderSuccess(compact('list'));
  82. }
  83. /**
  84. * 按商品SKU返回列表
  85. * @return array
  86. * @throws \think\db\exception\DbException
  87. */
  88. public function skuPickerList()
  89. {
  90. // 获取列表记录
  91. $model = new GoodsSkuModel;
  92. $params = $this->request->param();
  93. $list = $model->getPickerList($params);
  94. return $this->renderSuccess(compact('list'));
  95. }
  96. /**
  97. * 根据商品ID集获取列表记录
  98. * @param array $goodsIds
  99. * @return array
  100. */
  101. public function listByIds(array $goodsIds)
  102. {
  103. // 获取列表记录
  104. $model = new GoodsModel;
  105. $list = $model->getListByIds($goodsIds);
  106. return $this->renderSuccess(compact('list'));
  107. }
  108. /**
  109. * 商品详情
  110. * @param int $goodsId
  111. * @return array
  112. * @throws BaseException
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function detail(int $goodsId)
  118. {
  119. // 获取商品详情
  120. $model = new GoodsModel;
  121. $goodsInfo = $model->getDetail($goodsId);
  122. return $this->renderSuccess(compact('goodsInfo'));
  123. }
  124. /**
  125. * 添加商品
  126. * @return array
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\DbException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. */
  131. public function add()
  132. {
  133. $model = new GoodsModel;
  134. if ($model->add($this->postForm())) {
  135. return $this->renderSuccess('添加成功');
  136. }
  137. return $this->renderError($model->getError() ?: '添加失败');
  138. }
  139. /**
  140. * 编辑商品
  141. * @param int $goodsId
  142. * @return array
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. */
  147. public function edit(int $goodsId)
  148. {
  149. // 商品详情
  150. $model = GoodsModel::detail($goodsId);
  151. // 更新记录
  152. if ($model->edit($this->postForm())) {
  153. return $this->renderSuccess('更新成功');
  154. }
  155. return $this->renderError($model->getError() ?: '更新失败');
  156. }
  157. /**
  158. * 修改商品状态(上下架)
  159. * @param array $goodsIds 商品id集
  160. * @param bool $state 为true表示上架
  161. * @return array
  162. */
  163. public function state(array $goodsIds, bool $state)
  164. {
  165. $model = new GoodsModel;
  166. if (!$model->setStatus($goodsIds, $state)) {
  167. return $this->renderError($model->getError() ?: '操作失败');
  168. }
  169. return $this->renderSuccess('操作成功');
  170. }
  171. /**
  172. * 删除商品
  173. * @param array $goodsIds
  174. * @return array
  175. */
  176. public function delete(array $goodsIds)
  177. {
  178. $model = new GoodsModel;
  179. if (!$model->setDelete($goodsIds)) {
  180. return $this->renderError($model->getError() ?: '删除失败');
  181. }
  182. return $this->renderSuccess('删除成功');
  183. }
  184. /**
  185. * 根据商品id获取SKU列表
  186. * @param int $goodsId
  187. * @return array
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function skulist(int $good_id)
  193. {
  194. // 商品SKU列表
  195. $skulist = GoodsSkuModel::where("goods_id",$good_id)->select();
  196. $list = [];
  197. foreach($skulist as $row){
  198. $sku_name = $row->goods_props[0]['value']['name']??'';
  199. $goods_sku_id = $row->goods_sku_id;
  200. if(empty($sku_name)||empty($goods_sku_id)){
  201. continue;
  202. }
  203. $new['good_sku_id'] = $goods_sku_id;
  204. $new['name'] = $sku_name;
  205. $list[] = $new;
  206. }
  207. return $this->renderSuccess(compact('list'));
  208. }
  209. //成本管理
  210. public function manageCosts(){
  211. $model = new GoodsModel;
  212. $list = $model->getCostsList($this->request->param());
  213. return $this->renderSuccess(compact('list'));
  214. }
  215. public function editClearingPrices(array $skues){
  216. //dd($skues);
  217. GoodsSku::updateClearingPrice($skues);
  218. return $this->renderSuccess('成功');
  219. }
  220. public function editPlatformRate(array $rates){
  221. //dd($skues);
  222. GoodsSku::updatePlatformRate($rates);
  223. return $this->renderSuccess('成功');
  224. }
  225. /**
  226. * 导出成本管理列表功能
  227. * @return array
  228. * @author: zjwhust
  229. * @Time: 2021/10/15 13:43
  230. */
  231. public function orderExport(){
  232. $param = $this->request->param();
  233. /* if(isset($param['goods_id']) && empty($param['goods_id'])){
  234. return $this->renderError('请勾选订单商品后再导出');
  235. }*/
  236. $model = new GoodsModel();
  237. $data = $model->listExport($param);
  238. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls',true);
  239. return $this->renderSuccess($res,'导出成功');
  240. }
  241. }