Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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\ApproveApplyGoods;
  17. use app\common\model\ApproveStockGoods;
  18. use app\common\model\ApproveUseGoods;
  19. use app\common\model\Goods as model;
  20. use app\common\model\GoodsStock;
  21. use app\common\service\GoodsCategoryService;
  22. use library\Controller;
  23. use think\Db;
  24. use think\Exception;
  25. /**
  26. * 商品
  27. */
  28. class Goods extends Controller
  29. {
  30. /**
  31. * 绑定数据表
  32. * @var string
  33. */
  34. protected $table = 'Goods';
  35. /**
  36. * 控制器初始化
  37. */
  38. protected function initialize()
  39. {
  40. $this->get_status_list = CommonConstant::get_status_list();
  41. }
  42. /**
  43. * 列表
  44. * @auth true
  45. * @menu true
  46. * @throws \think\Exception
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public function index()
  52. {
  53. $status = input('status');
  54. $goods_name = input('goods_name');
  55. $this->title = '商品列表';
  56. $data = model::field('is_deleted', true)
  57. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  58. ->when(array_key_exists($status, $this->get_status_list), function ($query) use ($status) {
  59. $query->where('status', $status);
  60. })
  61. ->when($goods_name, function ($query) use ($goods_name) {
  62. $query->where('goods_name', 'like', '%' . $goods_name . '%');
  63. })
  64. ->with([
  65. 'goodsCategoryOne' => function ($query) {
  66. $query->field('id,name');
  67. },
  68. 'goodsCategory' => function ($query) {
  69. $query->field('id,name');
  70. },
  71. 'goodsStock'
  72. ])
  73. ->order('sort desc,id desc');
  74. self::_init($data);
  75. }
  76. /**
  77. * 添加
  78. * @auth true
  79. * @throws \think\Exception
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. * @throws \think\exception\PDOException
  84. */
  85. public function add()
  86. {
  87. $this->title = '添加';
  88. $this->_submit('form');
  89. }
  90. /**
  91. * 编辑
  92. * @auth true
  93. * @throws \think\Exception
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. * @throws \think\exception\PDOException
  98. */
  99. public function edit()
  100. {
  101. $this->title = '编辑';
  102. $this->_submit('form');
  103. }
  104. protected function _submit($template)
  105. {
  106. $id = input('id') ?: 0;
  107. if ($this->request->isGet()) {
  108. list($data) = [[]];
  109. if ($id > 0) {
  110. $info = model::field('is_deleted', true)
  111. ->with([
  112. 'goodsStock'
  113. ])
  114. ->find($id);
  115. if (!$info){
  116. $this->error('该商品不存在或已删除');
  117. }
  118. $info['first_classify'] = $info['goods_category_first'];
  119. $info['second_classify'] = $info['goods_category_id'];
  120. $data = $info;
  121. }
  122. $this->category_list = GoodsCategoryService::get_list([], 1);
  123. return $this->fetch($template, ['vo' => $data]);
  124. }
  125. if ($this->request->isPost()) {
  126. list($data) = [$this->request->post()];
  127. if (!isset_full($data, 'goods_stock')) {
  128. $this->error('请添加商品规格');
  129. }
  130. if (!array_filter($data['goods_stock'])) {
  131. $this->error('请添加商品规格!');
  132. }
  133. $data['goods_category_first'] = $data['first_classify'];
  134. $data['goods_category_id'] = $data['second_classify'];
  135. if($id > 0){
  136. $info = model::field('is_deleted', true)
  137. ->with([
  138. 'goodsStock'
  139. ])
  140. ->find($id);
  141. if (!$info){
  142. $this->error('该商品不存在或已删除');
  143. }
  144. $goods_info = model::field('id')
  145. ->where('goods_category_first', $data['goods_category_first'])
  146. ->where('goods_category_id', $data['goods_category_id'])
  147. ->where('goods_name', $data['goods_name'])
  148. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  149. ->where('id', 'neq',$id)
  150. ->find();
  151. if ($goods_info) {
  152. $this->error('该商品已存在不能重复添加');
  153. }
  154. } else{
  155. $goods_info = model::field('id')
  156. ->where('goods_category_first', $data['goods_category_first'])
  157. ->where('goods_category_id', $data['goods_category_id'])
  158. ->where('goods_name', $data['goods_name'])
  159. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  160. ->find();
  161. if ($goods_info) {
  162. $this->error('该商品已存在不能重复添加');
  163. }
  164. }
  165. Db::startTrans();
  166. try {
  167. if ($id > 0) {
  168. self::create_stock($id, $data,$info,'update');
  169. $info->force(true)->save($data);
  170. } else {
  171. $result = model::create($data);
  172. $id = $result->id;
  173. self::create_stock($id, $data,[],'create');
  174. }
  175. Db::commit();
  176. } catch (Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. $this->success('商品编辑成功!', 'javascript:history.back()');
  181. }
  182. }
  183. protected function create_stock($id, $data,$info,$type)
  184. {
  185. $goods_stock = $data['goods_stock'];
  186. $goods_stock_data = [];
  187. $add_data = [];
  188. $del_ids = [];
  189. if ($type == 'update') {
  190. // 编辑商品 编辑规格
  191. foreach ($goods_stock as $key => $val) {
  192. if (isset($val['id']) && $val['id'] > 0) {
  193. // 修改规格
  194. $goods_stock_data[$val['id']] = [
  195. 'goods_id' => $id,
  196. 'name' => $val['name'],
  197. 'stock' => $val['stock'],
  198. ];
  199. } else {
  200. // 添加规格
  201. $add_data[] = [
  202. 'goods_id' => $id,
  203. 'name' => $val['name'],
  204. 'stock' => $val['stock'],
  205. ];
  206. }
  207. }
  208. if(isset($info['goods_stock'])){
  209. foreach ($info['goods_stock'] as $index) {
  210. if (array_key_exists($index['id'], $goods_stock_data)) {
  211. // 更新规格
  212. $save_data = $goods_stock_data[$index['id']];
  213. $index->save($save_data);
  214. } else {
  215. // 删除规格
  216. $del_ids[] = $index['id'];
  217. }
  218. }
  219. }
  220. if ($add_data) {
  221. GoodsStock::insertAll($add_data);
  222. }
  223. if ($del_ids) {
  224. GoodsStock::where(['id' => ['IN', $del_ids]])->delete();
  225. }
  226. } else {
  227. // 添加商品 添加规格
  228. foreach ($goods_stock as $key => $val) {
  229. $goods_stock_data[] = [
  230. 'goods_id' => $id,
  231. 'name' => $val['name'],
  232. 'stock' => $val['stock'],
  233. ];
  234. }
  235. if ($goods_stock_data) {
  236. GoodsStock::insertAll($goods_stock_data);
  237. }
  238. }
  239. return true;
  240. }
  241. /**
  242. * 删除
  243. * @auth true
  244. * @throws \think\Exception
  245. * @throws \think\exception\PDOException
  246. */
  247. public function remove()
  248. {
  249. $this->applyCsrfToken();
  250. // 删除商品时,检验申请数据
  251. $id = input('id') ?: 0;
  252. $apply = ApproveApplyGoods::where('goods_id',$id)->column('info_id');
  253. $stock = ApproveStockGoods::where('goods_id',$id)->column('info_id');
  254. $use = ApproveUseGoods::where('goods_id',$id)->column('info_id');
  255. $data = array_merge($apply,$stock);
  256. $info_id = array_merge($data,$use);
  257. if($info_id){
  258. $approve = \app\common\model\ApproveInfo::field('id')
  259. ->where('id','in',$info_id)
  260. ->where('status',CommonConstant::STATUS_2)
  261. ->where('is_deleted',CommonConstant::IS_DELETED_0)
  262. ->find();
  263. if($approve){
  264. $this->error('存在有审批中的申请单,不能删除');
  265. }
  266. }
  267. $this->_delete($this->table);
  268. }
  269. /**
  270. * 启用
  271. * @auth true
  272. * @throws \think\Exception
  273. * @throws \think\exception\PDOException
  274. */
  275. public function resume()
  276. {
  277. $this->applyCsrfToken();
  278. $this->_save($this->table, ['status' => CommonConstant::STATUS_NORMAL]);
  279. }
  280. /**
  281. * 禁用
  282. * @auth true
  283. * @throws \think\Exception
  284. * @throws \think\exception\PDOException
  285. */
  286. public function forbid()
  287. {
  288. $this->applyCsrfToken();
  289. $this->_save($this->table, ['status' => CommonConstant::STATUS_FROZEN]);
  290. }
  291. }