Goods.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\store\controller;
  15. use library\Controller;
  16. use library\tools\Data;
  17. use think\Db;
  18. /**
  19. * 金刚区服务管理
  20. * Class Goods
  21. * @package app\store\controller
  22. */
  23. class Goods extends Controller
  24. {
  25. /**
  26. * 指定数据表
  27. * @var string
  28. */
  29. protected $table = 'StoreGoods';
  30. /**
  31. * 金刚区服务管理
  32. * @auth true
  33. * @menu true
  34. * @throws \think\Exception
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. * @throws \think\exception\PDOException
  39. */
  40. public function index()
  41. {
  42. $this->title = '金刚区服务管理';
  43. $query = $this->_query($this->table)->equal('status,cate_id')->like('title');
  44. $query->where(['is_deleted' => '0','cate_id'=>0])->order('sort desc,id desc')->page();
  45. }
  46. /**
  47. * 数据列表处理
  48. * @param array $data
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. protected function _index_page_filter(&$data)
  54. {
  55. $this->clist = Db::name('StoreGoodsCate')->where(['is_deleted' => '0', 'status' => '1'])->select();
  56. $list = Db::name('StoreGoodsList')->where('status', '1')->whereIn('goods_id', array_unique(array_column($data, 'id')))->select();
  57. foreach ($data as &$vo) {
  58. list($vo['list'], $vo['cate']) = [[], []];
  59. foreach ($list as $goods) if ($goods['goods_id'] === $vo['id']) array_push($vo['list'], $goods);
  60. foreach ($this->clist as $cate) if ($cate['id'] === $vo['cate_id']) $vo['cate'] = $cate;
  61. }
  62. }
  63. /**
  64. * 服务库存入库
  65. * @auth true
  66. * @throws \think\Exception
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. * @throws \think\exception\PDOException
  71. */
  72. public function stock()
  73. {
  74. if ($this->request->isGet()) {
  75. $GoodsId = $this->request->get('id');
  76. $goods = Db::name('StoreGoods')->where(['id' => $GoodsId])->find();
  77. empty($goods) && $this->error('无效的金刚区服务,请稍候再试!');
  78. $goods['list'] = Db::name('StoreGoodsList')->where(['goods_id' => $GoodsId])->select();
  79. $this->fetch('', ['vo' => $goods]);
  80. } else {
  81. list($post, $data) = [$this->request->post(), []];
  82. if (isset($post['id']) && isset($post['goods_id']) && is_array($post['goods_id'])) {
  83. foreach (array_keys($post['goods_id']) as $key) if ($post['goods_number'][$key] > 0) array_push($data, [
  84. 'goods_id' => $post['goods_id'][$key],
  85. 'goods_spec' => $post['goods_spec'][$key],
  86. 'number_stock' => $post['goods_number'][$key],
  87. ]);
  88. if (!empty($data)) {
  89. Db::name('StoreGoodsStock')->insertAll($data);
  90. \app\store\service\GoodsService::syncStock($post['id']);
  91. $this->success('金刚区服务入库成功!');
  92. }
  93. }
  94. $this->error('没有需要服务入库的数据!');
  95. }
  96. }
  97. /**
  98. * 添加金刚区服务
  99. * @auth true
  100. * @throws \think\Exception
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @throws \think\exception\DbException
  104. * @throws \think\exception\PDOException
  105. */
  106. public function add()
  107. {
  108. $this->title = '添加金刚区服务';
  109. $this->isAddMode = '1';
  110. $this->_form($this->table, 'form');
  111. }
  112. /**
  113. * 编辑金刚区服务
  114. * @auth true
  115. * @throws \think\Exception
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @throws \think\exception\DbException
  119. * @throws \think\exception\PDOException
  120. */
  121. public function edit()
  122. {
  123. $this->title = '编辑金刚区服务';
  124. $this->isAddMode = '0';
  125. $this->_form($this->table, 'form');
  126. }
  127. /**
  128. * 表单数据处理
  129. * @param array $data
  130. * @throws \think\Exception
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. * @throws \think\exception\PDOException
  135. */
  136. protected function _form_filter(&$data)
  137. {
  138. // 生成服务ID
  139. if(empty($data['id'])){
  140. $goods_id = Db::name('store_goods')->order('id desc')->value('id');
  141. $data['id'] = $goods_id + 1;
  142. }
  143. if ($this->request->isGet()) {
  144. $fields = 'goods_spec,goods_id,status,price_market market,price_selling selling,number_virtual `virtual`,number_express express';
  145. $defaultValues = Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->column($fields);
  146. $this->defaultValues = json_encode($defaultValues, JSON_UNESCAPED_UNICODE);
  147. $this->cates = Db::name('StoreGoodsCate')->where(['is_deleted' => '0', 'status' => '1'])->order('sort desc,id desc')->select();
  148. } elseif ($this->request->isPost()) {
  149. if (empty($data['logo'])) $this->error('服务LOGO不能为空,请上传图片');
  150. if (empty($data['image'])) $this->error('服务展示图片不能为空,请上传图片');
  151. Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->update(['status' => '0']);
  152. //查询是否有规格信息
  153. $goods_type = Db::name('store_goods_type')->where('goods_id',$data['id'])->find();
  154. if(empty($goods_type)){
  155. $serve_type = array();
  156. $phone_type = array();
  157. foreach (json_decode($data['lists']) as $value){
  158. foreach ($value as $k=>$v){
  159. if($k == 0){
  160. if(!in_array($v->name,$serve_type)){
  161. $serve_type[] = $v->name;
  162. }
  163. }elseif ($k == 1){
  164. if(!in_array($v->name,$phone_type)){
  165. $phone_type[] = $v->name;
  166. }
  167. }
  168. }
  169. }
  170. foreach ($serve_type as $v){
  171. Db::name('store_goods_type')->insert(array('goods_id'=>$data['id'],'spec_name'=>$v));
  172. }
  173. foreach ($phone_type as $v){
  174. Db::name('store_goods_type')->insert(array('goods_id'=>$data['id'],'spec_name'=>$v,'type'=>2));
  175. }
  176. }
  177. foreach (json_decode($data['lists'], true) as $vo) Data::save('StoreGoodsList', [
  178. 'goods_id' => $data['id'],
  179. 'goods_spec' => $vo[0]['key'],
  180. 'price_market' => $vo[0]['market'],
  181. 'price_selling' => $vo[0]['selling'],
  182. 'number_virtual' => $vo[0]['virtual'],
  183. 'number_express' => $vo[0]['express'],
  184. 'status' => $vo[0]['status'] ? 1 : 0,
  185. ], 'goods_spec', ['goods_id' => $data['id']]);
  186. }
  187. }
  188. /**
  189. * 表单结果处理
  190. * @param boolean $result
  191. */
  192. protected function _form_result($result)
  193. {
  194. if ($result && $this->request->isPost()) {
  195. $this->success('服务编辑成功!', 'javascript:history.back()');
  196. }
  197. }
  198. /**
  199. * 禁用金刚区服务
  200. * @auth true
  201. * @throws \think\Exception
  202. * @throws \think\exception\PDOException
  203. */
  204. public function forbid()
  205. {
  206. $this->_save($this->table, ['status' => '0']);
  207. }
  208. /**
  209. * 启用金刚区服务
  210. * @auth true
  211. * @throws \think\Exception
  212. * @throws \think\exception\PDOException
  213. */
  214. public function resume()
  215. {
  216. $this->_save($this->table, ['status' => '1']);
  217. }
  218. /**
  219. * 删除金刚区服务
  220. * @auth true
  221. * @throws \think\Exception
  222. * @throws \think\exception\PDOException
  223. */
  224. public function remove()
  225. {
  226. $this->_delete($this->table);
  227. }
  228. }