Keepbrand.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;use think\facade\Validate;
  6. //品牌授权
  7. class Keepbrand extends Base
  8. {
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. }
  13. public function create(){
  14. $param = $this->request->only([
  15. "brand_book",
  16. "gyscode",
  17. "brand_id",
  18. "is_del"=>0,
  19. 'remark',
  20. 'long',
  21. "status",
  22. "starttime"=>date("Y-m-d H:i:s"),
  23. "endtime"=>''],"post","trim");
  24. $valud= Validate::rule([
  25. "brand_book|授权书图片"=>"max:255",
  26. "gyscode|供应商编号"=>"require|max:255",
  27. "brand_id|品牌id"=>"require|number|gt:0|unique:brand_book,brand_id^gyscode^is_del",
  28. "long|授权类型"=>"require|number|in:0,1",
  29. "status|状态"=>"require|number|in:0,1",
  30. "starttime|开始日期"=>"require|date|dateFormat:Y-m-d H:i:s",
  31. "endtime|结束日期"=>"requireIf:long,1|date|dateFormat:Y-m-d H:i:s",
  32. ]);
  33. if($valud->check($param)==false) return error_show(1004,$valud->getError());
  34. $data=[];
  35. $data['createrid']= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  36. $data['creater']= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  37. if($param['long']===1){
  38. $data['endtime']='';
  39. }
  40. $userCommon = \app\admin\common\User::getIns();
  41. $companyinfo =$userCommon->handle('getCodeAndName',['code'=>$param['gyscode']]);
  42. // var_dump($companyinfo);
  43. if(!isset($companyinfo['data']) || empty($companyinfo['data'])){
  44. return error_show(1004,'供应商信息未找到');
  45. }
  46. $data['gysname'] =$companyinfo['data'][$param['gyscode']]?:"";
  47. $info = Db::name("brand_book")->insert(array_merge($param,$data));
  48. if($info){
  49. return error_show(0,"新建成功");
  50. }else{
  51. return error_show(1002,"新建失败");
  52. }
  53. }
  54. public function list(){
  55. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) :"1";
  56. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) :"10";
  57. $where=[['a.is_del',"=",0]];
  58. $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !=="" ? trim($this->post['gyscode']):"";
  59. if($gyscode!=""){
  60. $where[]=['a.gyscode',"like","%$gyscode%"];
  61. }
  62. $name = isset($this->post['name']) && $this->post['name'] !=="" ? trim($this->post['name']):"";
  63. if($name!=""){
  64. $where[]=['b.name',"like","%$name%"];
  65. }
  66. $count = Db::name('brand_book')
  67. ->alias('a')
  68. ->where($where)->count();
  69. $total = ceil($count / $size);
  70. $page = $page >= $total ? $total : $page;
  71. $list = Db::name('brand_book')->alias('a')
  72. ->join("brand c","a.brand_id=c.id","left")
  73. ->where($where)
  74. ->page($page,$size)
  75. ->field("a.*,c.brand_name")->order("addtime desc")
  76. ->select();
  77. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  78. }
  79. public function info(){
  80. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  81. if($id==""){
  82. return error_show(1002,"参数id不能为空");
  83. }
  84. $info = Db::name("brand_book")->where(["id"=>$id])->find();
  85. if(!$info) {
  86. return error_show(1002, "未找到对应数据");
  87. }
  88. $data = Db::name("brand")->alias('a')
  89. ->join("brand_book b","b.brand_id=a.id")
  90. ->field("b.*,a.brand_name")
  91. ->where(["a.id"=>$info['brand_id']])->find();
  92. if($data){
  93. return app_show(0,"获取成功",$data);
  94. }else{
  95. return app_show(1002,"获取失败",$data);
  96. }
  97. }
  98. public function status(){
  99. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  100. if($id==""){
  101. return error_show(1002,"参数id不能为空");
  102. }
  103. $info = Db::name("brand_book")->where([["id","=",$id]])->find();
  104. if(!$info){
  105. return error_show(1002,"未找到对应数据");
  106. }
  107. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  108. if($status===""){
  109. return error_show(1002,"参数status不能为空");
  110. }
  111. if(!in_array($status,[0,1])){
  112. return error_show(1002,"参数status无效");
  113. }
  114. $info['status']=$status;
  115. $info['updatetime']=date("Y-m-d H:i:s");
  116. $msg = $status==1?"启用":"禁用";
  117. $update = Db::name("brand_book")->save($info);
  118. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  119. }
  120. public function del(){
  121. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  122. if($id==""){
  123. return error_show(1004,"参数id不能为空");
  124. }
  125. $str= Db::name('brand_book')->where(['id'=>$id,'is_del'=>0])->find();
  126. if(empty($str)){
  127. return error_show(1002,"未找到数据");
  128. }
  129. $end = Db::name('brand_book')->update(['id'=>$id,'is_del'=>1]);
  130. if($end){
  131. return error_show(0,"删除成功");
  132. }else{
  133. return error_show(1002,"删除失败");
  134. }
  135. }
  136. public function edit(){
  137. $param = $this->request->only([
  138. "id",
  139. 'brand_book',
  140. 'gyscode',
  141. 'brand_id',
  142. 'is_del'=>0,
  143. 'remark',
  144. 'long',
  145. 'status',
  146. 'starttime'=>date('Y-m-d H:i:s'),
  147. 'endtime'=>null],'post','trim');
  148. $valud= Validate::rule([
  149. 'id|主键id'=>'require|number|gt:0',
  150. 'brand_book|授权书图片'=>'url',
  151. 'gyscode|供应商编号'=>'require|max:255',
  152. 'brand_id|品牌id'=>'require|number|gt:0|unique:brand_book,brand_id^gyscode^is_del',
  153. 'long|授权类型'=>'require|number|in:0,1',
  154. 'status|状态'=>'require|number|in:0,1',
  155. 'starttime|开始日期'=>'require|date|dateFormat:Y-m-d H:i:s',
  156. 'endtime|结束日期'=>'requireIf:long,1|date|dateFormat:Y-m-d H:i:s',
  157. ]);
  158. if($valud->check($param)==false) return error_show(1004,$valud->getError());
  159. $info = Db::name('brand_book')->where(["id"=>$param['id'],"is_del"=>0])->findOrEmpty();
  160. if(empty($info))return error_show(1004,"未找到数据");
  161. if($param['long']!=$info['long']&&$param['long']===1){
  162. $data['endtime']='';
  163. }
  164. if($param['gyscode']!=$info['gyscode']){
  165. $userCommon = \app\admin\common\User::getIns();
  166. $companyinfo =$userCommon->handle('getCodeAndName',['code'=>$param['gyscode']]);
  167. if(!isset($companyinfo['data']) || empty($companyinfo['data'])){
  168. return error_show(1004,'供应商信息未找到');
  169. }
  170. $data['gysname'] =$companyinfo['data'][$param['gyscode']]?:'';
  171. }
  172. $info = Db::name('brand_book')->save(array_merge($param,$data));
  173. if($info){
  174. return error_show(0,'更新成功');
  175. }else{
  176. return error_show(1002,'更新失败');
  177. }
  178. }
  179. public function query(){
  180. $param =$this->request->only(["brand_id"=>"","gyscode"=>"","is_del"=>0],"post","trim");
  181. $valid = Validate::rule(["brand_id|品牌id"=>"require|number|gt:0","gyscode|供应商编号"=>"require"]);
  182. if($valid->check($param)==false)return error_show(1002,$valid->getError());
  183. $info = Db::name('brand_book')->where($param)->findOrEmpty();
  184. return app_show(0,"获取成功",$info);
  185. }
  186. }