Goods.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace app\admin\controller\mall;
  3. use app\admin\logic\ShopGoodsLogic;
  4. use app\admin\logic\ShopGoodsSpecLogic;
  5. use app\admin\logic\ShopGoodsSpecTypeLogic;
  6. use app\admin\logic\ShopGoodsTypeLogic;
  7. use app\admin\logic\ShopHotKeywordsLogic;
  8. use app\admin\logic\ShopSupplierLogic;
  9. use app\admin\model\dao\ShopGoodsSpecRelation;
  10. use app\admin\model\dao\ShopGoodsSpecType;
  11. use app\common\model\ShopGoods as ShopGoods;
  12. use app\common\model\ShopGoodsSpec;
  13. use app\admin\traits\Curd;
  14. use app\common\command\Tree;
  15. use app\common\controller\AdminController;
  16. use EasyAdmin\annotation\ControllerAnnotation;
  17. use EasyAdmin\annotation\NodeAnotation;
  18. use think\App;
  19. use app\common\model\ShopGoodsSpecType as Spec;
  20. use think\facade\Db;
  21. use think\Model;
  22. /**
  23. * Class Goods
  24. * @package app\admin\controller\mall
  25. * @ControllerAnnotation(title="商城商品管理")
  26. */
  27. class Goods extends AdminController
  28. {
  29. use Curd;
  30. protected $relationSearch = true;
  31. public function __construct(App $app)
  32. {
  33. parent::__construct($app);
  34. $this->model = new ShopGoods();
  35. }
  36. /**
  37. * @NodeAnotation(title="列表")
  38. */
  39. public function index()
  40. {
  41. if ($this->request->isAjax()) {
  42. if (input('selectFields')) {
  43. return $this->selectList();
  44. }
  45. list($page, $limit, $where) = $this->buildTableParames();
  46. list($count, $list) = ShopGoodsLogic::getList($page, $limit, $where);
  47. $data = [
  48. 'code' => 0,
  49. 'msg' => '',
  50. 'count' => $count,
  51. 'data' => $list,
  52. ];
  53. return json($data);
  54. }
  55. return $this->fetch();
  56. }
  57. public function goodsBase($id)
  58. {
  59. list($data, $hot_keywords, $shop_goods_type, $shop_supplier, $specAttrId) = ShopGoodsLogic::getGoodsBase($id);
  60. // var_dump($data);
  61. $this->assign('shop_goods_type', $shop_goods_type);
  62. $this->assign('box_type', system_box_typesetting());
  63. $this->assign('hot_keywords', $hot_keywords);
  64. $this->assign('shop_supplier', $shop_supplier);
  65. $this->assign('specAttrId', $specAttrId);
  66. $this->assign('data', $data);
  67. }
  68. /**
  69. * @param $goodsId
  70. * @param $attriId
  71. * @return \think\response\Json
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * 获取商品规格,商品sku
  76. */
  77. public function getGoodsSpecData($goodsId, $attriId)
  78. {
  79. if ($this->request->isAjax()) {
  80. $specRelation = ShopGoodsSpecRelation::getArrListByGoodsId($goodsId);
  81. $specAttrId = ShopGoodsSpecType::getPidById($specRelation[0]['spec_id']);
  82. $specType = ShopGoodsSpecTypeLogic::getCacheList();
  83. $specType = app()->make(Tree::class)->DeepTree($specType, 'child');
  84. $specTypeArr = [];
  85. foreach ($specType as $k => $v) {
  86. if ($v['id'] == $specAttrId) {
  87. if (isset($v['child']))
  88. $specTypeArr = $v['child'];
  89. }
  90. }
  91. $specRelationArr = array_reduce($specRelation, function ($result, $value) {
  92. return array_merge($result,
  93. array_values(json_decode($value['spec_value'], true)));
  94. }, array());
  95. $specRelationArr = array_column($specRelationArr, 'id');
  96. foreach ($specTypeArr as $k => &$v) {
  97. if (isset($v['child'])) {
  98. foreach ($v['child'] as $ck => &$cv) {
  99. if (in_array($cv['id'], $specRelationArr)) {
  100. $cv['checked'] = true;
  101. } else {
  102. $cv['checked'] = false;
  103. }
  104. }
  105. }
  106. }
  107. $goodsSkuArr = [];
  108. $goodsSku = ShopGoodsSpecLogic::getGoodsSpecByGoodId($goodsId);
  109. foreach ($goodsSku as $sk => $sv) {
  110. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][picture]'] = $sv['picture'];
  111. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][price]'] = $sv['price'];
  112. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][cost_price]'] = $sv['cost_price'];
  113. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][weight]'] = $sv['weight'];
  114. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][stock]'] = $sv['stock'];
  115. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][rebate_score]'] = $sv['rebate_score'];
  116. }
  117. $data = [
  118. 'code' => 0,
  119. 'msg' => '',
  120. 'spec' => $specTypeArr,
  121. 'sku' => $goodsSkuArr
  122. ];
  123. return json($data);
  124. }
  125. }
  126. /**
  127. * @NodeAnotation(title="商品添加")
  128. */
  129. public function add()
  130. {
  131. if ($this->request->isAjax()) {
  132. $goods = $this->request->post();
  133. Db::startTrans();
  134. try {
  135. if (!isset($goods['is_exist_many_spec'])) {
  136. ShopGoods::singleSpecGoods($goods);
  137. } else {
  138. ShopGoods::manySpecGoods($goods);
  139. }
  140. Db::commit();
  141. } catch (\Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. $this->success('成功');
  146. }
  147. $hot_keywords = ShopHotKeywordsLogic::getCacheList();
  148. $shop_supplier = ShopSupplierLogic::getCacheList();
  149. $this->assign('hot_keywords', $hot_keywords);
  150. $this->assign('shop_supplier', $shop_supplier);
  151. return $this->fetch();
  152. }
  153. /**
  154. * @NodeAnotation(title="商品编辑")
  155. */
  156. public function edit($id)
  157. {
  158. if ($this->request->isAjax()) {
  159. $goods = $this->request->post();
  160. Db::startTrans();
  161. try {
  162. if (!isset($goods['is_exist_many_spec'])) {
  163. ShopGoods::singleSpecGoods($goods);
  164. } else {
  165. ShopGoods::manySpecGoods($goods);
  166. }
  167. Db::commit();
  168. } catch (\Exception $e) {
  169. Db::rollback();
  170. $this->error($e->getMessage());
  171. }
  172. $this->success('保存成功');
  173. }
  174. $this->goodsBase($id);
  175. return $this->fetch();
  176. }
  177. public function copy($id)
  178. {
  179. if ($this->request->isAjax()) {
  180. $goods = $this->request->post();
  181. Db::startTrans();
  182. try {
  183. if (!isset($goods['is_exist_many_spec'])) {
  184. ShopGoods::singleSpecGoods($goods);
  185. } else {
  186. ShopGoods::manySpecGoods($goods);
  187. }
  188. Db::commit();
  189. } catch (\Exception $e) {
  190. Db::rollback();
  191. $this->error($e->getMessage());
  192. }
  193. $this->success('保存成功');
  194. }
  195. $this->goodsBase($id);
  196. return $this->fetch();
  197. }
  198. /**
  199. * @NodeAnotation(title="入库")
  200. */
  201. public function stock($id)
  202. {
  203. $sku = ShopGoodsSpecLogic::getGoodsSpecByGoodId($id);
  204. $this->assign('sku', $sku);
  205. if ($this->request->isAjax()) {
  206. $data = $this->request->post();
  207. if (empty($data['goods_id'])) {
  208. return json(['code' => 0, 'msg' => '参数错误']);
  209. }
  210. unset($data['goods_id']);
  211. $stock = [];
  212. foreach ($data as $k => $v) {
  213. $stock[] = [
  214. 'goods_spec_id' => explode('_', $k)[1],
  215. 'stock' => $v
  216. ];
  217. }
  218. $ShopGoodsSpec = new ShopGoodsSpec();
  219. $flag = $ShopGoodsSpec->saveAll($stock);
  220. return $flag ? json(['code' => 1, 'msg' => '保存成功']) :
  221. json(['code' => 0, 'msg' => '保存失败']);
  222. }
  223. return $this->fetch();
  224. }
  225. }