Brand.php 8.7 KB

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