Brand.php 9.0 KB

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