Act.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. "depart"=>"",
  81. "contactor"=>'',
  82. "mobile"=>'',
  83. "web_url"=>'',
  84. "start"=>'',
  85. "end"=>'',
  86. "settle_time"=>"",
  87. "act_type"=>1,
  88. ],"post","trim");
  89. $validate = Validate::rule([
  90. 'act_name|活动名称' => 'require',
  91. 'company_name|活动公司名称' => 'require',
  92. 'contactor|联系人' => 'require|max:255',
  93. 'mobile|联系电话' => 'require',
  94. "depart|部门"=>"require",
  95. "settle_time|结算时间"=>"require",
  96. ]);
  97. if($validate->check($param)==false)$this->error($validate->getError());
  98. $actCode=makeNo("ACE");
  99. $data=[
  100. "actCode"=>$actCode,
  101. "act_name"=>$param['act_name'],
  102. "company_name"=>$param['company_name'],
  103. "contactor"=>$param['contactor'],
  104. "mobile"=>$param['mobile'],
  105. "depart"=>$param['depart'],
  106. "act_type"=>$param['act_type'],
  107. "startTime"=>$param['start']==''?null : $param['start'],
  108. "endTime"=>$param['end']==''?null : $param['end'],
  109. "web_url"=>$param['web_url']??"",
  110. "settle_time"=>date("Y-m-d H:i:s",strtotime($param['settle_time'])),
  111. "status"=>0,
  112. "version"=>"2.0",
  113. "apply_id"=>$this->uid,
  114. "apply_name"=>$this->uname,
  115. "addtime"=>date("Y-m-d H:i:s"),
  116. "updatetime"=>date("Y-m-d H:i:s"),
  117. ];
  118. $add =$this->model->create($data);
  119. if($add->isEmpty())$this->error("活动添加失败");
  120. $this->success("添加成功",["actCode"=>$actCode]);
  121. }
  122. /**
  123. * 保存新建的资源
  124. *
  125. * @param \think\Request $request
  126. * @return \think\Response
  127. */
  128. public function save()
  129. {
  130. $param = $this->request->only([
  131. "act_name"=>"",
  132. "company_name"=>'',
  133. "contactor"=>'',
  134. "mobile"=>'',
  135. "depart"=>"",
  136. "web_url"=>'',
  137. "start"=>'',
  138. "end"=>'',
  139. "actCode"=>'',
  140. 'settle_time'=>'',
  141. "act_type"=>1,
  142. ],"post","trim");
  143. $validate = Validate::rule([
  144. 'act_name|活动名称' => 'require',
  145. 'actCode|活动编号' => 'require',
  146. 'company_name|活动公司名称' => 'require',
  147. 'contactor|联系人' => 'require|max:255',
  148. 'mobile|联系电话' => 'require',
  149. 'depart|部门'=>'require',
  150. 'settle_time|结算时间'=>'require',
  151. ]);
  152. if($validate->check($param)==false)$this->error($validate->getError());
  153. $actm=new Actm();
  154. $info = $actm->where(["actCode"=>$param['actCode'],"is_del"=>0])->find();
  155. if($info->isEmpty())$this->error("活动不存在");
  156. if($info->status==1)$this->error("活动已发布,不能编辑");
  157. $data=[
  158. "act_name"=>$param['act_name'],
  159. "company_name"=>$param['company_name'],
  160. "contactor"=>$param['contactor'],
  161. "mobile"=>$param['mobile'],
  162. "act_type"=>$param['act_type'],
  163. "depart"=>$param['depart'],
  164. "settle_time"=>date("Y-m-d H:i:s",strtotime($param['settle_time'])),
  165. "startTime"=>$param['start']==''?null : $param['start'],
  166. "endTime"=>$param['end']==''?null : $param['end'],
  167. "web_url"=>$param['web_url']??"",
  168. "updatetime"=>date("Y-m-d H:i:s"),
  169. ];
  170. $add =$info->save($data);
  171. if($add==false)$this->error('活动修改失败');
  172. $this->success('活动修改成功');
  173. }
  174. /**
  175. * 显示指定的资源
  176. *
  177. * @param int $id
  178. * @return \think\Response
  179. */
  180. public function info()
  181. {
  182. $param = $this->request->only(["actCode"=>''],"post","trim");
  183. $validate = Validate::rule([
  184. 'actCode|活动编号' => 'require',]);
  185. if($validate->check($param)==false)$this->error($validate->getError());
  186. $info = $this->model->with(["goodInfo"])->where(["actCode"=>$param['actCode'],"is_del"=>0])->findOrEmpty();
  187. if($info->isEmpty()) $this->error("活动不存在");
  188. $info['act_status']=time()< strtotime($info['startTime'])?1:(time()> strtotime($info['endTime'])?3:2);
  189. $info['goodInfo']->each(function(&$item){
  190. $item['status_cn']=ActGood::$statusCn[$item['status']]??'';
  191. });
  192. $info['statusCn'] = \app\txx\model\Act::$statusCn[$info['status']]??'';
  193. $info['act_status_cn']=self::$ActStatusCn[$info['act_status']];
  194. $this->success("获取成功",$info);
  195. }
  196. /**
  197. * 删除指定资源
  198. *
  199. * @param actCode
  200. * @return \think\Response
  201. */
  202. public function delete()
  203. {
  204. $param = $this->request->only(["actCode"=>''],"post","trim");
  205. $validate = Validate::rule([
  206. 'actCode|活动编号' => 'require',]);
  207. if($validate->check($param)==false)$this->error($validate->getError());
  208. ;
  209. $info = $this->model->where(["actCode"=>$param['actCode'],"is_del"=>0])->findOrEmpty();
  210. if($info==false) $this->error("活动不存在");
  211. $info->is_del=1;
  212. $info->save();
  213. $info->goodInfo()->update(["is_del"=>1]);
  214. $this->success("删除成功");
  215. }
  216. /**
  217. @param $actCode
  218. * @param $status
  219. */
  220. public function status(){
  221. $param = $this->request->only(['actCode'=>'','status'=>0],'post','trim');
  222. $validate = Validate::rule([
  223. 'actCode|活动编号' => 'require',
  224. 'status|状态' => 'require|in:0,1,2,3,4,5,6',
  225. ]);
  226. if($validate->check($param)==false)$this->error($validate->getError());
  227. $info = $this->model->where(['actCode'=>$param['actCode'],'is_del'=>0])->findOrEmpty();
  228. if($info==false) $this->error('活动不存在');
  229. $info->status=$param['status'];
  230. if($info->save()==false)$this->error('修改失败');
  231. $this->success('修改成功');
  232. }
  233. }