Good.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\Admin\controller;
  3. use think\facade\Db;
  4. class Good extends Base
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. public function list(){
  11. $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
  12. $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
  13. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  14. $where = ["is_del"=>0];
  15. if ($status !== "") {
  16. $where['status'] = $status;
  17. }
  18. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name'])
  19. : "";
  20. if ($good_name !== "") {
  21. $where['good_name'] = array("like","%$good_name%");
  22. }
  23. $group_id = isset($this->post['group_id']) && $this->post['group_id'] !== "" ? intval($this->post['group_id'])
  24. : "";
  25. if ($group_id !== "") {
  26. $where['group_id'] =$group_id;
  27. }
  28. $start = isset($this->post['start']) &&$this->post['start']!=""? $this->post['start'] :"";
  29. if($start!==""){
  30. $where['addtime'] =array(">=",date("Y-m-d H:i:s",strtotime($start)));
  31. }
  32. $end = isset($this->post['end']) &&$this->post['end']!=""? $this->post['end'] :"";
  33. if($end!==""){
  34. $where['addtime'] =array("<=",date("Y-m-d H:i:s",strtotime($end)+24*3600));
  35. }
  36. $count =Db::name("good")->where($where)->count();
  37. $total = ceil($count/$size);
  38. $page = $page>$total?$total:$page;
  39. $list = Db::name("good")->where($where)->page($page,$size)->select();
  40. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  41. }
  42. public function create(){
  43. $good_name = isset($this->post['good_name'])&&$this->post['good_name']!=""?trim($this->post['good_name']):"";
  44. if($good_name==""){
  45. return error_show(1004,"参数good_name不能为空");
  46. }
  47. $good_desc = isset($this->post['good_desc'])&&$this->post['good_desc']!=""?trim($this->post['good_desc']):"";
  48. $good_unit = isset($this->post['good_unit'])&&$this->post['good_unit']!=""?trim($this->post['good_unit']):"";
  49. if($good_unit==""){
  50. return error_show(1004,"参数good_unit不能为空");
  51. }
  52. $good_img = isset($this->post['good_img'])&&$this->post['good_img']!=""?trim($this->post['good_img']):"";
  53. if($good_img==""){
  54. return error_show(1004,"参数good_img不能为空");
  55. }
  56. $origin_place = isset($this->post['origin_place'])&&$this->post['origin_place']!=""?trim($this->post['origin_place']):"";
  57. $group_id = isset($this->post['group_id'])&&$this->post['group_id']!==""?intval($this->post['group_id']):"";
  58. if($group_id===""){
  59. return error_show(1004,"参数group_id不能为空");
  60. }
  61. $isT = Db::name("good")->where(["good_name"=>$good_name,"is_del"=>0,"group_id"=>$group_id])->find();
  62. if($isT){
  63. return error_show(1004,"商品名称已存在");
  64. }
  65. $data=[
  66. "good_name"=>$good_name,
  67. "good_desc"=>$good_desc,
  68. "good_unit"=>$good_unit,
  69. "good_img"=>$good_img,
  70. "origin_place"=>$origin_place,
  71. "group_id"=>$group_id,
  72. "createrid"=>$this->userinfo['id'],
  73. "addtime"=>date("Y-m-d H:i:s"),
  74. "updatetime"=>date("Y-m-d H:i:s")
  75. ];
  76. $in = Db::name("good")->insert($data);
  77. if($in){
  78. return app_show(0,"新建成功");
  79. }else{
  80. return app_show(1004,"新建失败");
  81. }
  82. }
  83. public function edit(){
  84. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']):"";
  85. if($id===""){
  86. return error_show(1004,"参数id不能为空");
  87. }
  88. $data = Db::name("good")->where(["id"=>$id,"is_del"=>0])->find();
  89. if($data==false){
  90. return error_show(1004,"未找到商品数据");
  91. }
  92. $good_name = isset($this->post['good_name'])&&$this->post['good_name']!=""?trim($this->post['good_name']):"";
  93. if($good_name==""){
  94. return error_show(1004,"参数good_name不能为空");
  95. }
  96. $group_id = isset($this->post['group_id'])&&$this->post['group_id']!=""?intval($this->post['group_id']):"";
  97. if($group_id==""){
  98. return error_show(1004,"参数group_id不能为空");
  99. }
  100. $isT = Db::name("good")->where(["good_name"=>$good_name,"is_del"=>0,"group_id"=>$group_id])->find();
  101. if($isT && $isT['id']!=$data['id']){
  102. return error_show(1004,"商品名称已存在");
  103. }
  104. $good_desc = isset($this->post['good_desc'])&&$this->post['good_desc']!=""?trim($this->post['good_desc']):"";
  105. $good_unit = isset($this->post['good_unit'])&&$this->post['good_unit']!=""?trim($this->post['good_unit']):"";
  106. if($good_unit==""){
  107. return error_show(1004,"参数good_unit不能为空");
  108. }
  109. $good_img = isset($this->post['good_img'])&&$this->post['good_img']!=""?trim($this->post['good_img']):"";
  110. if($good_img==""){
  111. return error_show(1004,"参数good_img不能为空");
  112. }
  113. $origin_place = isset($this->post['origin_place'])&&$this->post['origin_place']!=""?trim($this->post['origin_place']):"";
  114. $datas=[
  115. "good_name"=>$good_name,
  116. "good_desc"=>$good_desc,
  117. "good_unit"=>$good_unit,
  118. "good_img"=>$good_img,
  119. "origin_place"=>$origin_place,
  120. "group_id"=>$group_id,
  121. "updatetime"=>date("Y-m-d H:i:s")
  122. ];
  123. $in = Db::name("good")->where($data)->save($datas);
  124. if($in){
  125. return app_show(0,"编辑成功");
  126. }else{
  127. return error_show(1004,"编辑失败");
  128. }
  129. }
  130. public function delete(){
  131. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']):"";
  132. if($id===""){
  133. return error_show(1004,"参数id不能为空");
  134. }
  135. $data = Db::name("good")->where(["id"=>$id,"is_del"=>0])->find();
  136. if($data==false){
  137. return error_show(1004,"未找到商品数据");
  138. }
  139. $data['is_del']=1;
  140. $data['updatetime']=date("Y-m-d H:i:s");
  141. $del = Db::name("good")->save($data);
  142. if($del){
  143. return app_show(0,"删除成功");
  144. }else{
  145. return error_show(1004,"删除失败");
  146. }
  147. }
  148. public function status(){
  149. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']):"";
  150. if($id===""){
  151. return error_show(1004,"参数id不能为空");
  152. }
  153. $data = Db::name("good")->where(["id"=>$id,"is_del"=>0])->find();
  154. if($data==false){
  155. return error_show(1004,"未找到商品数据");
  156. }
  157. $status = isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  158. if($status===""){
  159. return error_show(1004,"参数status不能为空");
  160. }
  161. $msg = $status==1?"启用":"禁用";
  162. $data['status']=$status;
  163. $data['updatetime']=date("Y-m-d H:i:s");
  164. $del = Db::name("good")->save($data);
  165. if($del){
  166. return app_show(0,"{$msg}成功");
  167. }else{
  168. return error_show(1004,"{$msg}失败");
  169. }
  170. }
  171. public function info(){
  172. $id = isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']):"";
  173. if($id===""){
  174. return error_show(1004,"参数id不能为空");
  175. }
  176. $data = Db::name("good")->where(["id"=>$id,"is_del"=>0])->find();
  177. if($data==false){
  178. return error_show(1004,"未找到商品数据");
  179. }
  180. $creater = Db::name("admin")->where(["id"=>$data['createrid']])->find();
  181. $data['creater'] = isset($creater['username']) ? $creater['username']:"";
  182. return app_show(0,"获取成功",$data);
  183. }
  184. }