Act.php 8.6 KB

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