Brand.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. class Brand extends BaseController
  8. {
  9. public $post="";
  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. // "createrid"=>$createrid,
  126. // "creater"=>$creater,
  127. "status"=>$status,
  128. "is_del"=>0,
  129. "updatetime"=>date("Y-m-d H:i:s")
  130. ];
  131. $temp = array_diff($data,$info);
  132. $json = json_encode($temp,JSON_UNESCAPED_UNICODE);
  133. $jsp = json_encode($info,JSON_UNESCAPED_UNICODE);
  134. $item = Db::name("brand")->save($data);
  135. ChangeLog::logAdd(6,$info['id'],$jsp,$json,$this->post['token'],$this->post);
  136. if($item){
  137. return error_show(0,"更新成功");
  138. }else{
  139. return error_show(1002,"更新失败");
  140. }
  141. }
  142. public function status()
  143. {
  144. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  145. if($id==""){
  146. return error_show(1002,"参数id不能为空");
  147. }
  148. $info = Db::name("brand")->where([["id","=",$id]])->find();
  149. if(!$info){
  150. return error_show(1002,"未找到对应数据");
  151. }
  152. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  153. if($status===""){
  154. return error_show(1002,"参数status不能为空");
  155. }
  156. if(!in_array($status,[0,1])){
  157. return error_show(1002,"参数status无效");
  158. }
  159. $info['status']=$status;
  160. $info['updatetime']=date("Y-m-d H:i:s");
  161. $msg = $status==1?"启用":"禁用";
  162. $update = Db::name("brand")->save($info);
  163. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  164. }
  165. public function del(){
  166. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  167. if($id===""){
  168. return error_show(1004,"参数id不能为空");
  169. }
  170. $str= Db::name('brand')->where(['id'=>$id,'is_del'=>0])->find();
  171. if(empty($str)){
  172. return error_show(1002,"未找到数据");
  173. }
  174. $end = Db::name('brand')->update(['id'=>$id,'is_del'=>1]);
  175. if($end){
  176. return error_show(0,"删除成功");
  177. }else{
  178. return error_show(1002,"删除失败");
  179. }
  180. }
  181. public function info(){
  182. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  183. if($id==""){
  184. return error_show(1002,"参数id不能为空");
  185. }
  186. $info = Db::name("brand")->where([["id","=",$id]])->find();
  187. if(!$info) {
  188. return error_show(1002, "未找到对应数据");
  189. }
  190. $data = Db::name("brand_book")->alias('a')
  191. ->join("brand b","a.brand_id=b.id")->field("b.logo_url,b.brand_name")
  192. ->join("supplier c","c.code= a.gyscode","left")
  193. ->field("a.creater,a.addtime,c.code as gyscode,c.name,c.status")->where(['brand_id'=>$info['id']])->find();
  194. if($data){
  195. return app_show(0,"获取成功",$data);
  196. }else{
  197. return app_show(1002,"获取失败",$data);
  198. }
  199. }
  200. }