Product.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/ThinkAdmin
  12. // +----------------------------------------------------------------------
  13. namespace app\goods\controller;
  14. use app\goods\service\ProductService;
  15. use controller\BasicAdmin;
  16. use service\DataService;
  17. use service\ToolsService;
  18. use think\Db;
  19. use think\exception\HttpResponseException;
  20. /**
  21. * 商店产品管理
  22. * Class Goods
  23. * @package app\store\controller
  24. * @author Anyon <zoujingli@qq.com>
  25. * @date 2017/03/27 14:43
  26. */
  27. class Product extends BasicAdmin
  28. {
  29. /**
  30. * 定义当前操作表名
  31. * @var string
  32. */
  33. public $table = 'Goods';
  34. /**
  35. * 普通产品
  36. * @return array|string
  37. * @throws \think\Exception
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. public function index()
  43. {
  44. $this->title = '产品管理';
  45. $get = $this->request->get();
  46. $db = Db::name($this->table)->where(['is_deleted' => '0']);
  47. foreach (['tags_id', 'goods_title'] as $field) {
  48. (isset($get[$field]) && $get[$field] !== '') && $db->whereLike($field, "%,{$get[$field]},%");
  49. }
  50. foreach (['cate_id', 'brand_id'] as $field) {
  51. (isset($get[$field]) && $get[$field] !== '') && $db->where($field, $get[$field]);
  52. }
  53. if (isset($get['create_at']) && $get['create_at'] !== '') {
  54. list($start, $end) = explode(' - ', $get['create_at']);
  55. $db->whereBetween('create_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
  56. }
  57. return parent::_list($db->order('status desc,sort asc,id desc'));
  58. }
  59. /**
  60. * 商城数据处理
  61. * @param array $data
  62. */
  63. protected function _data_filter(&$data)
  64. {
  65. $result = ProductService::buildGoodsList($data);
  66. $this->assign([
  67. 'brands' => $result['brand'],
  68. 'cates' => ToolsService::arr2table($result['cate']),
  69. ]);
  70. }
  71. /**
  72. * 添加产品
  73. * @return array|string
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. * @throws \think\exception\DbException
  77. * @throws \think\Exception
  78. */
  79. public function add()
  80. {
  81. if (!$this->request->isPost()) {
  82. $this->title = '添加产品';
  83. $this->_form_assign();
  84. return $this->_form($this->table, 'form');
  85. }
  86. try {
  87. $data = $this->_form_build_data();
  88. Db::transaction(function () use ($data) {
  89. $goodsID = Db::name($this->table)->insertGetId($data['main']);
  90. foreach ($data['list'] as &$vo) {
  91. $vo['goods_id'] = $goodsID;
  92. }
  93. Db::name('GoodsList')->insertAll($data['list']);
  94. });
  95. } catch (HttpResponseException $exception) {
  96. return $exception->getResponse();
  97. } catch (\Exception $e) {
  98. $this->error('产品添加失败,请稍候再试!');
  99. }
  100. list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('goods/product/index')];
  101. $this->success('添加产品成功!', "{$base}#{$url}?spm={$spm}");
  102. }
  103. /**
  104. * 编辑产品
  105. * @return array|string
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @throws \think\exception\DbException
  109. */
  110. public function edit()
  111. {
  112. if (!$this->request->isPost()) {
  113. $goods_id = $this->request->get('id');
  114. $goods = Db::name($this->table)->where(['id' => $goods_id, 'is_deleted' => '0'])->find();
  115. empty($goods) && $this->error('需要编辑的产品不存在!');
  116. $goods['list'] = Db::name('GoodsList')->where(['goods_id' => $goods_id, 'is_deleted' => '0'])->select();
  117. $this->_form_assign();
  118. return $this->fetch('form', ['vo' => $goods, 'title' => '编辑产品']);
  119. }
  120. try {
  121. $data = $this->_form_build_data();
  122. $goods_id = $this->request->post('id');
  123. $goods = Db::name($this->table)->where(['id' => $goods_id, 'is_deleted' => '0'])->find();
  124. empty($goods) && $this->error('产品编辑失败,请稍候再试!');
  125. foreach ($data['list'] as &$vo) {
  126. $vo['goods_id'] = $goods_id;
  127. }
  128. Db::transaction(function () use ($data, $goods_id, $goods) {
  129. // 更新产品主表
  130. $where = ['id' => $goods_id, 'is_deleted' => '0'];
  131. Db::name('Goods')->where($where)->update(array_merge($goods, $data['main']));
  132. // 更新产品详细
  133. Db::name('GoodsList')->where(['goods_id' => $goods_id])->delete();
  134. Db::name('GoodsList')->insertAll($data['list']);
  135. });
  136. } catch (HttpResponseException $exception) {
  137. return $exception->getResponse();
  138. } catch (\Exception $e) {
  139. $this->error('产品编辑失败,请稍候再试!' . $e->getMessage());
  140. }
  141. list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('goods/product/index')];
  142. $this->success('产品编辑成功!', "{$base}#{$url}?spm={$spm}");
  143. }
  144. /**
  145. * 表单数据处理
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @throws \think\exception\DbException
  149. */
  150. protected function _form_assign()
  151. {
  152. list($where, $order) = [['status' => '1', 'is_deleted' => '0'], 'sort asc,id desc'];
  153. $specs = (array)Db::name('GoodsSpec')->where($where)->order($order)->select();
  154. $brands = (array)Db::name('GoodsBrand')->where($where)->order($order)->select();
  155. $cates = (array)Db::name('GoodsCate')->where($where)->order($order)->select();
  156. // 所有的产品信息
  157. $where = ['is_deleted' => '0', 'status' => '1'];
  158. $goodsListField = 'goods_id,goods_spec,goods_stock,goods_sale';
  159. $goods = Db::name('Goods')->field('id,goods_title')->where($where)->select();
  160. $list = Db::name('GoodsList')->field($goodsListField)->where($where)->select();
  161. foreach ($goods as $k => $g) {
  162. $goods[$k]['list'] = [];
  163. foreach ($list as $v) {
  164. ($g['id'] === $v['goods_id']) && $goods[$k]['list'][] = $v;
  165. }
  166. }
  167. array_unshift($specs, ['spec_title' => ' - 不使用规格模板 -', 'spec_param' => '[]', 'id' => '0']);
  168. $this->assign([
  169. 'specs' => $specs,
  170. 'cates' => ToolsService::arr2table($cates),
  171. 'brands' => $brands,
  172. 'all' => $goods,
  173. ]);
  174. }
  175. /**
  176. * 读取POST表单数据
  177. * @return array
  178. */
  179. protected function _form_build_data()
  180. {
  181. list($main, $list, $post, $verify) = [[], [], $this->request->post(), false];
  182. empty($post['goods_logo']) && $this->error('产品LOGO不能为空,请上传后再提交数据!');
  183. // 产品主数据组装
  184. $main['cate_id'] = $this->request->post('cate_id', '0');
  185. $main['spec_id'] = $this->request->post('spec_id', '0');
  186. $main['brand_id'] = $this->request->post('brand_id', '0');
  187. $main['goods_logo'] = $this->request->post('goods_logo', '');
  188. $main['goods_title'] = $this->request->post('goods_title', '');
  189. $main['goods_video'] = $this->request->post('goods_video', '');
  190. $main['goods_image'] = $this->request->post('goods_image', '');
  191. $main['goods_desc'] = $this->request->post('goods_desc', '', null);
  192. $main['goods_content'] = $this->request->post('goods_content', '');
  193. $main['tags_id'] = ',' . join(',', isset($post['tags_id']) ? $post['tags_id'] : []) . ',';
  194. // 产品从数据组装
  195. if (!empty($post['goods_spec'])) {
  196. foreach ($post['goods_spec'] as $key => $value) {
  197. $goods = [];
  198. $goods['goods_spec'] = $value;
  199. $goods['market_price'] = $post['market_price'][$key];
  200. $goods['selling_price'] = $post['selling_price'][$key];
  201. $goods['status'] = intval(!empty($post['spec_status'][$key]));
  202. !empty($goods['status']) && $verify = true;
  203. $list[] = $goods;
  204. }
  205. } else {
  206. $this->error('没有产品规格或套餐信息哦!');
  207. }
  208. !$verify && $this->error('没有设置有效的产品规格!');
  209. return ['main' => $main, 'list' => $list];
  210. }
  211. /**
  212. * 删除产品
  213. * @throws \think\Exception
  214. * @throws \think\exception\PDOException
  215. */
  216. public function del()
  217. {
  218. if (DataService::update($this->table)) {
  219. $this->success("产品删除成功!", '');
  220. }
  221. $this->error("产品删除失败,请稍候再试!");
  222. }
  223. /**
  224. * 产品禁用
  225. * @throws \think\Exception
  226. * @throws \think\exception\PDOException
  227. */
  228. public function forbid()
  229. {
  230. if (DataService::update($this->table)) {
  231. $this->success("产品禁用成功!", '');
  232. }
  233. $this->error("产品禁用失败,请稍候再试!");
  234. }
  235. /**
  236. * 产品禁用
  237. * @throws \think\Exception
  238. * @throws \think\exception\PDOException
  239. */
  240. public function resume()
  241. {
  242. if (DataService::update($this->table)) {
  243. $this->success("产品启用成功!", '');
  244. }
  245. $this->error("产品启用失败,请稍候再试!");
  246. }
  247. }