Goods.php 9.7 KB

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