Act.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\txx\controller;
  4. use app\txx\model\ActGood;use think\facade\Validate;
  5. use app\txx\model\Act as Actm;
  6. use think\App;
  7. class Act extends Base
  8. {
  9. protected $uid=0;
  10. protected $uname='';
  11. protected $model;
  12. protected static $ActStatusCn=["无状态",'活动未开始','活动进行中',"活动已结束"];
  13. public function __construct(App $app) {
  14. parent::__construct($app);
  15. if($this->request->isCx==1){
  16. $this->uid=$this->request->uid;
  17. $this->uname=$this->request->uname;
  18. }
  19. $this->model=new Actm();
  20. }
  21. /**
  22. * 显示资源列表
  23. *
  24. * @return \think\Response
  25. */
  26. public function list()
  27. {
  28. $param = $this->request->only([
  29. "page"=>1,
  30. "size"=>15,
  31. "act_name"=>'',
  32. "actCode"=>'',
  33. "company_name"=>'',
  34. "contactor"=>'',
  35. "act_type"=>'',
  36. "start"=>'',
  37. "end"=>'',
  38. "status"=>'',
  39. "version"=>"1.0",
  40. 'act_status'=>'',
  41. ],"post",'trim');
  42. $condition=[["is_del","=",0]];
  43. if($param['act_name']!='') $condition[]=["act_name","like","%{$param['act_name']}%"];
  44. if($param['actCode']!='') $condition[]=["actCode","like","%{$param['actCode']}%"];
  45. if($param['company_name']!='') $condition[]=["company_name","like","%{$param['company_name']}%"];
  46. if($param['contactor']!='') $condition[]=["contactor","like","%{$param['contactor']}%"];
  47. if($param['act_type']!='') $condition[]=["act_type","=",$param["act_type"]];
  48. if($param['start']!='') $condition[]=["addtime",">=",$param["start"]." 00:00:00"];
  49. if($param['end']!='') $condition[]=["addtime","<=",$param["end"]." 23:59:59"];
  50. if($param['status']!='') $condition[]=["status","=",$param["status"]];
  51. if($param['version']!='') $condition[]=["version","=",$param["version"]];
  52. if($param['act_status']!='') {
  53. if($param['act_status']==1){
  54. $condition[]=["startTime",">",date("Y-m-d H:i:s")];
  55. }elseif($param['act_status']==2){
  56. $condition[]=['startTime','<',date('Y-m-d H:i:s')];
  57. $condition[]=['endTime','>',date('Y-m-d H:i:s')];
  58. }elseif($param['act_status']==3){
  59. $condition[]=['endTime','<',date('Y-m-d H:i:s')];
  60. }
  61. }
  62. $list=$this->model->where($condition)
  63. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']])
  64. ->each(function(&$item){
  65. $item['act_status']=time()< strtotime($item['startTime'])?1:(time()> strtotime($item['endTime'])?3:2);
  66. $item['act_status_cn'] = self::$ActStatusCn[$item['act_status']];
  67. });
  68. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  69. }
  70. /**
  71. * 显示创建资源表单页.
  72. *
  73. * @return \think\Response
  74. */
  75. public function create()
  76. {
  77. $param = $this->request->only([
  78. "act_name"=>"",
  79. "company_name"=>'',
  80. "contactor"=>'',
  81. "mobile"=>'',
  82. "web_url"=>'',
  83. "start"=>'',
  84. "end"=>'',
  85. "act_type"=>1,
  86. ],"post","trim");
  87. $validate = Validate::rule([
  88. 'act_name|活动名称' => 'require',
  89. 'company_name|活动公司名称' => 'require',
  90. 'contactor|联系人' => 'require|max:255',
  91. 'mobile|联系电话' => 'require',
  92. ]);
  93. if($validate->check($param)==false)$this->error($validate->getError());
  94. $actCode=makeNo("ACE");
  95. $data=[
  96. "actCode"=>$actCode,
  97. "act_name"=>$param['act_name'],
  98. "company_name"=>$param['company_name'],
  99. "contactor"=>$param['contactor'],
  100. "mobile"=>$param['mobile'],
  101. "act_type"=>$param['act_type'],
  102. "startTime"=>$param['start']==''?null : $param['start'],
  103. "endTime"=>$param['end']==''?null : $param['end'],
  104. "web_url"=>$param['web_url']??"",
  105. "status"=>0,
  106. "version"=>"2.0",
  107. "apply_id"=>$this->uid,
  108. "apply_name"=>$this->uname,
  109. "addtime"=>date("Y-m-d H:i:s"),
  110. "updatetime"=>date("Y-m-d H:i:s"),
  111. ];
  112. $add =$this->model->create($data);
  113. if($add->isEmpty())$this->error("活动添加失败");
  114. $this->success("添加成功",["actCode"=>$actCode]);
  115. }
  116. /**
  117. * 保存新建的资源
  118. *
  119. * @param \think\Request $request
  120. * @return \think\Response
  121. */
  122. public function save()
  123. {
  124. $param = $this->request->only([
  125. "act_name"=>"",
  126. "company_name"=>'',
  127. "contactor"=>'',
  128. "mobile"=>'',
  129. "web_url"=>'',
  130. "start"=>'',
  131. "end"=>'',
  132. "actCode"=>'',
  133. "act_type"=>1,
  134. ],"post","trim");
  135. $validate = Validate::rule([
  136. 'act_name|活动名称' => 'require',
  137. 'actCode|活动编号' => 'require',
  138. 'company_name|活动公司名称' => 'require',
  139. 'contactor|联系人' => 'require|max:255',
  140. 'mobile|联系电话' => 'require',
  141. ]);
  142. if($validate->check($param)==false)$this->error($validate->getError());
  143. $actm=new Actm();
  144. $info = $actm->where(["actCode"=>$param['actCode'],"is_del"=>0])->find();
  145. if($info->isEmpty())$this->error("活动不存在");
  146. if($info->status==1)$this->error("活动已发布,不能编辑");
  147. $data=[
  148. "act_name"=>$param['act_name'],
  149. "company_name"=>$param['company_name'],
  150. "contactor"=>$param['contactor'],
  151. "mobile"=>$param['mobile'],
  152. "act_type"=>$param['act_type'],
  153. "startTime"=>$param['start']==''?null : $param['start'],
  154. "endTime"=>$param['end']==''?null : $param['end'],
  155. "web_url"=>$param['web_url']??"",
  156. "updatetime"=>date("Y-m-d H:i:s"),
  157. ];
  158. $add =$info->save($data);
  159. if($add==false)$this->error('活动修改失败');
  160. $this->success('活动修改成功');
  161. }
  162. /**
  163. * 显示指定的资源
  164. *
  165. * @param int $id
  166. * @return \think\Response
  167. */
  168. public function info()
  169. {
  170. $param = $this->request->only(["actCode"=>''],"post","trim");
  171. $validate = Validate::rule([
  172. 'actCode|活动编号' => 'require',]);
  173. if($validate->check($param)==false)$this->error($validate->getError());
  174. $info = $this->model->with(["goodInfo"])->where(["actCode"=>$param['actCode'],"is_del"=>0])->findOrEmpty();
  175. if($info->isEmpty()) $this->error("活动不存在");
  176. $info['act_status']=time()< strtotime($info['startTime'])?1:(time()> strtotime($info['endTime'])?3:2);
  177. $info['goodInfo']->each(function(&$item){
  178. $item['status_cn']=ActGood::$statusCn[$item['status']]??'';
  179. });
  180. $info['statusCn'] = \app\txx\model\Act::$statusCn[$info['status']]??'';
  181. $info['act_status_cn']=self::$ActStatusCn[$info['act_status']];
  182. $this->success("获取成功",$info);
  183. }
  184. /**
  185. * 删除指定资源
  186. *
  187. * @param int $id
  188. * @return \think\Response
  189. */
  190. public function delete()
  191. {
  192. $param = $this->request->only(["actCode"=>''],"post","trim");
  193. $validate = Validate::rule([
  194. 'actCode|活动编号' => 'require',]);
  195. if($validate->check($param)==false)$this->error($validate->getError());
  196. ;
  197. $info = $this->model->where(["actCode"=>$param['actCode'],"is_del"=>0])->findOrEmpty();
  198. if($info==false) $this->error("活动不存在");
  199. $info->is_del=1;
  200. $info->save();
  201. $info->goodInfo()->update(["is_del"=>1]);
  202. $this->success("删除成功");
  203. }
  204. }