Goods.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\approve\controller;
  15. use app\common\constant\CommonConstant;
  16. use app\common\model\Goods as model;
  17. use app\common\model\GoodsStock;
  18. use app\common\service\GoodsCategoryService;
  19. use library\Controller;
  20. use library\tools\Data;
  21. use think\Db;
  22. /**
  23. * 商品
  24. */
  25. class Goods extends Controller
  26. {
  27. /**
  28. * 绑定数据表
  29. * @var string
  30. */
  31. protected $table = 'Goods';
  32. /**
  33. * 控制器初始化
  34. */
  35. protected function initialize()
  36. {
  37. $this->get_status_list = CommonConstant::get_status_list();
  38. }
  39. /**
  40. * 列表
  41. * @auth true
  42. * @menu true
  43. * @throws \think\Exception
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. */
  48. public function index()
  49. {
  50. $status = input('status');
  51. $goods_name = input('goods_name');
  52. $this->title = '商品列表';
  53. $data = model::field('is_deleted', true)
  54. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  55. ->when(array_key_exists($status, $this->get_status_list), function ($query) use ($status) {
  56. $query->where('status', $status);
  57. })
  58. ->when($goods_name,function ($query) use($goods_name){
  59. $query->where('goods_name','like','%'.$goods_name.'%');
  60. })
  61. ->with([
  62. 'goodsCategoryOne'=>function($query){
  63. $query->field('id,name');
  64. },
  65. 'goodsCategory'=>function($query){
  66. $query->field('id,name');
  67. },
  68. 'goodsStock'
  69. ])
  70. ->order('sort desc,id asc');
  71. self::_init($data);
  72. }
  73. /**
  74. * 添加
  75. * @auth true
  76. * @throws \think\Exception
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. * @throws \think\exception\DbException
  80. * @throws \think\exception\PDOException
  81. */
  82. public function add()
  83. {
  84. $this->title = '添加';
  85. $this->_form($this->table, 'form');
  86. }
  87. /**
  88. * 编辑
  89. * @auth true
  90. * @throws \think\Exception
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. * @throws \think\exception\PDOException
  95. */
  96. public function edit()
  97. {
  98. $this->title = '编辑';
  99. $this->_form($this->table, 'form');
  100. }
  101. /**
  102. * 表单处理
  103. * @param array $data
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. * @throws \think\exception\DbException
  107. */
  108. protected function _form_filter(&$data)
  109. {
  110. if ($this->request->isGet()) {
  111. if($data){
  112. // 编辑获取数据时
  113. $data['first_classify'] = $data['goods_category_first'];
  114. $data['second_classify'] = $data['goods_category_id'];
  115. $data['goods_stock'] = GoodsStock::where('goods_id',$data['id'])->select();
  116. }
  117. $this->category_list = GoodsCategoryService::get_list([],1);
  118. }
  119. if ($this->request->isPost()) {
  120. if($data){
  121. // 编辑提交数据时
  122. $data['goods_category_first'] = $data['first_classify'];
  123. $data['goods_category_id'] = $data['second_classify'];
  124. }
  125. }
  126. }
  127. /**
  128. * 表单结果处理
  129. * @param boolean $result
  130. */
  131. protected function _form_result($result)
  132. {
  133. if ($result && $this->request->isPost()) {
  134. list($data) = [$this->request->post()];
  135. p($result);
  136. p($data);
  137. exit;
  138. // $data['id'] = $result;
  139. // $low_price = 0;
  140. // foreach (json_decode($data['lists'], true) as $ko=>$vo){
  141. // if($low_price == 0 || $vo[0]['sell_price'] < $low_price )$low_price = $vo[0]['sell_price'];
  142. // Data::save('StoreGoodsItem', [
  143. // 'goods_id' => $data['id'],
  144. // 'spec_cover' => $data['spec_cover'][$ko] ? $data['spec_cover'][$ko] : $vo[0]['spec_cover'],
  145. // 'goods_spec' => $vo[0]['key'],
  146. // 'goods_no' => $vo[0]['sku'],
  147. // 'original_price' => $vo[0]['original_price'],
  148. // 'sell_price' => $vo[0]['sell_price'],
  149. // 'virtual' => $vo[0]['virtual'],
  150. // 'status' => $vo[0]['status'] ? 1 : 0,
  151. // 'weight' => $vo[0]['weight'] ? $vo[0]['weight'] : 0,
  152. // ], 'goods_spec', ['goods_id' => $data['id']]);
  153. // }
  154. // Db::name('StoreGoods')->where(['id'=>$data['id']])->update(['low_price'=>$low_price]);
  155. // \app\common\model\StoreGoods::esAdd($result);
  156. // \app\common\model\TopSearch::saveData($result,'goods');
  157. $this->success('商品编辑成功!', 'javascript:history.back()');
  158. }
  159. }
  160. /**
  161. * 删除
  162. * @auth true
  163. * @throws \think\Exception
  164. * @throws \think\exception\PDOException
  165. */
  166. public function remove()
  167. {
  168. $this->applyCsrfToken();
  169. $this->_delete($this->table);
  170. }
  171. /**
  172. * 启用
  173. * @auth true
  174. * @throws \think\Exception
  175. * @throws \think\exception\PDOException
  176. */
  177. public function resume()
  178. {
  179. $this->applyCsrfToken();
  180. $this->_save($this->table, ['status' => CommonConstant::STATUS_NORMAL]);
  181. }
  182. /**
  183. * 禁用
  184. * @auth true
  185. * @throws \think\Exception
  186. * @throws \think\exception\PDOException
  187. */
  188. public function forbid()
  189. {
  190. $this->applyCsrfToken();
  191. $this->_save($this->table, ['status' => CommonConstant::STATUS_FROZEN]);
  192. }
  193. }