Act.php 8.6 KB

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