Keepbrand.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $info['brand_name'] = Db::name("brand")->where(["id"=>$info['brand_id']])->value("brand_name",'');
  89. return app_show(0,"获取成功",$info);
  90. }
  91. public function status(){
  92. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  93. if($id==""){
  94. return error_show(1002,"参数id不能为空");
  95. }
  96. $info = Db::name("brand_book")->where([["id","=",$id]])->find();
  97. if(!$info){
  98. return error_show(1002,"未找到对应数据");
  99. }
  100. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  101. if($status===""){
  102. return error_show(1002,"参数status不能为空");
  103. }
  104. if(!in_array($status,[0,1])){
  105. return error_show(1002,"参数status无效");
  106. }
  107. $info['status']=$status;
  108. $info['updatetime']=date("Y-m-d H:i:s");
  109. $msg = $status==1?"启用":"禁用";
  110. $update = Db::name("brand_book")->save($info);
  111. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  112. }
  113. public function del(){
  114. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  115. if($id==""){
  116. return error_show(1004,"参数id不能为空");
  117. }
  118. $str= Db::name('brand_book')->where(['id'=>$id,'is_del'=>0])->find();
  119. if(empty($str)){
  120. return error_show(1002,"未找到数据");
  121. }
  122. $end = Db::name('brand_book')->update(['id'=>$id,'is_del'=>1]);
  123. if($end){
  124. return error_show(0,"删除成功");
  125. }else{
  126. return error_show(1002,"删除失败");
  127. }
  128. }
  129. public function edit(){
  130. $param = $this->request->only([
  131. "id",
  132. 'brand_book',
  133. 'gyscode',
  134. 'brand_id',
  135. 'is_del'=>0,
  136. 'remark',
  137. 'long',
  138. 'status',
  139. 'starttime'=>date('Y-m-d H:i:s'),
  140. 'endtime'=>null],'post','trim');
  141. $valud= Validate::rule([
  142. 'id|主键id'=>'require|number|gt:0',
  143. 'brand_book|授权书图片'=>'url',
  144. 'gyscode|供应商编号'=>'require|max:255',
  145. 'brand_id|品牌id'=>'require|number|gt:0|unique:brand_book,brand_id^gyscode^is_del',
  146. 'long|授权类型'=>'require|number|in:0,1',
  147. 'status|状态'=>'require|number|in:0,1',
  148. 'starttime|开始日期'=>'require|date|dateFormat:Y-m-d H:i:s',
  149. 'endtime|结束日期'=>'requireIf:long,1|date|dateFormat:Y-m-d H:i:s',
  150. ]);
  151. if($valud->check($param)==false) return error_show(1004,$valud->getError());
  152. $info = Db::name('brand_book')->where(["id"=>$param['id'],"is_del"=>0])->findOrEmpty();
  153. if(empty($info))return error_show(1004,"未找到数据");
  154. if($param['long']!=$info['long']&&$param['long']===1){
  155. $data['endtime']='';
  156. }
  157. if($param['gyscode']!=$info['gyscode']){
  158. $userCommon = \app\admin\common\User::getIns();
  159. $companyinfo =$userCommon->handle('getCodeAndName',['code'=>$param['gyscode']]);
  160. if(!isset($companyinfo['data']) || empty($companyinfo['data'])){
  161. return error_show(1004,'供应商信息未找到');
  162. }
  163. $data['gysname'] =$companyinfo['data'][$param['gyscode']]?:'';
  164. }
  165. $info = Db::name('brand_book')->save(array_merge($param,$data));
  166. if($info){
  167. return error_show(0,'更新成功');
  168. }else{
  169. return error_show(1002,'更新失败');
  170. }
  171. }
  172. public function query(){
  173. $param =$this->request->only(["brand_id"=>"","gyscode"=>"","is_del"=>0],"post","trim");
  174. $valid = Validate::rule(["brand_id|品牌id"=>"require|number|gt:0","gyscode|供应商编号"=>"require"]);
  175. if($valid->check($param)==false)return error_show(1002,$valid->getError());
  176. $info = Db::name('brand_book')->where($param)->findOrEmpty();
  177. return app_show(0,"获取成功",$info);
  178. }
  179. }