DatumUrl.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use app\common\model\DatumIntro;
  4. use app\common\model\PlatformSwitch;
  5. use app\common\model\SupplierGoods;
  6. use app\common\model\User;
  7. use library\Controller;
  8. use app\common\model\DatumCate;
  9. use function AlibabaCloud\Client\value;
  10. /**
  11. * 资料管理
  12. * Class OneDatum
  13. * @package app\Nutrition\controller
  14. */
  15. class DatumUrl extends Controller
  16. {
  17. /**
  18. * 绑定数据表
  19. * @var string
  20. */
  21. protected $table = 'DatumUrl';
  22. /**
  23. * 列表
  24. * @auth true
  25. * @menu true
  26. * @throws \think\Exception
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. * @throws \think\exception\PDOException
  31. */
  32. public function index()
  33. {
  34. $this->title = '资料列表';
  35. $this->datum_id = input('datum_id');
  36. $sel_where = [];
  37. $sel_where[] = ['is_deleted','=',0];
  38. $sel_where[] = ['datum_id','=',$this->datum_id];
  39. $query = $this->_query($this->table);
  40. $query->where($sel_where)->order('sort desc,id desc')->page(false);
  41. }
  42. /**
  43. * 数据列表处理
  44. * @auth true
  45. * @menu true
  46. * @param array $data
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. protected function _index_page_filter(&$data)
  52. {
  53. }
  54. /**
  55. * 添加
  56. * @auth true
  57. * @menu true
  58. * @throws \think\Exception
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. * @throws \think\exception\DbException
  62. * @throws \think\exception\PDOException
  63. */
  64. public function add()
  65. {
  66. $this->title = '添加资料';
  67. $this->_form($this->table, 'form');
  68. }
  69. /**
  70. * 编辑
  71. * @auth true
  72. * @menu true
  73. * @throws \think\Exception
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. * @throws \think\exception\DbException
  77. * @throws \think\exception\PDOException
  78. */
  79. public function edit()
  80. {
  81. $this->title = '编辑资料';
  82. $this->_form($this->table, 'form') ;
  83. }
  84. /**
  85. * 删除资料
  86. * @auth true
  87. * @menu true
  88. * @throws \think\Exception
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @throws \think\exception\DbException
  92. * @throws \think\exception\PDOException
  93. */
  94. public function del()
  95. {
  96. $datum_id = \app\common\model\DatumUrl::where('id',input('id'))->value('datum_id');
  97. DatumIntro::where('id',$datum_id)->setDec('url_num');
  98. \app\common\model\DatumUrl::where('id',input('id'))->update(['is_deleted'=>1]);
  99. \app\common\model\DatumUrl::esAdd(input('id'));
  100. \app\common\model\TopSearch::saveData(input('id'),'datum');
  101. $this->success('删除成功!');
  102. }
  103. /**
  104. * 批量删除
  105. * @auth true
  106. * @menu true
  107. * @throws \think\Exception
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. * @throws \think\exception\DbException
  111. * @throws \think\exception\PDOException
  112. */
  113. public function remove()
  114. {
  115. $ids = input('id');
  116. foreach (explode(',',$ids) as $id) {
  117. $datum_id = \app\common\model\DatumUrl::where('id',$id)->value('datum_id');
  118. DatumIntro::where('id',$datum_id)->setDec('url_num');
  119. \app\common\model\DatumUrl::where('id',$id)->update(['is_deleted'=>1]);
  120. \app\common\model\DatumUrl::esAdd($id);
  121. \app\common\model\TopSearch::saveData($id,'datum');
  122. }
  123. $this->success('删除成功!');
  124. }
  125. /**
  126. * 表单数据处理
  127. * @auth true
  128. * @menu true
  129. * @param array $data
  130. */
  131. protected function _form_filter(&$data)
  132. {
  133. if($this->request->isGet()){
  134. $this->datum_id = input('datum_id');
  135. $this->datum_info = DatumIntro::where('id',$this->datum_id)->find()->toArray();
  136. $this->cate_name = DatumCate::where('id',$this->datum_info['datum_cate'])->value('title');
  137. // 视频
  138. $this->video_list = \app\common\model\VideoIntro::with('videoArr')
  139. ->where(['is_deleted'=>0])->order('id desc')
  140. ->select()->toArray();
  141. $this->supplier_goods = SupplierGoods::getSupplierGoodsList($this->request->action() == 'add' ? ['s.is_deleted'=>0]: []);
  142. }
  143. if($this->request->isPost()){
  144. list($post) = [$this->request->post()];
  145. if(empty($post['url'])) $this->error('请上传文件');
  146. if(!$post['is_encrypt'] && !strstr($post['url'],'https')) $this->error('请重新上传文件');
  147. if($post['is_encrypt'] && strstr($post['url'],'https')) $this->error('请重新上传文件');
  148. if(!$data['release_time']) $data['release_time'] = date("Y-m-d H:i:s");
  149. if($data['hot_num'] != $data['hot_num_old']) $data['hot_time'] = date("Y-m-d H:i:s");
  150. //定时热搜
  151. if(!$post['hot_num']){
  152. $post['hot_num'] = 0;
  153. }
  154. if(isset($post['id'])){
  155. $info = \app\common\model\DatumUrl::where('id',$data['id'])->find();
  156. if(($post['regular_hot_end_time'] && $post['hot_target_num'] && $info['regular_hot_end_time'] != $post['regular_hot_end_time']) || ($info['hot_num'] != $post['hot_num'] && $post['regular_hot_end_time'] && $post['hot_target_num'])){
  157. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  158. $startdate = strtotime($data['regular_hot_start_time']);
  159. $enddate = strtotime($post['regular_hot_end_time']);
  160. $diff_seconds = ($enddate-$startdate)/60;
  161. $min_num = ceil($diff_seconds/10);
  162. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  163. $num = ceil($hot_num/$min_num);
  164. if($num < 0){
  165. $num = 0;
  166. }
  167. $data['regular_num'] = $num;
  168. }
  169. }else{
  170. if($post['regular_hot_end_time'] && $post['hot_target_num']){
  171. $data['regular_hot_start_time'] = date("Y-m-d H:i:s");
  172. $startdate = strtotime($data['regular_hot_start_time']);
  173. $enddate = strtotime($post['regular_hot_end_time']);
  174. $diff_seconds = ($enddate-$startdate)/60;
  175. $min_num = ceil($diff_seconds/10);
  176. $hot_num = $post['hot_target_num'] - $post['hot_num'];
  177. $num = ceil($hot_num/$min_num);
  178. if($num < 0){
  179. $num = 0;
  180. }
  181. $data['regular_num'] = $num;
  182. }
  183. }
  184. //定时热搜end
  185. if(!empty($post['phone'])) {
  186. $user_id = User::where('phone|email',$post['phone'])->value('id');
  187. if(!$user_id) $this->error('账号未注册');
  188. $data['user_id'] = $user_id;
  189. }else{
  190. $data['user_id'] = '';
  191. }
  192. }
  193. }
  194. protected function _form_result($result)
  195. {
  196. $url_num = \app\common\model\DatumUrl::where(['datum_id'=>$this->request->post('datum_id'),'is_deleted'=>0])->count();
  197. DatumIntro::where('id',$this->request->post('datum_id'))->update(['url_num'=>$url_num]);
  198. \app\common\model\DatumUrl::esAdd($result);
  199. \app\common\model\TopSearch::saveData($result,'datum');
  200. if($this->request->action() == 'add'){
  201. $datum_info = DatumIntro::where('id',$this->request->post('datum_id'))->find()->toArray();
  202. PlatformSwitch::followMsg(4,$this->request->post('datum_id'),$datum_info['title'],$this->request->post('title'),$result);
  203. }
  204. $this->success('保存成功');
  205. }
  206. /**
  207. * 禁用
  208. * @auth true
  209. * @menu true
  210. * @throws \think\Exception
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\ModelNotFoundException
  213. * @throws \think\exception\DbException
  214. * @throws \think\exception\PDOException
  215. */
  216. public function forbidden()
  217. {
  218. \app\common\model\DatumUrl::where('id',input('id'))->update(['status'=>0]);
  219. \app\common\model\DatumUrl::esAdd(input('id'));
  220. \app\common\model\TopSearch::saveData(input('id'),'datum');
  221. $this->success('已禁用!');
  222. }
  223. /**
  224. * 启用
  225. * @auth true
  226. * @menu true
  227. * @throws \think\Exception
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\ModelNotFoundException
  230. * @throws \think\exception\DbException
  231. * @throws \think\exception\PDOException
  232. */
  233. public function enable()
  234. {
  235. \app\common\model\DatumUrl::where('id',input('id'))->update(['status'=>1]);
  236. \app\common\model\DatumUrl::esAdd(input('id'));
  237. \app\common\model\TopSearch::saveData(input('id'),'datum');
  238. $this->success('已启用!');
  239. }
  240. }