Brand.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. {
  53. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  54. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  55. $where = [["is_del", "=", 0]];
  56. $brand_name = isset($this->post['brand_name']) && $this->post['brand_name'] !== "" ? trim($this->post['brand_name']) : "";
  57. if ($brand_name !== "") {
  58. $where[] = ['brand_name', "like", "%$brand_name%"];
  59. }
  60. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status'])
  61. : "";
  62. if ($status !== "") {
  63. $where[] = ['status', "=", $status];
  64. }
  65. $creater = isset($this->post['creater']) && $this->post['creater'] !== "" ? trim($this->post['creater']) : "";
  66. if ($creater !== "") {
  67. $where[] = ['creater', "like", "%$creater%"];
  68. }
  69. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  70. if ($start !== "") {
  71. $where[] = ['addtime', ">=", date('Y-m-d H:i:s', strtotime($start))];
  72. }
  73. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  74. if ($end !== "") {
  75. $where[] = ['addtime', "<", date('Y-m-d H:i:s', strtotime($end) + 24 * 3600)];
  76. }
  77. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  78. if($supplierNo!=""){
  79. $gyscode = Db::name("supplier")->where(["code"=>$supplierNo])->find();
  80. if(empty($gyscode)){
  81. return error_show(1004,"未找到供应商信息");
  82. }
  83. $wsmcode = Db::name("brand_book")->where(["gyscode" => $supplierNo, "is_del" => 0])->column("brand_id");
  84. $where[] = ['id', "in", $wsmcode];
  85. }
  86. $count = Db::name('brand') ->where($where)->count();
  87. $total = ceil($count / $size);
  88. $page = $page >= $total ? $total : $page;
  89. $list = Db::name('brand')->where($where)->page($page,$size)->order("addtime desc,id desc")->select();
  90. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  91. }
  92. public function edit(){
  93. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  94. if($id==""){
  95. return error_show(1002,"参数id不能为空");
  96. }
  97. $info = Db::name('brand')->where(['id'=>$id,"is_del"=>0])->find();
  98. if($info==""){
  99. return error_show(1003,"未找到数据");
  100. }
  101. $brand_name=isset( $this->post['brand_name']) && $this->post['brand_name'] !=="" ? trim($this->post['brand_name']):"";
  102. if($brand_name==""){
  103. return error_show(1002,"参数brand_name不能为空");
  104. }
  105. $logo_url= isset($this->post['logo_url']) && $this->post['logo_url'] !=="" ? trim($this->post['logo_url']):"";
  106. // $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  107. // if($token==''){
  108. // return error_show(1005,"参数token不能为空");
  109. // }
  110. // $user =GetUserInfo($token);
  111. // if(empty($user)||$user['code']!=0){
  112. // return error_show(1002,"创建人数据不存在");
  113. // }
  114. // $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  115. // $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  116. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  117. $data=[
  118. "id"=>$id,
  119. "brand_name"=>$brand_name,
  120. "logo_url"=>$logo_url,
  121. // "createrid"=>$createrid,
  122. // "creater"=>$creater,
  123. "status"=>$status,
  124. "is_del"=>0,
  125. "updatetime"=>date("Y-m-d H:i:s")
  126. ];
  127. $item = Db::name("brand")->save($data);
  128. return $item? error_show(0,"更新成功"):error_show(1002,"更新失败");
  129. }
  130. public function status()
  131. {
  132. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  133. if($id==""){
  134. return error_show(1002,"参数id不能为空");
  135. }
  136. $info = Db::name("brand")->where([["id","=",$id]])->find();
  137. if(!$info){
  138. return error_show(1002,"未找到对应数据");
  139. }
  140. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  141. if($status===""){
  142. return error_show(1002,"参数status不能为空");
  143. }
  144. if(!in_array($status,[0,1])){
  145. return error_show(1002,"参数status无效");
  146. }
  147. $info['status']=$status;
  148. $info['updatetime']=date("Y-m-d H:i:s");
  149. $msg = $status==1?"启用":"禁用";
  150. $update = Db::name("brand")->save($info);
  151. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  152. }
  153. public function del(){
  154. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  155. if($id===""){
  156. return error_show(1004,"参数id不能为空");
  157. }
  158. $str= Db::name('brand')->where(['id'=>$id,'is_del'=>0])->find();
  159. if(empty($str)){
  160. return error_show(1002,"未找到数据");
  161. }
  162. $end = Db::name('brand')->update(['id'=>$id,'is_del'=>1]);
  163. if($end){
  164. return error_show(0,"删除成功");
  165. }else{
  166. return error_show(1002,"删除失败");
  167. }
  168. }
  169. public function info(){
  170. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  171. if($id==""){
  172. return error_show(1002,"参数id不能为空");
  173. }
  174. $info = Db::name("brand")->where([["id","=",$id]])->find();
  175. if(!$info) {
  176. return error_show(1002, "未找到对应数据");
  177. }
  178. $data = Db::name("brand_book")->alias('a')
  179. ->join("brand b","a.brand_id=b.id")->field("b.logo_url,b.brand_name")
  180. ->join("supplier c","c.code= a.gyscode","left")
  181. ->field("a.creater,a.addtime,c.code as gyscode,c.name,c.status")->where(['brand_id'=>$info['id']])->find();
  182. if($data){
  183. return app_show(0,"获取成功",$data);
  184. }else{
  185. return app_show(1002,"获取失败",$data);
  186. }
  187. }
  188. }