Brand.php 8.6 KB

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