Goods.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace app\data\controller\api\business;
  3. use app\data\model\ShopGoods;
  4. use app\data\model\ShopGoodsCate;
  5. use app\data\model\ShopGoodsItem;
  6. use think\admin\Controller;
  7. use hg\apidoc\annotation\Title;
  8. use hg\apidoc\annotation\Method;
  9. use hg\apidoc\annotation\Param;
  10. use hg\apidoc\annotation\Returned;
  11. use think\admin\extend\CodeExtend;
  12. use think\db\Where;
  13. use think\exception\ErrorException;
  14. use think\facade\Db;
  15. /**
  16. * 商家商品数据接口
  17. */
  18. class Goods extends Controller
  19. {
  20. public function goods_status()
  21. {
  22. $data = ShopGoods::mk()->where('status',1)->field('status,stock_total,id')->select();
  23. foreach ($data as &$v){
  24. if($v['stock_total']<=0){
  25. ShopGoods::mk()->where('id',$v['id'])->save(['status'=>2]);
  26. }
  27. }
  28. }
  29. /**
  30. * @Title ("商品分类")
  31. * @return void
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function getCate()
  37. {
  38. $data = ShopGoodsCate::treeData();
  39. foreach ($data as &$v){
  40. if(!isset($v['sub'])){
  41. $v['sub']=[];
  42. }
  43. }
  44. $this->success('获取分类成功',$data );
  45. }
  46. /**
  47. * @Title ("我的商品数据")
  48. * @Param ("name",desc="商品名称")
  49. * @Param ("status",desc="1售卖中 0已下架 2已售罄 传空全部")
  50. * @Param ("examine",desc="0,待审核 1审核成功 2审核失败,组合查询示例写法传'0,2'表示待审核和审核失败的所有商品")
  51. * @Param ("cateids",desc="分类id 1,2")
  52. * @return void
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function goods_list(){
  58. $admin_id = $this->uuid();
  59. $count = ['全部','售卖中','已下架','已售罄'];
  60. foreach ($count as &$v){
  61. switch ($v){
  62. case '全部':
  63. $where =[];
  64. break;
  65. case '售卖中':
  66. $where['status']=1;
  67. $where['examine']=1;
  68. break;
  69. case '已下架':
  70. $where['status']=0;
  71. $where['examine']=1;
  72. break;
  73. case '已售罄':
  74. $where['status']=2;
  75. $where['examine']=1;
  76. }
  77. $v=ShopGoods::mk()->where('admin_id',$admin_id->id)->where('deleted',0)->where($where)->count();
  78. }
  79. array_push($count,ShopGoods::mk()->where('admin_id',$admin_id->id)->where('deleted',0)->whereIn('examine',[0,2])->count());
  80. $cateids = input('cateids');
  81. $ids =[];
  82. if(!empty($cateids)){
  83. $ids[]= ['cateids','find in set' ,$cateids];
  84. }
  85. $query = ShopGoods::mQuery()->like('name')->equal('code,status')->in('examine');
  86. if(input('status') != ''){
  87. $result = $query->where('admin_id',$admin_id->id)->where($ids)->where('deleted',0)->order('id desc')->where('examine',1)->page(true, false, false, 10);
  88. }else{
  89. $result = $query->where('admin_id',$admin_id->id)->where($ids)->where('deleted',0)->order('id desc')->page(true, false, false, 10);
  90. }
  91. foreach ($result['list'] as &$v){
  92. if($v['stock_total'] == 0){
  93. ShopGoods::mk()->where('id',$v['id'])->save(['status'=>2]);
  94. }
  95. // $v['cover'] = explode('|',$v['cover']);
  96. if($index = strpos($v['cover'],"|")){
  97. $v['cover'] = substr($v['cover'],0,$index);
  98. }
  99. $v['cover'] = substr($v['cover'],0);
  100. }
  101. $data['count'] =$count;
  102. $data['list']=$result;
  103. $this->success('我的商品列表',$data);
  104. }
  105. /**
  106. * @Title ("添加商品")
  107. * @Param ("name",desc="商品名称")
  108. * @Param ("cateids",desc="商品分类")
  109. * @Param ("cover",desc="商品封面图")
  110. * @Param ("slider",desc="商品轮播图")
  111. * @Param ("content",desc="商品详情")
  112. * @Param ("stock_total",desc="商品库存")
  113. * @Param ("price_selling",desc="商品销售价")
  114. * @Param ("status",desc="1在售 0下架")
  115. * @Param ("spec",desc="规格 [{name:默认分组,list:[{name:默认规格,check:true,show:true,group:默认分组}]}]")
  116. * @Param ("items",desc="商品规格信息 [ goods_spec 规格名 颜色::红色;;大小::1','stock_total 库存','price_selling 售价','sku_image 图片''status 1在售 2 下架']")
  117. */
  118. public function goods_add(){
  119. $admin_id = $this->uuid();
  120. $data = $this->_vali(
  121. [
  122. 'name.require'=>'商品名称不能为空',
  123. 'cateids.require'=>'商品分类不能不选',
  124. 'cover.require'=>'商品封面不能为空',
  125. // 'slider.require'=>'商品轮播图不能为空',
  126. // 'content.require'=>'商品详情不能为空',
  127. 'stock_total.require'=>'商品库存不能为空',
  128. 'price_selling.require'=>'商品销售价格不能为空',
  129. 'spec.require'=>'商品规格参数',
  130. 'items.require'=>'商品规格不能为空',
  131. 'status.require'=>'商品状态不能为空',
  132. ]
  133. );
  134. $goods_data= [
  135. 'name'=>$data['name'],
  136. 'cateids'=>$data['cateids'],
  137. 'cover'=>$data['cover'],
  138. 'slider'=>input('slider')?input('slider'):'',
  139. 'content'=>input('content')?input('content'):'',
  140. 'stock_total'=>$data['stock_total'],
  141. 'price_selling'=>$data['price_selling'],
  142. 'price_market'=>$data['price_selling'],
  143. 'code'=>CodeExtend::uniqidNumber(20, 'G'),
  144. 'data_specs'=>$data['spec'],
  145. 'data_items'=>$data['items'],
  146. 'status'=>$data['status'],
  147. 'create_at'=>date('Y-m-d H:i:s'),
  148. 'admin_id'=>$admin_id->id,
  149. 'postagetype'=>input('postagetype',1),
  150. ];
  151. Db::startTrans();
  152. try {
  153. ShopGoods::mk()->insertGetId($goods_data);
  154. $data['items']=json_decode($data['items'],true);
  155. foreach ($data['items'] as &$v){
  156. $item_data = [
  157. 'goods_sku'=>CodeExtend::uniqidNumber(12, 'S'),
  158. 'goods_code'=>$goods_data['code'],
  159. 'goods_spec'=>$v['goods_spec'],
  160. 'stock_total'=>$v['stock_total'],
  161. 'price_selling'=>$v['price_selling'],
  162. 'price_market'=>$v['price_selling'],
  163. 'show_image'=>$v['sku_image'] ? $v['sku_image'] : $data['cover'],
  164. 'create_at'=>date('Y-m-d H:i:s'),
  165. 'status'=>$v['status'],
  166. 'admin_id'=>$admin_id->id
  167. ];
  168. ShopGoodsItem::mk()->insertGetId($item_data);
  169. }
  170. Db::commit();
  171. $this->success('商品数据入库成功!');
  172. }catch (ErrorException $e){
  173. Db::rollback();
  174. $this->error('商品数据入库失败!');
  175. }
  176. }
  177. /**
  178. * @Title ("商品修改")
  179. * @Param ("goods_id",desc="商品id")
  180. * @Param ("name",desc="商品名称")
  181. * @Param ("cateids",desc="商品分类")
  182. * @Param ("cover",desc="商品封面图")
  183. * @Param ("slider",desc="商品轮播图")
  184. * @Param ("content",desc="商品详情")
  185. * @Param ("stock_total",desc="商品库存")
  186. * @Param ("price_selling",desc="商品销售价")
  187. * @Param ("status",desc="商品状态 1上架 0下架")
  188. * @Param ("spec",desc="规格 [{name:默认分组,list:[{name:默认规格,check:true,show:true,group:默认分组}]}]")
  189. * @Param ("items",desc="商品规格信息 ['id sku的id' , goods_spec 规格名 颜色::红色;;大小::1','stock_total 库存','price_selling 售价','sku_image 图片''status 1在售 2 下架']")
  190. * @return void
  191. *
  192. */
  193. public function goods_edit(){
  194. $admin_id = $this->uuid();
  195. $goods_id = input('goods_id');
  196. $goods = ShopGoods::mk()->where('id',$goods_id)->find();
  197. // if($goods['examine'] == 0)$this->error('商品审核中暂不支持编辑');
  198. $data = $this->_vali(
  199. [
  200. 'name.require'=>'商品名称不能为空',
  201. 'cateids.require'=>'商品分类不能不选',
  202. 'cover.require'=>'商品封面不能为空',
  203. // 'slider.require'=>'商品轮播图不能为空',
  204. // 'content.require'=>'商品详情不能为空',
  205. 'stock_total.require'=>'商品库存不能为空',
  206. 'price_selling.require'=>'商品销售价格不能为空',
  207. 'items.require'=>'商品规格不能为空',
  208. 'status.require'=>'商品状态不能为空'
  209. ]
  210. );
  211. // $a = json_decode($data['items'],true);
  212. // foreach ($a as $v){
  213. // print_r($v);
  214. // }
  215. if(input('spec')){
  216. $goods_data= [
  217. 'name'=>$data['name'],
  218. 'cateids'=>$data['cateids'],
  219. 'cover'=>$data['cover'],
  220. 'slider'=>input('slider')?input('slider'):'',
  221. 'content'=>input('content')?input('content'):'',
  222. 'stock_total'=>$data['stock_total'],
  223. 'price_selling'=>$data['price_selling'],
  224. 'data_specs'=>input('spec'),
  225. 'data_items'=>$data['items'],
  226. 'status'=>$data['status'],
  227. 'create_at'=>date('Y-m-d H:i:s'),
  228. 'examine'=>0,
  229. 'postagetype'=>input('postagetype',1),
  230. ];
  231. Db::startTrans();
  232. try {
  233. ShopGoods::mk()->where('id',$goods_id)->save($goods_data);
  234. //删除就规格
  235. ShopGoodsItem::where('goods_code',$goods['code'])->delete();
  236. $data['items']=json_decode($data['items'],true);
  237. foreach ($data['items'] as &$v){
  238. $item_data = [
  239. 'goods_sku'=>CodeExtend::uniqidNumber(12, 'S'),
  240. 'goods_code'=>$goods['code'],
  241. 'goods_spec'=>$v['goods_spec'],
  242. 'stock_total'=>$v['stock_total'],
  243. 'price_selling'=>$v['price_selling'],
  244. 'price_market'=>$v['price_selling'],
  245. 'show_image'=>$v['sku_image'] ? $v['sku_image'] : $data['cover'],
  246. 'create_at'=>date('Y-m-d H:i:s'),
  247. 'status'=>$v['status'],
  248. 'admin_id'=>$admin_id->id
  249. ];
  250. ShopGoodsItem::mk()->insertGetId($item_data);
  251. }
  252. Db::commit();
  253. $this->success('商品数据入库成功!');
  254. }catch (ErrorException $e){
  255. Db::rollback();
  256. $this->error('商品数据入库失败!');
  257. }
  258. }else{
  259. $goods_data= [
  260. 'name'=>$data['name'],
  261. 'cateids'=>$data['cateids'],
  262. 'cover'=>$data['cover'],
  263. 'slider'=>input('slider')?input('slider'):'',
  264. 'content'=>input('content')?input('content'):'',
  265. 'stock_total'=>$data['stock_total'],
  266. 'price_selling'=>$data['price_selling'],
  267. 'data_items'=>$data['items'],
  268. 'status'=>$data['status'],
  269. 'create_at'=>date('Y-m-d H:i:s'),
  270. 'examine'=>0,
  271. 'postagetype'=>input('postagetype',1),
  272. ];
  273. Db::startTrans();
  274. try {
  275. ShopGoods::mk()->where('id',$goods_id)->save($goods_data);
  276. $data['items']=json_decode($data['items'],true);
  277. foreach ($data['items']as &$v){
  278. $item_data = [
  279. 'goods_spec'=>$v['goods_spec'],
  280. 'stock_total'=>$v['stock_total'],
  281. 'price_selling'=>$v['price_selling'],
  282. 'price_market'=>$v['price_selling'],
  283. 'show_image'=>$v['sku_image'],
  284. 'status'=>$v['status'],
  285. 'create_at'=>date('Y-m-d H:i:s'),
  286. ];
  287. ShopGoodsItem::mk()->where('id',$v['id'])->save($item_data);
  288. }
  289. Db::commit();
  290. $this->success('商品数据编辑成功!');
  291. }catch (ErrorException $e){
  292. Db::rollback();
  293. $this->success('商品数据编辑失败!'.$e);
  294. }
  295. }
  296. }
  297. /**
  298. * @Title ("删除商品")
  299. * @Method ("post")
  300. * @Param ("goods_id",desc="商品id")
  301. * @return void
  302. */
  303. public function del_goods(){
  304. $admin = $this->uuid();
  305. $goods_id=input('goods_id');
  306. ShopGoods::mk()->where('admin_id',$admin->id)->where('id',$goods_id)->save(['deleted'=>1]);
  307. $this->success('商品已删除');
  308. }
  309. /**
  310. * @Title ("商品详情")
  311. * @Method ("post")
  312. * @Param ("goods_id",desc="商品id")
  313. */
  314. public function goods_details(){
  315. $goods_id = input('goods_id');
  316. $details = ShopGoods::mk()->alias('goods')
  317. ->where('goods.id',$goods_id)
  318. ->with('postagetype')
  319. ->find();
  320. $details['item']=ShopGoodsItem::mk()->where('goods_code', $details['code'])->select();
  321. $details['cate_name']=ShopGoodsCate::mk()->whereIn('id',$details['cateids'])->field('name')->select();
  322. if($details['data_specs'] == '""' || $details['data_specs'] == ""){
  323. $details['data_specs'] = '[]';
  324. }
  325. $this->success('商品详情',$details);
  326. }
  327. /**
  328. * @Title ("商品下架")
  329. * @Method ("post")
  330. * @Param ("goods_id",desc="商品id")
  331. * @return void
  332. */
  333. public function lower(){
  334. $admin = $this->uuid();
  335. $data = $this->_vali(['goods_id.require'=>'请选择要处理的商品']);
  336. ShopGoods::mk()->where('admin_id',$admin->id)->whereIn('id',$data['goods_id'])->save(['status'=>0]);
  337. $this->success('商品已下架成功');
  338. }
  339. /**
  340. * @Title ("商品上架")
  341. * @Method ("post")
  342. * @Param ("goods_id",desc="商品id")
  343. * @return void
  344. */
  345. public function shelves(){
  346. $admin = $this->uuid();
  347. $data = $this->_vali(['goods_id.require'=>'请选择要处理的商品']);
  348. ShopGoods::mk()->where('admin_id',$admin->id)->whereIn('id',$data['goods_id'])->save(['status'=>1]);
  349. $this->success('商品已上架成功');
  350. }
  351. public function uuid(){
  352. $purchase_model = new Common($this->app);
  353. $uuid = $purchase_model->uuid();
  354. return $uuid;
  355. }
  356. }