Serve.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Serve 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'])->where('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. $cate_id_arr = Db::name('store_goods_cate')->column('title','id');
  58. foreach ($data as &$vo) {
  59. list($vo['list'], $vo['cate']) = [[], []];
  60. foreach ($list as $goods) if ($goods['goods_id'] === $vo['id']) array_push($vo['list'], $goods);
  61. foreach ($this->clist as $cate) if ($cate['id'] === $vo['cate_id']) $vo['cate'] = $cate;
  62. $vo['cate_name'] = $cate_id_arr[$vo['cate_id']];
  63. }
  64. }
  65. /**
  66. * 添加服务
  67. * @auth true
  68. * @throws \think\Exception
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @throws \think\exception\DbException
  72. * @throws \think\exception\PDOException
  73. */
  74. public function add()
  75. {
  76. $this->title = '添加服务';
  77. $this->isAddMode = '1';
  78. $this->_form($this->table, 'form');
  79. }
  80. /**
  81. * 编辑服务
  82. * @auth true
  83. * @throws \think\Exception
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. * @throws \think\exception\DbException
  87. * @throws \think\exception\PDOException
  88. */
  89. public function edit()
  90. {
  91. $this->title = '编辑服务';
  92. $this->isAddMode = '0';
  93. $this->_form($this->table, 'form');
  94. }
  95. /**
  96. * 表单数据处理
  97. * @param array $data
  98. * @throws \think\Exception
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @throws \think\exception\DbException
  102. * @throws \think\exception\PDOException
  103. */
  104. protected function _form_filter(&$data)
  105. {
  106. // 生成服务ID
  107. if(empty($data['id'])){
  108. $goods_id = Db::name('store_goods')->order('id desc')->value('id');
  109. $data['id'] = $goods_id + 1;
  110. }
  111. if ($this->request->isGet()) {
  112. $fields = 'goods_spec,goods_id,status,price_market market,price_selling selling,number_virtual `virtual`,number_express express';
  113. $defaultValues = Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->column($fields);
  114. $this->defaultValues = json_encode($defaultValues, JSON_UNESCAPED_UNICODE);
  115. $this->cates = Db::name('StoreGoodsCate')->where(['is_deleted' => '0', 'status' => '1'])->order('sort desc,id desc')->select();
  116. } elseif ($this->request->isPost()) {
  117. if (empty($data['logo'])) $this->error('服务LOGO不能为空,请上传图片');
  118. Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->update(['status' => '0']);
  119. //查询是否有规格信息
  120. $goods_type = Db::name('store_goods_type')->where('goods_id',$data['id'])->find();
  121. if(empty($goods_type)){
  122. $serve_type = array();
  123. foreach (json_decode($data['lists']) as $value){
  124. foreach ($value as $k=>$v){
  125. if($k == 0){
  126. if(!in_array($v->name,$serve_type)){
  127. $serve_type[] = $v->name;
  128. }
  129. }
  130. }
  131. }
  132. foreach ($serve_type as $v){
  133. Db::name('store_goods_type')->insert(array('goods_id'=>$data['id'],'spec_name'=>$v));
  134. }
  135. }
  136. foreach (json_decode($data['lists'], true) as $vo) Data::save('StoreGoodsList', [
  137. 'goods_id' => $data['id'],
  138. 'goods_spec' => $vo[0]['key'],
  139. 'price_market' => $vo[0]['market'],
  140. 'price_selling' => $vo[0]['selling'],
  141. 'number_virtual' => $vo[0]['virtual'],
  142. 'number_express' => $vo[0]['express'],
  143. 'status' => $vo[0]['status'] ? 1 : 0,
  144. ], 'goods_spec', ['goods_id' => $data['id']]);
  145. }
  146. }
  147. /**
  148. * 表单结果处理
  149. * @param boolean $result
  150. */
  151. protected function _form_result($result)
  152. {
  153. if ($result && $this->request->isPost()) {
  154. $this->success('服务编辑成功!', 'javascript:history.back()');
  155. }
  156. }
  157. /**
  158. * 禁用服务
  159. * @auth true
  160. * @throws \think\Exception
  161. * @throws \think\exception\PDOException
  162. */
  163. public function forbid()
  164. {
  165. $this->_save($this->table, ['status' => '0']);
  166. }
  167. /**
  168. * 启用服务
  169. * @auth true
  170. * @throws \think\Exception
  171. * @throws \think\exception\PDOException
  172. */
  173. public function resume()
  174. {
  175. $this->_save($this->table, ['status' => '1']);
  176. }
  177. /**
  178. * 删除服务
  179. * @auth true
  180. * @throws \think\Exception
  181. * @throws \think\exception\PDOException
  182. */
  183. public function remove()
  184. {
  185. $this->_delete($this->table);
  186. }
  187. }