Brand.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class Brand extends BaseController
  7. {
  8. public $post="";
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->post=$this->request->post();
  13. }
  14. public function create(){
  15. $brand_name=isset( $this->post['brand_name']) && $this->post['brand_name'] !=="" ? trim($this->post['brand_name']):"";
  16. if($brand_name==""){
  17. return error_show(1002,"参数brand_name不能为空");
  18. }
  19. $logo_url= isset($this->post['logo_url']) && $this->post['logo_url'] !=="" ? trim($this->post['logo_url']):"";
  20. // if($logo_url==""){
  21. // return error_show(1002,"参数logo_url不能为空");
  22. // }
  23. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  24. if($token==''){
  25. return error_show(1005,"参数token不能为空");
  26. }
  27. $user =GetUserInfo($token);
  28. if(empty($user)||$user['code']!=0){
  29. return error_show(1002,"创建人数据不存在");
  30. }
  31. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  32. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  33. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"1";
  34. $data=[
  35. "brand_name"=>$brand_name,
  36. "logo_url"=>$logo_url,
  37. "createrid"=>$createrid,
  38. "creater"=>$creater,
  39. "status"=>$status,
  40. "is_del"=>0,
  41. "addtime"=>date("Y-m-d H:i:s"),
  42. "updatetime"=>date("Y-m-d H:i:s")
  43. ];
  44. $datainfo =Db::name('brand')->insert($data);
  45. if($datainfo){
  46. return error_show(0,"新建成功");
  47. }else{
  48. return error_show(1002,"新建失败");
  49. }
  50. }
  51. public function list(){
  52. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  53. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  54. $where=[["is_del","=",0]];
  55. $brand_name = isset($this->post['brand_name']) && $this->post['brand_name'] !=="" ? trim($this->post['brand_name']):"";
  56. if($brand_name!==""){
  57. $where[]=['brand_name',"like","%$brand_name%"];
  58. }
  59. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  60. :"";
  61. if($status!==""){
  62. $where[]=['status',"=",$status];
  63. }
  64. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):"";
  65. if($creater!==""){
  66. $where[]=['creater',"like","%$creater%"];
  67. }
  68. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  69. if($start!==""){
  70. $where[]=['addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  71. }
  72. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  73. if($end!==""){
  74. $where[]=['addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  75. }
  76. $count = Db::name('brand') ->where($where)->count();
  77. $total = ceil($count / $size);
  78. $page = $page >= $total ? $total : $page;
  79. $list = Db::name('brand')->where($where)->page($page,$size)->order("addtime desc")->select();
  80. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  81. }
  82. public function edit(){
  83. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  84. if($id==""){
  85. return error_show(1002,"参数id不能为空");
  86. }
  87. $info = Db::name('brand')->where(['id'=>$id,"is_del"=>0])->find();
  88. if($info==""){
  89. return error_show(1003,"未找到数据");
  90. }
  91. $brand_name=isset( $this->post['brand_name']) && $this->post['brand_name'] !=="" ? trim($this->post['brand_name']):"";
  92. if($brand_name==""){
  93. return error_show(1002,"参数brand_name不能为空");
  94. }
  95. $logo_url= isset($this->post['logo_url']) && $this->post['logo_url'] !=="" ? trim($this->post['logo_url']):"";
  96. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  97. if($token==''){
  98. return error_show(1005,"参数token不能为空");
  99. }
  100. $user =GetUserInfo($token);
  101. if(empty($user)||$user['code']!=0){
  102. return error_show(1002,"创建人数据不存在");
  103. }
  104. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  105. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  106. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  107. $data=[
  108. "id"=>$id,
  109. "brand_name"=>$brand_name,
  110. "logo_url"=>$logo_url,
  111. "createrid"=>$createrid,
  112. "creater"=>$creater,
  113. "status"=>$status,
  114. "is_del"=>0,
  115. "updatetime"=>date("Y-m-d H:i:s")
  116. ];
  117. $item = Db::name("brand")->save($data);
  118. return $item? error_show(0,"更新成功"):error_show(1002,"更新失败");
  119. }
  120. public function status()
  121. {
  122. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  123. if($id==""){
  124. return error_show(1002,"参数id不能为空");
  125. }
  126. $info = Db::name("brand")->where([["id","=",$id]])->find();
  127. if(!$info){
  128. return error_show(1002,"未找到对应数据");
  129. }
  130. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  131. if($status===""){
  132. return error_show(1002,"参数status不能为空");
  133. }
  134. if(!in_array($status,[0,1])){
  135. return error_show(1002,"参数status无效");
  136. }
  137. $info['status']=$status;
  138. $info['updatetime']=date("Y-m-d H:i:s");
  139. $msg = $status==1?"启用":"禁用";
  140. $update = Db::name("brand")->save($info);
  141. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  142. }
  143. public function del(){
  144. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  145. if($id===""){
  146. return error_show(1004,"参数id不能为空");
  147. }
  148. $str= Db::name('brand')->where(['id'=>$id,'is_del'=>0])->find();
  149. if(empty($str)){
  150. return error_show(1002,"未找到数据");
  151. }
  152. $end = Db::name('brand')->update(['id'=>$id,'is_del'=>1]);
  153. if($end){
  154. return error_show(0,"删除成功");
  155. }else{
  156. return error_show(1002,"删除失败");
  157. }
  158. }
  159. public function info(){
  160. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  161. if($id==""){
  162. return error_show(1002,"参数id不能为空");
  163. }
  164. $info = Db::name("brand")->where([["id","=",$id]])->find();
  165. if(!$info) {
  166. return error_show(1002, "未找到对应数据");
  167. }
  168. $data = Db::name("brand_book")->alias('a')
  169. ->join("brand b","a.brand_id=b.id")->field("b.logo_url,b.brand_name")
  170. ->join("supplier c","c.code= a.gyscode","left")
  171. ->field("a.creater,a.addtime,c.code as gyscode,c.name,c.status")->where(['brand_id'=>$info['id']])->find();
  172. if($data){
  173. return app_show(0,"获取成功",$data);
  174. }else{
  175. return app_show(1002,"获取失败",$data);
  176. }
  177. }
  178. }