Act.php 8.1 KB

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