Brand.php 9.3 KB

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