Chuyutimedtask.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Cannelorder;
  4. use app\admin\model\RecoveryBill;
  5. use app\admin\model\Smslog;
  6. use app\admin\model\TablewareBill;
  7. use app\admin\model\User;
  8. use app\admin\model\Userscore;
  9. use app\common\controller\Api;
  10. use app\admin\model\Recoveryorder;
  11. use Alicode\Alisms;
  12. use app\common\library\Sms as Smslib;
  13. use fast\Random;
  14. use think\Db;
  15. /**
  16. * 定时任务
  17. */
  18. class Chuyutimedtask extends Api
  19. {
  20. // 如果$noNeedLogin为空表示所有接口都需要登录才能请求 无需登录的接口,*表示全部
  21. /* *可以通过@ApiInternal忽略请求的方法
  22. * @ApiInternal
  23. * */
  24. protected $noNeedLogin = ['*'];
  25. // 如果$noNeedRight为空表示所有接口都需要验证权限才能请求无需鉴权的接口,*表示全部
  26. protected $noNeedRight = ['*'];
  27. public function SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam)
  28. {
  29. // 循环发送
  30. $sendBatchSmsRequest = [
  31. // "templateCode" => "SMS_250975801",
  32. // "phoneNumberJson" => "['13161001120', '15550493042']",
  33. // "signNameJson" => "['乐餐宝', '乐餐宝']",
  34. // "code"=>"123456"
  35. ];
  36. $sendSmsRequest = [
  37. "TemplateCode" => $TemplateCode,
  38. "phoneNumber" => $phoneNumber,
  39. "TemplateParam" => $TemplateParam
  40. ];
  41. $res = Alisms::sendSmsCode($sendSmsRequest);
  42. // 短信发送记录
  43. $smslog_model = new Smslog();
  44. $insert_data = [
  45. 'mobile' => $phoneNumber,
  46. 'template_code' => $TemplateCode,
  47. 'template_param' => $TemplateParam,
  48. 'createtime' => time(),
  49. 'response_param' => json_encode($res),
  50. ];
  51. $smslog_model->insert($insert_data);
  52. return $res['Code'] == 'OK' ? true : false;
  53. }
  54. /**
  55. * 回收订单 - 超时未回收订单提醒
  56. */
  57. public function RecoveryTimeout()
  58. {
  59. // 读取超时时间
  60. $order_recovery_overtime = !empty(config('site.order_recovery_overtime')) ? config('site.order_recovery_overtime') : 8;
  61. $overtime = time() - $order_recovery_overtime * 3600;
  62. $recovery_order_model = new Recoveryorder();
  63. $data = [
  64. 'timeout' => 1,
  65. ];
  66. $where['status'] = 1;
  67. $where['timeout'] = 0;
  68. $where['create_time'] = ['<=', $overtime];
  69. $recovery_order_model->isUpdate(true, $where)->save($data);
  70. $this->success('ok');
  71. }
  72. public function order_sms($data){
  73. $TemplateCode = "SMS_262400702";
  74. $phoneNumber = $data['mobile'];
  75. $TemplateParam = json_encode(['number' => $data['score']]);
  76. $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam);
  77. }
  78. /**
  79. * 店铺注销退货(餐具)- 超时退货回收提醒 --发给b端配送员
  80. */
  81. public function CancelOrderTimeOut()
  82. {
  83. // 读取超时时限
  84. $cancel_order_recovery = !empty(config('site.cancel_order_recovery')) ? config('site.cancel_order_recovery') : 48;
  85. $overtime = time() - $cancel_order_recovery * 3600;
  86. $cancel_order_model = new Cannelorder();
  87. $data = [
  88. 'timeout' => 1,
  89. ];
  90. $where['status'] = 1;
  91. $where['timeout'] = 0;
  92. $where['create_time'] = ['<=', $overtime];
  93. $cancel_order_model->isUpdate(true, $where)->save($data);
  94. $this->success('ok');
  95. //状态:1=已接收,2=已清点,3=待退款,4=已退款,5=已入库--
  96. // --需求更改不要短信了
  97. // $cancel_order = $cancel_order_model->alias('o')
  98. // ->join('user u', 'o.b_user_id = u.id')
  99. // ->field('o.id,o.b_user_id,u.mobile')
  100. // ->where('o.status', '=', 1)
  101. // ->where('o.timeout', '=', 0)
  102. // ->where('o.createtime', '<=', $overtime)->select();
  103. //
  104. // if (count($cancel_order) == 0) {
  105. // $this->success('无数据');
  106. // }
  107. // $TemplateCode = "SMS_250740130";
  108. // $ids = [];
  109. // foreach ($cancel_order as $k => $row) {
  110. // $phoneNumber = $row['mobile'];
  111. // $code = "786324";
  112. // $TemplateParam = json_encode(['code' => $code]);
  113. // $is_success = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam);
  114. //
  115. // if ($is_success) {
  116. // $ids[] = $row['id'];
  117. // }
  118. // }
  119. // if (count($ids) > 0) {
  120. // $data = [
  121. // 'timeout' => 1,
  122. // ];
  123. // $where['id'] = ['in', $ids];
  124. // $cancel_order_model->isUpdate(true, $where)->save($data);
  125. //
  126. // $this->success(__('发送成功'));
  127. // }
  128. }
  129. /**
  130. * 账单未支付提醒 -- 发给配送员和回收员
  131. */
  132. public function OrderUnpaid()
  133. {
  134. // 读取超时时限
  135. $order_recovery_overtime = !empty(config('site.bill_unpaid_overtime')) ? config('site.bill_unpaid_overtime') : 360;
  136. $overtime = time() - $order_recovery_overtime * 3600;
  137. // 回收月结账单
  138. $recovery_bill_model = new RecoveryBill();
  139. $recovery_bill = $recovery_bill_model->alias('b')
  140. ->join('user u', 'b.c_user_id = u.id')
  141. ->field('b.id,b.c_user_id,u.mobile')
  142. ->where('b.status', '=', 1)
  143. ->where('b.pay_timeout', '=', 0)
  144. ->where('b.createtime', '<=', $overtime)
  145. ->select();
  146. //var_dump($recovery_bill);exit();
  147. if (count($recovery_bill) != 0) {
  148. $ids = [];
  149. // $mobiles = [];
  150. // foreach ($recovery_bill as $k => $row) {
  151. // $mobiles[] = $row['mobile'];
  152. // $ids[] = $row['id'];
  153. // }
  154. //SMS_262525727、亲爱的“餐厨宝”会员,您的${name}账单已发送至您的微信小程序支付界面,请您尽快前往确认并支付,感谢您的支持!
  155. $TemplateCode = "SMS_262525727";
  156. foreach ($recovery_bill as $k => $row) {
  157. $phoneNumber = $row['mobile'];
  158. $TemplateParam = json_encode(['name' => $row['order_no']]);
  159. $is_success = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam);
  160. if ($is_success) {
  161. $ids[] = $row['id'];
  162. }
  163. // todo add else log
  164. }
  165. // var_dump($ids);
  166. if (count($ids) > 0) {
  167. $data = [
  168. 'pay_timeout' => 1,
  169. ];
  170. $where['id'] = ['in', $ids];
  171. $recovery_bill_model->isUpdate(true, $where)->save($data);
  172. echo(__('发送成功'));
  173. }
  174. }
  175. //餐具月结账单
  176. $tableware_bill_model = new TablewareBill();
  177. $tableware_bill = $tableware_bill_model->alias('b')
  178. ->join('user u', 'b.c_user_id = u.id')
  179. ->field('b.id,b.c_user_id,u.mobile')
  180. ->where('b.status', '=', 1)
  181. ->where('b.pay_timeout', '=', 0)
  182. ->where('b.createtime', '<=', $overtime)
  183. ->select();
  184. if (count($tableware_bill) != 0) {
  185. $ids = [];
  186. // $mobiles = [];
  187. // foreach ($tableware_bill as $k => $row) {
  188. // $mobiles[] = $row['mobile'];
  189. // $ids[] = $row['id'];
  190. // }
  191. // $msg = "有餐具月结账单未支付,请及时催收";
  192. // $template = 1;
  193. // $ret = Smslib::notice($mobiles, $msg, $template);
  194. $TemplateCode = "SMS_262525727";
  195. foreach ($tableware_bill as $k => $row) {
  196. $phoneNumber = $row['mobile'];
  197. $TemplateParam = json_encode(['name' => $row['order_no']]);
  198. $is_success = $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam);
  199. if ($is_success) {
  200. $ids[] = $row['id'];
  201. }
  202. // todo add else log
  203. }
  204. if (count($ids) > 0) {
  205. $data_tableware_bill = [
  206. 'pay_timeout' => 1,
  207. ];
  208. $where_tableware_bill['id'] = ['in', $ids];
  209. $tableware_bill_model->isUpdate(true, $where_tableware_bill)->save($data_tableware_bill);
  210. echo(__('发送成功'));
  211. }
  212. }
  213. }
  214. /**
  215. * 积分清零
  216. */
  217. public function ScoreClear()
  218. {
  219. $score_clear_date = config('site.score_clear_date');
  220. if (empty($score_clear_date)) {
  221. $this->success('score_clear_date is null!');
  222. }
  223. $score_clear_int = strtotime($score_clear_date); // 函数将日期转换为时间戳;
  224. $clear_date = date('m-d', $score_clear_int);
  225. // today
  226. $now_date = date('m-d', time());
  227. if ($clear_date != $now_date) {
  228. $this->success('not clear date!');
  229. }
  230. $user_model = new User();
  231. // $user_model->where('id','>',0)->update(['score'=>0]);
  232. $users = $user_model->field('id,score,username')->where('score', '>', 0)->select();
  233. if (count($users) == 0) {
  234. $this->success('无操作数据');
  235. }
  236. $insert_list = [];
  237. $user_ids = [];
  238. foreach ($users as $v) {
  239. $insert_list[] = [
  240. 'user_id' => $v['id'],
  241. 'username' => $v['username'],
  242. 'number' => $v['score'],
  243. 'type' => 3,
  244. 'desc' => '程序清零',
  245. ];
  246. $user_ids[] = $v['id'];
  247. }
  248. Db::startTrans();
  249. try {
  250. // 添加记录
  251. $user_score_model = new Userscore();
  252. $user_score_model->saveAll($insert_list);
  253. // 清空积分
  254. $where_user_ids['id'] = ['in', $user_ids];
  255. $user_model->where('id', 'in', $user_ids)->update(['score' => 0, 'updatetime' => time()]);
  256. Db::commit();
  257. } catch (\Exception $e) {
  258. Db::rollback();
  259. $this->error($e->getMessage());
  260. }
  261. $this->success('ok');
  262. }
  263. /**
  264. * 积分过期提醒
  265. */
  266. public function ScoreOverdue()
  267. {
  268. $score_overdue_date = config('site.score_overdue_date');
  269. if (empty($score_overdue_date)) {
  270. $this->success('score_overdue_date is null!');
  271. }
  272. $score_overdue_int = strtotime($score_overdue_date); // 函数将日期转换为时间戳;
  273. $overdue_date = date('m-d', $score_overdue_int);
  274. // today
  275. $now_date = date('m-d', time());
  276. if ($overdue_date != $now_date) {
  277. $this->success('not overdue date!');
  278. }
  279. $user_model = new User();
  280. $users = $user_model->field('id,score,mobile')
  281. ->where(['status' => 'normal','cancel_shop'=>0])
  282. ->where('score', '>', 0)->select(); // 账户未注销的
  283. if (count($users) == 0) {
  284. $this->success('无操作数据');
  285. }
  286. // SMS_262585705、亲爱的会员,您的账户剩余${number}积分快到期了,您可以进入“餐厨宝”商城使用您的积分,感谢您的支持!
  287. $TemplateCode = "SMS_262585705";
  288. foreach ($users as $v) {
  289. $phoneNumber = $v['mobile'];
  290. $TemplateParam = json_encode(['number' => $v['score']]);
  291. $this->SmsTemplate($TemplateCode, $phoneNumber, $TemplateParam);
  292. }
  293. $this->success('ok');
  294. }
  295. }