Stock2.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\Admin\controller;
  3. use think\Db;
  4. class Stock2 extends Base
  5. {
  6. // public $post="";
  7. // public $userinfo="";
  8. public function __construct()
  9. {
  10. parent:: __construct();
  11. // $post=request()->post();
  12. // $this->post=$post;
  13. }
  14. public function video()
  15. {
  16. $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
  17. $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
  18. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  19. $where = ['is_del'=>0];
  20. if ($status != "") {
  21. $where ['status'] = $status;
  22. }
  23. $video_sn = isset($this->post['video_sn']) && $this->post['video_sn'] !== "" ? trim($this->post['video_sn']) : "";
  24. if ($video_sn !== "") {
  25. $where['video_sn'] = ["like", "%{$video_sn}%"];
  26. }
  27. $video_name = isset($this->post['video_name']) && $this->post['video_name'] !== "" ? trim($this->post['video_name']) : "";
  28. if ($video_name) {
  29. $where['video_name'] = ["like", "%{$video_name}%"];
  30. }
  31. $video_url = isset($this->post['video_url']) && $this->post['video_url'] !== "" ? trim($this->post['video_url']) : "";
  32. if ($video_url) {
  33. $where['video_url'] = ["like", "%{$video_url}%"];
  34. }
  35. $video_img = isset($this->post['video_img']) && $this->post['video_img'] !== "" ? trim($this->post['video_img']) : "";
  36. if ($video_img) {
  37. $where['video_img'] = ["like", "%{$video_img}%"];
  38. }
  39. $count = Db::name("video")->where($where)->count();
  40. $total = ceil($count / $size);
  41. $page = $page >= $total ? $total : $page;
  42. $video = Db::name("video")->where($where)->page($page, $size)->order("addtime desc")->select();
  43. return app_show(0, "获取成功", ["video" => $video, "count" => $count]);
  44. }
  45. public function Create()
  46. {
  47. /**
  48. * @param video_name
  49. * @param video_url
  50. * @param video_img
  51. * @param remark
  52. */
  53. $video_sn = makeNo("FC");
  54. //var_dump($this->post);
  55. $video_name = isset($this->post['video_name']) && $this->post['video_name'] !== "" ? trim($this->post['video_name']) : "";
  56. // var_dump($video_name);
  57. if ($video_name == "") {
  58. return error_show(1004, "参数video_name 不能为空");
  59. }
  60. $isN = Db::name("video")->where(["is_del" => 0, "video_name" => $video_name])->find();
  61. //var_dump(Db::name("video")->getLastSql());
  62. if (!empty($isN)) {
  63. return error_show(1004, "视频名称已存在");
  64. }
  65. $video_url = isset($this->post['video_url']) && $this->post['video_url'] !== "" ? trim($this->post['video_url']) : "";
  66. // var_dump( $video_url);
  67. if ($video_url == "") {
  68. return error_show(1004, "参数video_url 视频地址不能为空");
  69. }
  70. $video_img = isset($this->post['video_img']) && $this->post['video_img'] !== "" ? trim($this->post['video_img']) :"";
  71. //var_dump($video_img);
  72. if ($video_img == "") {
  73. return error_show(1004, "参数video_img 视频图片不能为空");
  74. }
  75. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  76. $data = [
  77. "video_sn" => $video_sn,
  78. "video_name"=>$video_name,
  79. "video_url"=>$video_url,
  80. "video_img"=>$video_img,
  81. "remark"=>$remark,
  82. "is_del" => 0,
  83. "status" => 1,
  84. "addtime" => date("Y-m-d H:i:s"),
  85. "updatetime" => date("Y-m-d H:i:s"),
  86. ];
  87. Db::startTrans();
  88. try{
  89. $video = Db::name("video")->insert($data, false, true);
  90. //var_dump( Db::name("video")->getLastSql());
  91. if ($video) {
  92. write_log("视频{$video}新建成功", $this->userinfo, "account", "add");
  93. Db::commit();
  94. return app_show(0, "视频新建成功");
  95. }
  96. Db::rollback();
  97. return error_show(1005, "视频新建失败");
  98. }catch (\Exception $e){
  99. Db::rollback();
  100. return error_show(1003,$e->getMessage());
  101. }
  102. }
  103. public function Read(){
  104. $id=isset($this->post['id'])&&$this->post["id"]!="" ? intval($this->post['id']):"";
  105. if($id==""){
  106. return error_show(1004,"参数id 不能为空");
  107. }
  108. $video_sn = db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
  109. if(empty($video_sn)){
  110. return error_show(1005,"未找到视频编号");
  111. }
  112. if($video_sn["is_del"]==1) {
  113. return error_show(1005, "视频编号已删除");
  114. }
  115. $video_sn['status_n'] = $video_sn['status']==0? "禁用视频": $video_sn['status']==1? "启用视频":"禁用视频";
  116. return app_show(0,"成功获取",$video_sn);
  117. }
  118. /**
  119. * @param video_sn
  120. * @param video_name
  121. * @param video_url
  122. * @param video_img
  123. * @param remark
  124. */
  125. public function change(){
  126. $id = isset($this->post['id'])? intval($this->post['id']) : "";
  127. // var_dump($this->post,$id);
  128. if($id==""){
  129. return error_show(1001,'参数ID 不能为空');
  130. }
  131. $act = Db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
  132. if(empty($act)){
  133. return error_show(1001,"未找到视频");
  134. }
  135. if($act['is_del']==1){
  136. return error_show(1003,'视频编号已删除');
  137. }
  138. $status = isset($this->post['status']) ? intval($this->post['status']) : "";
  139. if($status===""){
  140. return error_show(1004,"参数不能为空");
  141. }
  142. if(!in_array($status,[0,1])){
  143. return error_show(1004,"参数status无效值");
  144. }
  145. try{
  146. $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  147. $result = Db::name("video")->where(["id"=>$id])->update($data);
  148. $msg = $status ==1?"启用":"禁用";
  149. //var_dump(Db::name("video")->getLastSql());
  150. if($result){
  151. write_log("视频{$act['video_name']}功能{$act['video_name']}{$msg}成功",$this->userinfo,"status","0");
  152. return app_show(0,"更新成功");
  153. }else{
  154. return error_show(1004,"更新失败");
  155. }
  156. }catch (\Exception $e){
  157. return error_show(1003,$e->getMessage());
  158. }
  159. }
  160. /**
  161. * @param video_sn
  162. * @param video_name
  163. * @param video_url
  164. * @param video_img
  165. */
  166. public function Edit(){
  167. $id = isset($this->post['id'])&&$this->post['id']!== "" ?trim($this->post['id']):"";
  168. if($id==""){
  169. return error_show(1005,"参数id不能为空");
  170. }
  171. $in = Db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
  172. //var_dump($in);
  173. if(empty($in)){
  174. return error_show(1004,"视频不存在");
  175. }
  176. $weight = isset($this->post['weight'])&&$this->post['weight']!==""?$this->post['weight']:$in["weight"];
  177. $video_name = isset($this->post['video_name']) ? trim($this->post['video_name']) : "";
  178. if($video_name==""){
  179. return error_show(1004,"视频名称不能为空");
  180. }
  181. $video_url = isset($this->post['video_url']) ? trim($this->post['video_url']) : "";
  182. if($video_name==""){
  183. return error_show(1004,"视频地址不能为空");
  184. }
  185. $video_img = isset($this->post['video_img']) ? trim($this->post['video_img']) : "";
  186. if($video_img==""){
  187. return error_show(1004,"视频图片不能为空");
  188. }
  189. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  190. Db::startTrans();
  191. try{
  192. $data=[
  193. "id"=>$id,
  194. "video_name"=>$video_name,
  195. "video_url"=>$video_url,
  196. "video_img"=>$video_img,
  197. "updatetime"=>date("Y-m-d H:i:s"),
  198. "weight"=>$weight,
  199. "remark"=>$remark
  200. ];
  201. $video_name= Db::name("video")->update($data);
  202. // var_dump(Db::name("video")->getLastSql());
  203. if($video_name){
  204. Db::commit();
  205. return app_show(0,"视频内容编辑成功");
  206. }
  207. Db::rollback();
  208. return error_show(1008,"视频内容修改失败");
  209. }catch (\Exception $e){
  210. Db::rollback();
  211. return error_show(1008,$e->getMessage());
  212. }
  213. }
  214. public function Delete(){
  215. $id = isset($this->post['id'])&&$this->post['id']!==""? intval($this->post['id']):"";
  216. if($id==""){
  217. return error_show(1005,"参数id不能为空");
  218. }
  219. $list = Db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
  220. if(empty($list)){
  221. return error_show(1004,"未找到数据");
  222. }
  223. $del = Db::name("video")->where(["id"=>$id])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  224. // var_dump( Db::name("video")->getLastSql());
  225. if($del){
  226. return app_show(0,"视频数据修改成功");
  227. }
  228. return error_show(1005,"视频数修改失败");
  229. }
  230. }