BusinessSchool.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\controller\merchant\business;
  3. use crmeb\basic\BaseController;
  4. use FormBuilder\Factory\Elm;
  5. use think\facade\Db;
  6. use think\facade\Route;
  7. class BusinessSchool extends BaseController
  8. {
  9. public function classify_list(){
  10. // $list = Db::name("business_school_classify")->where('mer_id',$this->request->merId())->where('is_del',0)->order("sort desc,id desc")-> select() -> toArray();
  11. $list = Db::name("business_school_classify")->where('is_del',0)->order("sort desc,id desc")-> select() -> toArray();
  12. return app("json") -> success($list);
  13. }
  14. public function classify_del(){
  15. $id=$this->request->params(['id']);
  16. // $count=Db::name('business_school')->where('mer_id',$this->request->merId())->where('classify',$id['id'])->where('is_del',0)->count();
  17. $count=Db::name('business_school')->where('classify',$id['id'])->where('is_del',0)->count();
  18. if($count){
  19. return app('json')->fail('该分类下有数据,不能直接删除~');
  20. }
  21. // Db::name('business_school_classify')->where('mer_id',$this->request->merId())->where('id',$id['id'])->update(['is_del'=>1]);
  22. Db::name('business_school_classify')->where('id',$id['id'])->update(['is_del'=>1]);
  23. return app("json") -> success('删除成功');
  24. }
  25. public function classify_add_form(){
  26. $data['state']=1;
  27. $data['sort']=0;
  28. $form= Elm::createForm(Route::buildUrl('business_school_classify_save')->build(), [
  29. Elm::input('name', '分类名称')->required(),
  30. Elm::number('sort', '排序')->required(),
  31. Elm::switches('state', '状态')->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启'),
  32. ]);
  33. $form->setTitle('添加分类')->formData($data);
  34. $list=formToData($form);
  35. $list['data']=$data;
  36. return app('json')->success($list);
  37. }
  38. public function classify_save_form()
  39. {
  40. $id=$this->request->param('id');
  41. $data=Db::name('business_school_classify')->find($id);
  42. if(!$data){
  43. return app('json')->fail('数据不存在');
  44. }
  45. $data['state']=(int)$data['state'];
  46. $form= Elm::createForm(Route::buildUrl('business_school_classify_save', compact('id'))->build(), [
  47. Elm::input('name', '分类名称')->required(),
  48. Elm::number('sort', '排序')->required(),
  49. Elm::switches('state', '状态')->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启'),
  50. ]);
  51. $form->setTitle('修改分类')->formData($data);
  52. $list=formToData($form);
  53. $list['data']=$data;
  54. return app('json')->success($list);
  55. }
  56. public function classify_save(){
  57. $data = $this -> request -> params([
  58. 'name',
  59. 'state',
  60. 'sort',
  61. 'id'
  62. ]);
  63. if(!$data['name']){
  64. return app("json") -> fail('请输入名称');
  65. }
  66. if (mb_strlen($data['name'],'UTF-8') > 10) {
  67. return app('json')->fail('名称最多支持输入 10 个汉字');
  68. }
  69. $id = $data['id'] ?? 0;
  70. $data['mer_id']=$this->request->merId();
  71. unset($data['id']);
  72. if ($id == 0){
  73. //新增
  74. $res = Db::name("business_school_classify") -> insert($data);
  75. if ($res)return app("json") -> success("成功");
  76. return app("json") -> fail('出错');
  77. }else{
  78. //
  79. $res = Db::name("business_school_classify") -> where("id",$id) -> update($data);
  80. return app("json") -> success("成功");
  81. }
  82. }
  83. public function get_sort(){
  84. // $data=Db::name('business_school')->field('sort')->where('mer_id',$this->request->merId())->where('is_del',0)->order('sort desc,id desc')->find();
  85. $data=Db::name('business_school')->field('sort')->where('is_del',0)->order('sort desc,id desc')->find();
  86. $sort=1;
  87. if($data){
  88. $sort=$data['sort']+1;
  89. }
  90. return app("json") -> success("成功",['sort'=>$sort]);
  91. }
  92. public function lst(){
  93. [$page, $limit] = $this->getPage();
  94. $where = $this->request->params(['classify','type','state','name','date']);
  95. $query=Db::name('business_school')
  96. -> alias("sp")
  97. -> join("business_school_classify spc","sp.classify = spc.id","left")
  98. // ->where('spc.mer_id',$this->request->merId())
  99. ->where('sp.is_del',0)
  100. -> when(isset($where['type']) && $where['type']!='',function ($query) use ($where){
  101. $query -> where("sp.type",$where['type']);
  102. })
  103. -> when(isset($where['state']) && $where['state']!='',function ($query) use ($where){
  104. $query -> where("sp.state",$where['state']);
  105. })
  106. -> when(isset($where['name']) && $where['name']!='',function ($query) use ($where){
  107. $query -> whereLike("sp.name","%".$where['name']."%");
  108. })
  109. -> when(isset($where['classify']) && $where['classify']!='',function ($query) use ($where){
  110. $query -> where("sp.classify",$where['classify']);
  111. })
  112. -> when(isset($where['date']) && $where['date']!='',function ($query) use ($where){
  113. getModelTime($query, $where['date'], 'sp.create_time');
  114. })
  115. ->field("sp.*,spc.name as classify_name");
  116. $count = $query -> count();
  117. $list = $query ->order("sp.sort desc,sp.id desc")->page($page,$limit)->select()->toArray();
  118. return app('json')->success(compact("list","count"));
  119. }
  120. public function del(){
  121. $id_str=$this->request->param('id');
  122. if($id_str=='' || is_array($id_str)){
  123. return app("json") -> fail('请选择数据~');
  124. }
  125. // Db::name('business_school')->where('mer_id',$this->request->merId())->whereIn('id',explode(',',$id_str))->update(['is_del'=>1]);
  126. Db::name('business_school')->whereIn('id',explode(',',$id_str))->update(['is_del'=>1]);
  127. return app("json") -> success('删除成功');
  128. }
  129. public function get_one(){
  130. $id=$this->request->params(['id']);
  131. if(empty($id['id'])){
  132. return app('json')->fail('id不存在');
  133. }
  134. // $data=Db::name('business_school') ->where('mer_id',$this->request->merId())->where('id',$id['id'])->find();
  135. $data=Db::name('business_school')->where('id',$id['id'])->find();
  136. return app("json") -> success($data);
  137. }
  138. public function save(){
  139. $data = $this -> request -> params([
  140. 'name',
  141. 'image',
  142. 'classify',
  143. 'type',
  144. 'state',
  145. 'sort',
  146. 'mp4',
  147. 'cover',
  148. 'content',
  149. 'id'
  150. ]);
  151. if(!$data['name']){
  152. return app("json") -> fail('请输入名称');
  153. }
  154. $id = $data['id'] ?? 0;
  155. $data['mer_id']=$this->request->merId();
  156. unset($data['id']);
  157. if ($id == 0){
  158. //新增
  159. $res = Db::name("business_school") -> insert($data);
  160. if ($res)return app("json") -> success("成功");
  161. return app("json") -> fail('出错');
  162. }else{
  163. //
  164. $res = Db::name("business_school") -> where("id",$id) -> update($data);
  165. return app("json") -> success("成功");
  166. }
  167. }
  168. public function save_state(){
  169. $id_str=$this->request->param('id');
  170. $state=$this->request->param('state');
  171. if($id_str=='' || is_array($id_str)){
  172. return app("json") -> fail('请选择数据~');
  173. }
  174. $ids=explode(',',$id_str);
  175. Db::name('business_school')
  176. // ->where('mer_id',$this->request->merId())
  177. ->whereIn('id',$ids)
  178. ->update(['state'=>$state==1?0:1]);
  179. return app("json") -> success('操作成功');
  180. }
  181. public function save_share_classify(){
  182. $data = $this -> request -> params([
  183. 'name',
  184. 'sort',
  185. "id"
  186. ]);
  187. if(!$data['name']){
  188. return app("json") -> fail('请输入分类名称');
  189. }
  190. $id = $data['id'] ?? 0;
  191. unset($data['id']);
  192. $data['mer_id']=$this->request->merId();
  193. if ($id == 0 ){
  194. $res = Db::name("business_school_classify") -> insert($data);
  195. }else{
  196. // $res = Db::name("business_school_classify") ->where('mer_id',$this->request->merId()) -> where("id",$id) -> update($data);
  197. $res = Db::name("business_school_classify")->where("id",$id)->update($data);
  198. return app("json") -> success("成功");
  199. }
  200. if ($res)return app("json") -> success("成功");
  201. return app("json") -> fail('出错');
  202. }
  203. }