Goods.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $this->assign('shop_goods_type', $shop_goods_type);
  61. $this->assign('box_type', system_box_typesetting());
  62. $this->assign('hot_keywords', $hot_keywords);
  63. $this->assign('shop_supplier', $shop_supplier);
  64. $this->assign('specAttrId', $specAttrId);
  65. $this->assign('data', $data);
  66. }
  67. /**
  68. * @param $goodsId
  69. * @param $attriId
  70. * @return \think\response\Json
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * 获取商品规格,商品sku
  75. */
  76. public function getGoodsSpecData($goodsId, $attriId)
  77. {
  78. if ($this->request->isAjax()) {
  79. $specRelation = ShopGoodsSpecRelation::getArrListByGoodsId($goodsId);
  80. $specAttrId = ShopGoodsSpecType::getPidById($specRelation[0]['spec_id']);
  81. $specType = ShopGoodsSpecTypeLogic::getCacheList();
  82. $specType = app()->make(Tree::class)->DeepTree($specType, 'child');
  83. $specTypeArr = [];
  84. foreach ($specType as $k => $v) {
  85. if ($v['id'] == $specAttrId) {
  86. if (isset($v['child']))
  87. $specTypeArr = $v['child'];
  88. }
  89. }
  90. $specRelationArr = array_reduce($specRelation, function ($result, $value) {
  91. return array_merge($result,
  92. array_values(json_decode($value['spec_value'], true)));
  93. }, array());
  94. $specRelationArr = array_column($specRelationArr, 'id');
  95. foreach ($specTypeArr as $k => &$v) {
  96. if (isset($v['child'])) {
  97. foreach ($v['child'] as $ck => &$cv) {
  98. if (in_array($cv['id'], $specRelationArr)) {
  99. $cv['checked'] = true;
  100. } else {
  101. $cv['checked'] = false;
  102. }
  103. }
  104. }
  105. }
  106. $goodsSkuArr = [];
  107. $goodsSku = ShopGoodsSpecLogic::getGoodsSpecByGoodId($goodsId);
  108. foreach ($goodsSku as $sk => $sv) {
  109. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][picture]'] = $sv['picture'];
  110. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][price]'] = $sv['price'];
  111. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][cost_price]'] = $sv['cost_price'];
  112. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][weight]'] = $sv['weight'];
  113. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][stock]'] = $sv['stock'];
  114. $goodsSkuArr['skus[' . $sv['spec_ids'] . '][rebate_score]'] = $sv['rebate_score'];
  115. }
  116. $data = [
  117. 'code' => 0,
  118. 'msg' => '',
  119. 'spec' => $specTypeArr,
  120. 'sku' => $goodsSkuArr
  121. ];
  122. return json($data);
  123. }
  124. }
  125. /**
  126. * @NodeAnotation(title="商品添加")
  127. */
  128. public function add()
  129. {
  130. if ($this->request->isAjax()) {
  131. $goods = $this->request->post();
  132. Db::startTrans();
  133. try {
  134. if (!isset($goods['is_exist_many_spec'])) {
  135. ShopGoods::singleSpecGoods($goods);
  136. } else {
  137. ShopGoods::manySpecGoods($goods);
  138. }
  139. Db::commit();
  140. } catch (\Exception $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. }
  144. $this->success('成功');
  145. }
  146. $hot_keywords = ShopHotKeywordsLogic::getCacheList();
  147. $shop_supplier = ShopSupplierLogic::getCacheList();
  148. $this->assign('hot_keywords', $hot_keywords);
  149. $this->assign('shop_supplier', $shop_supplier);
  150. return $this->fetch();
  151. }
  152. /**
  153. * @NodeAnotation(title="商品编辑")
  154. */
  155. public function edit($id)
  156. {
  157. if ($this->request->isAjax()) {
  158. $goods = $this->request->post();
  159. Db::startTrans();
  160. try {
  161. if (!isset($goods['is_exist_many_spec'])) {
  162. ShopGoods::singleSpecGoods($goods);
  163. } else {
  164. ShopGoods::manySpecGoods($goods);
  165. }
  166. Db::commit();
  167. } catch (\Exception $e) {
  168. Db::rollback();
  169. $this->error($e->getMessage());
  170. }
  171. $this->success('保存成功');
  172. }
  173. $this->goodsBase($id);
  174. return $this->fetch();
  175. }
  176. public function copy($id)
  177. {
  178. if ($this->request->isAjax()) {
  179. $goods = $this->request->post();
  180. Db::startTrans();
  181. try {
  182. if (!isset($goods['is_exist_many_spec'])) {
  183. ShopGoods::singleSpecGoods($goods);
  184. } else {
  185. ShopGoods::manySpecGoods($goods);
  186. }
  187. Db::commit();
  188. } catch (\Exception $e) {
  189. Db::rollback();
  190. $this->error($e->getMessage());
  191. }
  192. $this->success('保存成功');
  193. }
  194. $this->goodsBase($id);
  195. return $this->fetch();
  196. }
  197. /**
  198. * @NodeAnotation(title="入库")
  199. */
  200. public function stock($id)
  201. {
  202. $sku = ShopGoodsSpecLogic::getGoodsSpecByGoodId($id);
  203. $this->assign('sku', $sku);
  204. if ($this->request->isAjax()) {
  205. $data = $this->request->post();
  206. if (empty($data['goods_id'])) {
  207. return json(['code' => 0, 'msg' => '参数错误']);
  208. }
  209. unset($data['goods_id']);
  210. $stock = [];
  211. foreach ($data as $k => $v) {
  212. $stock[] = [
  213. 'goods_spec_id' => explode('_', $k)[1],
  214. 'stock' => $v
  215. ];
  216. }
  217. $ShopGoodsSpec = new ShopGoodsSpec();
  218. $flag = $ShopGoodsSpec->saveAll($stock);
  219. return $flag ? json(['code' => 1, 'msg' => '保存成功']) :
  220. json(['code' => 0, 'msg' => '保存失败']);
  221. }
  222. return $this->fetch();
  223. }
  224. }