Brand.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 = [["b.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[] = ['b.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[] = ['b.status', "=", $status];
  65. }
  66. $creater = isset($this->post['creater']) && $this->post['creater'] !== "" ? trim($this->post['creater']) : "";
  67. if ($creater !== "") {
  68. $where[] = ['b.creater', "like", "%$creater%"];
  69. }
  70. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  71. if ($start !== "") {
  72. $where[] = ['b.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[] = ['b.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[] = ['b.id', "in", $wsmcode];
  86. }
  87. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  88. if ($company_name !== "") $where[] = ["b.createrid", 'in', get_company_item_user_by_name($company_name)];
  89. $count = Db::name('brand')
  90. ->alias('b')
  91. ->where($where)
  92. ->count();
  93. $total = ceil($count / $size);
  94. $page = $page >= $total ? $total : $page;
  95. $list = Db::name('brand')
  96. ->alias('b')
  97. ->where($where)
  98. ->page($page,$size)
  99. ->order("addtime desc,id desc")
  100. ->select();
  101. $all_createrid = array_column($list,'createrid');
  102. $item = get_company_name_by_uid($all_createrid);
  103. foreach ($list as &$val){
  104. $val['company_name']=$item[$val['createrid']]??'';
  105. }
  106. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  107. }
  108. public function edit(){
  109. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  110. if($id==""){
  111. return error_show(1002,"参数id不能为空");
  112. }
  113. $info = Db::name('brand')->where(['id'=>$id,"is_del"=>0])->find();
  114. if($info==""){
  115. return error_show(1003,"未找到数据");
  116. }
  117. if($info['status']==1){
  118. return error_show(1002,"状态是启用状态,无法编辑");
  119. }
  120. $brand_name=isset( $this->post['brand_name']) && $this->post['brand_name'] !=="" ? trim($this->post['brand_name']):"";
  121. if($brand_name==""){
  122. return error_show(1002,"参数brand_name不能为空");
  123. }
  124. $logo_url= isset($this->post['logo_url']) && $this->post['logo_url'] !=="" ? trim($this->post['logo_url']):"";
  125. // $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  126. // if($token==''){
  127. // return error_show(105,"参数token不能为空");
  128. // }
  129. // $user =GetUserInfo($token);
  130. // if(empty($user)||$user['code']!=0){
  131. // return error_show(1002,"创建人数据不存在");
  132. // }
  133. // $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  134. // $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  135. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  136. $data=[
  137. "id"=>$id,
  138. "brand_name"=>$brand_name,
  139. "logo_url"=>$logo_url,
  140. "status"=>$status,
  141. "is_del"=>0,
  142. "updatetime"=>date("Y-m-d H:i:s")
  143. ];
  144. $temp = array_diff($data,$info);
  145. $json = json_encode($temp,JSON_UNESCAPED_UNICODE);
  146. $jsp = json_encode($info,JSON_UNESCAPED_UNICODE);
  147. $item = Db::name("brand")->save($data);
  148. ChangeLog::logAdd(6,$info['id'],$jsp,$json,$this->post['token'],$this->post);
  149. if($item){
  150. return error_show(0,"更新成功");
  151. }else{
  152. return error_show(1002,"更新失败");
  153. }
  154. }
  155. public function status()
  156. {
  157. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  158. if($id==""){
  159. return error_show(1002,"参数id不能为空");
  160. }
  161. $info = Db::name("brand")->where([["id","=",$id]])->find();
  162. if(!$info){
  163. return error_show(1002,"未找到对应数据");
  164. }
  165. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  166. if($status===""){
  167. return error_show(1002,"参数status不能为空");
  168. }
  169. if(!in_array($status,[0,1])){
  170. return error_show(1002,"参数status无效");
  171. }
  172. $info['status']=$status;
  173. $info['updatetime']=date("Y-m-d H:i:s");
  174. $msg = $status==1?"启用":"禁用";
  175. $update = Db::name("brand")->save($info);
  176. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  177. }
  178. public function del(){
  179. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  180. if($id===""){
  181. return error_show(1004,"参数id不能为空");
  182. }
  183. $str= Db::name('brand')->where(['id'=>$id,'is_del'=>0])->find();
  184. if(empty($str)){
  185. return error_show(1002,"未找到数据");
  186. }
  187. $end = Db::name('brand')->update(['id'=>$id,'is_del'=>1]);
  188. if($end){
  189. return error_show(0,"删除成功");
  190. }else{
  191. return error_show(1002,"删除失败");
  192. }
  193. }
  194. public function info(){
  195. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  196. if($id==""){
  197. return error_show(1002,"参数id不能为空");
  198. }
  199. $info = Db::name("brand")->where([["id","=",$id]])->find();
  200. if(!$info) {
  201. return error_show(1002, "未找到对应数据");
  202. }
  203. $data = Db::name("brand_book")->alias('a')
  204. ->join("brand b","a.brand_id=b.id")->field("b.logo_url,b.brand_name")
  205. ->join("supplier c","c.code= a.gyscode","left")
  206. ->field("a.creater,a.addtime,c.code as gyscode,c.name,c.status")->where(['brand_id'=>$info['id']])->find();
  207. if($data){
  208. return app_show(0,"获取成功",$data);
  209. }else{
  210. return app_show(1002,"获取失败",$data);
  211. }
  212. }
  213. }