Brand.php 9.9 KB

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