Goods.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 think\Db;
  21. use think\Exception;
  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 desc');
  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->_submit('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->_submit('form');
  100. }
  101. protected function _submit($template)
  102. {
  103. $id = input('id') ?: 0;
  104. if ($this->request->isGet()) {
  105. list($data) = [[]];
  106. if ($id > 0) {
  107. $info = model::field('is_deleted', true)
  108. ->with([
  109. 'goodsStock'
  110. ])
  111. ->find($id);
  112. if (!$info){
  113. $this->error('该商品不存在或已删除');
  114. }
  115. $info['first_classify'] = $info['goods_category_first'];
  116. $info['second_classify'] = $info['goods_category_id'];
  117. $data = $info;
  118. }
  119. $this->category_list = GoodsCategoryService::get_list([], 1);
  120. return $this->fetch($template, ['vo' => $data]);
  121. }
  122. if ($this->request->isPost()) {
  123. list($data) = [$this->request->post()];
  124. if (!isset_full($data, 'goods_stock')) {
  125. $this->error('请添加商品规格');
  126. }
  127. if (!array_filter($data['goods_stock'])) {
  128. $this->error('请添加商品规格!');
  129. }
  130. $data['goods_category_first'] = $data['first_classify'];
  131. $data['goods_category_id'] = $data['second_classify'];
  132. if($id > 0){
  133. $info = model::field('is_deleted', true)
  134. ->with([
  135. 'goodsStock'
  136. ])
  137. ->find($id);
  138. if (!$info){
  139. $this->error('该商品不存在或已删除');
  140. }
  141. $goods_info = model::field('id')
  142. ->where('goods_category_first', $data['goods_category_first'])
  143. ->where('goods_category_id', $data['goods_category_id'])
  144. ->where('goods_name', $data['goods_name'])
  145. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  146. ->where('id', 'neq',$id)
  147. ->find();
  148. if ($goods_info) {
  149. $this->error('该商品已存在不能重复添加');
  150. }
  151. } else{
  152. $goods_info = model::field('id')
  153. ->where('goods_category_first', $data['goods_category_first'])
  154. ->where('goods_category_id', $data['goods_category_id'])
  155. ->where('goods_name', $data['goods_name'])
  156. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  157. ->find();
  158. if ($goods_info) {
  159. $this->error('该商品已存在不能重复添加');
  160. }
  161. }
  162. Db::startTrans();
  163. try {
  164. if ($id > 0) {
  165. self::create_stock($id, $data,$info,'update');
  166. $info->force(true)->save($data);
  167. } else {
  168. $result = model::create($data);
  169. $id = $result->id;
  170. self::create_stock($id, $data,[],'create');
  171. }
  172. Db::commit();
  173. } catch (Exception $e) {
  174. Db::rollback();
  175. $this->error($e->getMessage());
  176. }
  177. $this->success('商品编辑成功!', 'javascript:history.back()');
  178. }
  179. }
  180. protected function create_stock($id, $data,$info,$type)
  181. {
  182. $goods_stock = $data['goods_stock'];
  183. $goods_stock_data = [];
  184. $add_data = [];
  185. $del_ids = [];
  186. if ($type == 'update') {
  187. // 编辑商品 编辑规格
  188. foreach ($goods_stock as $key => $val) {
  189. if (isset($val['id']) && $val['id'] > 0) {
  190. // 修改规格
  191. $goods_stock_data[$val['id']] = [
  192. 'goods_id' => $id,
  193. 'name' => $val['name'],
  194. 'stock' => $val['stock'],
  195. ];
  196. } else {
  197. // 添加规格
  198. $add_data[] = [
  199. 'goods_id' => $id,
  200. 'name' => $val['name'],
  201. 'stock' => $val['stock'],
  202. ];
  203. }
  204. }
  205. if(isset($info['goods_stock'])){
  206. foreach ($info['goods_stock'] as $index) {
  207. if (array_key_exists($index['id'], $goods_stock_data)) {
  208. // 更新规格
  209. $save_data = $goods_stock_data[$index['id']];
  210. $index->save($save_data);
  211. } else {
  212. // 删除规格
  213. $del_ids[] = $index['id'];
  214. }
  215. }
  216. }
  217. if ($add_data) {
  218. GoodsStock::insertAll($add_data);
  219. }
  220. if ($del_ids) {
  221. GoodsStock::where(['id' => ['IN', $del_ids]])->delete();
  222. }
  223. } else {
  224. // 添加商品 添加规格
  225. foreach ($goods_stock as $key => $val) {
  226. $goods_stock_data[] = [
  227. 'goods_id' => $id,
  228. 'name' => $val['name'],
  229. 'stock' => $val['stock'],
  230. ];
  231. }
  232. if ($goods_stock_data) {
  233. GoodsStock::insertAll($goods_stock_data);
  234. }
  235. }
  236. return true;
  237. }
  238. /**
  239. * 删除
  240. * @auth true
  241. * @throws \think\Exception
  242. * @throws \think\exception\PDOException
  243. */
  244. public function remove()
  245. {
  246. $this->applyCsrfToken();
  247. // TODO 删除商品,检验申请数据
  248. $this->_delete($this->table);
  249. }
  250. /**
  251. * 启用
  252. * @auth true
  253. * @throws \think\Exception
  254. * @throws \think\exception\PDOException
  255. */
  256. public function resume()
  257. {
  258. $this->applyCsrfToken();
  259. $this->_save($this->table, ['status' => CommonConstant::STATUS_NORMAL]);
  260. }
  261. /**
  262. * 禁用
  263. * @auth true
  264. * @throws \think\Exception
  265. * @throws \think\exception\PDOException
  266. */
  267. public function forbid()
  268. {
  269. $this->applyCsrfToken();
  270. $this->_save($this->table, ['status' => CommonConstant::STATUS_FROZEN]);
  271. }
  272. }