Brand.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(105,"参数token不能为空");
  26. }
  27. $user =GetUserInfo($token);
  28. if(empty($user)||$user['code']!=0){
  29. return error_show(102,"创建人数据不存在");
  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. if($info['status']==1){
  102. return error_show(1002,"状态是启用状态,无法编辑");
  103. }
  104. $brand_name=isset( $this->post['brand_name']) && $this->post['brand_name'] !=="" ? trim($this->post['brand_name']):"";
  105. if($brand_name==""){
  106. return error_show(1002,"参数brand_name不能为空");
  107. }
  108. $logo_url= isset($this->post['logo_url']) && $this->post['logo_url'] !=="" ? trim($this->post['logo_url']):"";
  109. // $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  110. // if($token==''){
  111. // return error_show(105,"参数token不能为空");
  112. // }
  113. // $user =GetUserInfo($token);
  114. // if(empty($user)||$user['code']!=0){
  115. // return error_show(1002,"创建人数据不存在");
  116. // }
  117. // $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  118. // $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  119. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  120. $data=[
  121. "id"=>$id,
  122. "brand_name"=>$brand_name,
  123. "logo_url"=>$logo_url,
  124. // "createrid"=>$createrid,
  125. // "creater"=>$creater,
  126. "status"=>$status,
  127. "is_del"=>0,
  128. "updatetime"=>date("Y-m-d H:i:s")
  129. ];
  130. $item = Db::name("brand")->save($data);
  131. return $item? error_show(0,"更新成功"):error_show(1002,"更新失败");
  132. }
  133. public function status()
  134. {
  135. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  136. if($id==""){
  137. return error_show(1002,"参数id不能为空");
  138. }
  139. $info = Db::name("brand")->where([["id","=",$id]])->find();
  140. if(!$info){
  141. return error_show(1002,"未找到对应数据");
  142. }
  143. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  144. if($status===""){
  145. return error_show(1002,"参数status不能为空");
  146. }
  147. if(!in_array($status,[0,1])){
  148. return error_show(1002,"参数status无效");
  149. }
  150. $info['status']=$status;
  151. $info['updatetime']=date("Y-m-d H:i:s");
  152. $msg = $status==1?"启用":"禁用";
  153. $update = Db::name("brand")->save($info);
  154. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  155. }
  156. public function del(){
  157. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  158. if($id===""){
  159. return error_show(1004,"参数id不能为空");
  160. }
  161. $str= Db::name('brand')->where(['id'=>$id,'is_del'=>0])->find();
  162. if(empty($str)){
  163. return error_show(1002,"未找到数据");
  164. }
  165. $end = Db::name('brand')->update(['id'=>$id,'is_del'=>1]);
  166. if($end){
  167. return error_show(0,"删除成功");
  168. }else{
  169. return error_show(1002,"删除失败");
  170. }
  171. }
  172. public function info(){
  173. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  174. if($id==""){
  175. return error_show(1002,"参数id不能为空");
  176. }
  177. $info = Db::name("brand")->where([["id","=",$id]])->find();
  178. if(!$info) {
  179. return error_show(1002, "未找到对应数据");
  180. }
  181. $data = Db::name("brand_book")->alias('a')
  182. ->join("brand b","a.brand_id=b.id")->field("b.logo_url,b.brand_name")
  183. ->join("supplier c","c.code= a.gyscode","left")
  184. ->field("a.creater,a.addtime,c.code as gyscode,c.name,c.status")->where(['brand_id'=>$info['id']])->find();
  185. if($data){
  186. return app_show(0,"获取成功",$data);
  187. }else{
  188. return app_show(1002,"获取失败",$data);
  189. }
  190. }
  191. }