Keepbrand.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. //品牌授权
  7. class Keepbrand extends Base
  8. {
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. }
  13. public function create(){
  14. $brand_book = isset($this->post['brand_book']) && $this->post['brand_book'] !=="" ? trim($this->post['brand_book']):"";
  15. // if($brand_book==""){
  16. // return error_show(1003,"参数brand_book不能为空");
  17. // }
  18. $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !=="" ? trim($this->post['gyscode']):"";
  19. // if($gyscode==""){
  20. // return error_show(1003,"参数gyscode不能为空");
  21. // }
  22. $brand_id = isset($this->post['brand_id']) && $this->post['brand_id'] !=="" ? trim($this->post['brand_id']):"";
  23. if($brand_id==""){
  24. return error_show(1003,"参数brand_id不能为空");
  25. }
  26. $rename = Db::name('brand_book')->where(['brand_id' => $brand_id,"gyscode"=>$gyscode,'is_del' => 0])->find();
  27. if (!empty($rename)) {
  28. return error_show(1002, "品牌名称已存在");
  29. }
  30. // $is_book = isset($this->post['is_book']) && $this->post['is_book'] !=="" ? trim($this->post['is_book']):"";
  31. // if($is_book==""){
  32. // return error_show(1003,"参数is_book不能为空");
  33. // }
  34. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  35. if($token==''){
  36. return error_show(105,"参数token不能为空");
  37. }
  38. // $user =GetUserInfo($token);
  39. // if(empty($user)||$user['code']!=0){
  40. // return error_show(1002,"创建人数据不存在");
  41. // }
  42. $createrid= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  43. $creater= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  44. $long = isset($this->post['long']) && $this->post['long'] !=="" ? intval($this->post['long']):"0";
  45. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"0";
  46. $data=[
  47. "brand_book"=>$brand_book,
  48. "gyscode"=>$gyscode,
  49. // "is_book"=>$is_book,
  50. "brand_id"=>$brand_id,
  51. "createrid"=>$createrid,
  52. "creater"=>$creater,
  53. "long"=>$long,
  54. "is_del"=>0,
  55. "status"=>$status,
  56. "addtime"=>date("Y-m-d H:i:s"),
  57. "updatetime"=>date("Y-m-d H:i:s")
  58. ];
  59. if($long===1){
  60. $starttime = isset($this->post['starttime']) && $this->post['starttime'] !==""
  61. ?$this->post['starttime']:null;
  62. // if($starttime==""){
  63. // return error_show(1005,'参数starttime不能为空');
  64. // }
  65. $endtime= isset($this->post['endtime']) && $this->post['endtime'] !=="" ?$this->post['endtime']:null;
  66. // if($endtime==""){
  67. // return error_show(1005,'修改时间不能为空');
  68. // }
  69. $data['starttime']=$starttime;
  70. $data['endtime']=$endtime;
  71. }
  72. $info = Db::name("brand_book")->insert($data);
  73. if($info){
  74. return error_show(0,"新建成功");
  75. }else{
  76. return error_show(1002,"新建失败");
  77. }
  78. }
  79. public function list(){
  80. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) :"1";
  81. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) :"10";
  82. $where=[['a.is_del',"=",0]];
  83. $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !=="" ? trim($this->post['gyscode']):"";
  84. if($gyscode!=""){
  85. $where[]=['a.gyscode',"like","%$gyscode%"];
  86. }
  87. $name = isset($this->post['name']) && $this->post['name'] !=="" ? trim($this->post['name']):"";
  88. if($name!=""){
  89. $where[]=['b.name',"like","%$name%"];
  90. }
  91. $registerCode = isset($this->post['registerCode']) && $this->post['registerCode'] !=="" ? trim($this->post['registerCode']):"";
  92. if($registerCode!=""){
  93. $where[]=['b.registerCode',"like","%$registerCode%"];
  94. }
  95. $count = Db::name('brand_book')
  96. ->alias('a')->join("supplier b","a.gyscode=b.code","left")
  97. ->where($where)->count();
  98. $total = ceil($count / $size);
  99. $page = $page >= $total ? $total : $page;
  100. $list = Db::name('brand_book')->alias('a')
  101. ->join("supplier b","a.gyscode=b.code","left")
  102. ->join("brand c","a.brand_id=c.id","left")
  103. ->where($where)->page($page,$size)->field("a.*,c.brand_name,b.name as supplier_name")->order("addtime desc")
  104. ->select();
  105. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  106. }
  107. public function info(){
  108. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  109. if($id==""){
  110. return error_show(1002,"参数id不能为空");
  111. }
  112. $info = Db::name("brand_book")->where(["id"=>$id])->find();
  113. if(!$info) {
  114. return error_show(1002, "未找到对应数据");
  115. }
  116. $data = Db::name("brand")->alias('a')
  117. ->join("brand_book b","b.brand_id=a.id")
  118. ->join("supplier c","b.gyscode=c.code","left")->field("b.*,a.brand_name,c.name")
  119. ->where(["a.id"=>$info['brand_id']])->find();
  120. if($data){
  121. return app_show(0,"获取成功",$data);
  122. }else{
  123. return app_show(1002,"获取失败",$data);
  124. }
  125. }
  126. public function status(){
  127. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  128. if($id==""){
  129. return error_show(1002,"参数id不能为空");
  130. }
  131. $info = Db::name("brand_book")->where([["id","=",$id]])->find();
  132. if(!$info){
  133. return error_show(1002,"未找到对应数据");
  134. }
  135. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  136. if($status===""){
  137. return error_show(1002,"参数status不能为空");
  138. }
  139. if(!in_array($status,[0,1])){
  140. return error_show(1002,"参数status无效");
  141. }
  142. $info['status']=$status;
  143. $info['updatetime']=date("Y-m-d H:i:s");
  144. $msg = $status==1?"启用":"禁用";
  145. $update = Db::name("brand_book")->save($info);
  146. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  147. }
  148. public function del(){
  149. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  150. if($id==""){
  151. return error_show(1004,"参数id不能为空");
  152. }
  153. $str= Db::name('brand_book')->where(['id'=>$id,'is_del'=>0])->find();
  154. if(empty($str)){
  155. return error_show(1002,"未找到数据");
  156. }
  157. $end = Db::name('brand_book')->update(['id'=>$id,'is_del'=>1]);
  158. if($end){
  159. return error_show(0,"删除成功");
  160. }else{
  161. return error_show(1002,"删除失败");
  162. }
  163. }
  164. public function edit(){
  165. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  166. if($id == ""){
  167. return error_show(1002,"参数id不能为空");
  168. }
  169. $info = Db::name('brand_book')->where(['id'=>$id,'is_del'=>0])->find();
  170. if($info==""){
  171. return error_show(1002,"未找到数据");
  172. }
  173. $brand_book = isset($this->post['brand_book']) && $this->post['brand_book'] !=="" ? trim($this->post['brand_book']):"";
  174. // if($brand_book==""){
  175. // return error_show(1002,"参数brand_book不能为空");
  176. // }
  177. // $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !== "" ? trim($this->post['gyscode']):"";
  178. // if($gyscode==""){
  179. // return error_show(1002,"参数gyscode不能为空");
  180. // }
  181. $brand_id = isset($this->post['brand_id']) && $this->post['brand_id'] !== "" ? intval($this->post['brand_id']):"";
  182. if($brand_id==""){
  183. return error_show(1002,"参数brand_id不能为空");
  184. }
  185. $rename = Db::name('brand_book')->where(['brand_id' => $brand_id, 'is_del' => 0])->find();
  186. if (empty($rename)) {
  187. return error_show(1002, "品牌名称已存在");
  188. }
  189. // $is_book = isset($this->post['is_book']) && $this->post['is_book'] !== "" ? intval($this->post['is_book']):"";
  190. // if($is_book==""){
  191. // return error_show(1002,"参数is_book不能为空");
  192. // }
  193. $long = isset($this->post['long']) && $this->post['long'] !=="" ? intval($this->post['long']):"0";
  194. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"0";
  195. if($status==""){
  196. return error_show(1002,"参数status不能为空");
  197. }
  198. $data=[
  199. "id"=>$id,
  200. "brand_book"=>$brand_book,
  201. // "gyscode"=>$gyscode,
  202. // "is_book"=>$is_book,
  203. "brand_id"=>$brand_id,
  204. "long"=>$long,
  205. "is_del"=>0,
  206. "status"=>$status,
  207. "updatetime"=>date("Y-m-d H:i:s")
  208. ];
  209. if($long===1){
  210. $starttime = isset($this->post['starttime']) && $this->post['starttime'] !=="" ?$this->post['starttime']:null;
  211. // if($starttime==""){
  212. // return error_show(1005,'参数starttime不能为空');
  213. // }
  214. $endtime= isset($this->post['endtime']) && $this->post['endtime'] !=="" ?$this->post['endtime']:null;
  215. // if($endtime==""){
  216. // return error_show(1005,'修改时间不能为空');
  217. // }
  218. $data['starttime']=$starttime;
  219. $data['endtime']=$endtime;
  220. }
  221. $info = Db::name("brand_book")->save($data);
  222. if($info){
  223. return error_show(0,"更新成功");
  224. }else{
  225. return error_show(1002,"更新失败");
  226. }
  227. }
  228. }