Browse Source

修改机器人充电自动模拟数据任务细节

sptkw 1 year ago
parent
commit
c589819ffa
2 changed files with 65 additions and 25 deletions
  1. 16 25
      app/Crontab/SckwChgrobotCrontab.php
  2. 49 0
      app/Model/RRobotAutoUserModel.php

+ 16 - 25
app/Crontab/SckwChgrobotCrontab.php

@@ -5,10 +5,9 @@ namespace App\Crontab;
 
 
 use App\Model\RRobotAutoOrderModel;
+use App\Model\RRobotAutoUserModel;
 use App\Model\RTradeOrderModel;
 use App\Model\TEmployeeMemberModel;
-use App\Model\TEmployeeModel;
-use Carbon\Carbon;
 use Hyperf\Contract\StdoutLoggerInterface;
 use Hyperf\Crontab\Annotation\Crontab;
 use Hyperf\DbConnection\Db;
@@ -19,7 +18,7 @@ use Hyperf\Di\Annotation\Inject;
  * Author: Spt <2864934511@qq.com>
  * Date: 2024-03-13 08:36
  */
-#[Crontab(rule: "0 *\/1 * * *", name: "SckwChgrobot", callback: "execute", memo: "自动生成订单任务每天一次", enable: "isEnable")]
+#[Crontab(rule: "30 23 * * *", name: "SckwChgrobot", callback: "execute", memo: "自动生成订单任务每天一次", enable: "isEnable")]
 class SckwChgrobotCrontab
 {
 //    /**
@@ -30,11 +29,6 @@ class SckwChgrobotCrontab
     #[Inject]
     private StdoutLoggerInterface $logger;
 
-    private static array $users = [
-        'oJdn-7JDOOCzlOgS6rE5CsXw_PbM',//pdy
-//        '',//
-    ];
-
     /**
      * 充电最低标准电量
      * @var int
@@ -61,28 +55,27 @@ class SckwChgrobotCrontab
 
     private function task(): bool
     {
-
-        //插入测试员工 - ok
-        //INSERT INTO `t_employee`(`employee_id`, `system_type`, `login_name`, `login_pwd`, `open_id`, `actual_name`, `birthday`, `phone`, `remark`) VALUES (87, 5, '18589088034', '18589088034', 'oJdn-7JDOOCzlOgS6rE5CsXw_PbM', 'pdy', '2001-02-01 14:37:37', '18589088034', '测试号,不删除');
-        //插入测试员工会员卡 - ok
-        //INSERT INTO `r_member_order`(`id`, `open_id`, `type`, `user_id`, `order_no`, `member_no`, `end_time`, `create_time`, `update_time`, `status`, `del_flag`, `member_facility_id`) VALUES (NULL, 'oJdn-7JDOOCzlOgS6rE5CsXw_PbM', 1, 87, 'M2024031316352413', '202403131437082', '2024-03-15 16:35:24', '2024-03-08 16:35:24', '2024-03-08 16:35:24', 0, 0, NULL);
-        //INSERT INTO `r_employee_member`(`open_id`, `user_id`, `member_number`, `create_time`, `update_time`, `end_time`) VALUES ('oJdn-7JDOOCzlOgS6rE5CsXw_PbM', 87,'202403131437082','2024-03-13 14:34:01','2024-03-13 14:34:03','2029-04-01 14:34:05') ;
-        //
-
-
         //获取所有任务
         $list = RRobotAutoOrderModel::with(['robot', 'site', 'price'])
             ->where('num', '<', 2)//执行次数0无限1一次超过1则不执行
             ->where('del_flag', 0)
             ->get();
         if ($list->isNotEmpty()) {
+            //获取所有可用人员
+            $user_list = RRobotAutoUserModel::where('status', 1)->get();
+            if ($user_list->isEmpty()) {
+                $this->logger->error(' 查无任务可用用户');
+                return false;
+            }
+            $users = array_column($user_list->toArray(), 'open_id');
+
             //获取所有可用人员
             $userLists = TEmployeeMemberModel::with(['user'])
-                ->whereIn('open_id', self::$users)//openid 范围查询
+                ->whereIn('open_id', $users)//openid 范围查询
                 ->where('del_flag', 0)//是否删除0否 1是
                 ->get();
             if ($userLists->isEmpty()) {
-                $this->logger->error(' 查无会员用户:' . json_encode(self::$users, JSON_UNESCAPED_UNICODE));
+                $this->logger->error(' 查无会员用户:' . json_encode($users, JSON_UNESCAPED_UNICODE));
                 return false;
             } else {
                 $userLists = $userLists->toArray();
@@ -157,7 +150,7 @@ class SckwChgrobotCrontab
                     "charge_model" => 5,//充电模式(5 自然充满、10 按电\r\n量、15 按金额)
 //                    "plan_power"=>null,//计划充电量(kwh) 非自然充满模\r\n式必填
 //                    "plan_amount"=>null,//计划充电金额,自然充满模式则\r\n为用户余额
-                    "status" => 4,//订单状态(1进行中,2已结算,3未支付(预留),4完成)
+                    "status" => 2,//订单状态(1进行中,2已结算,3未支付(预留),4完成)
 //                    "third_order_no"=>'',//三方订单号(记录 ID)
                     "duration" => 0,//充电时长(分钟)向上取整-
                     "voltage" => 220,//电压
@@ -272,7 +265,7 @@ class SckwChgrobotCrontab
 //                        //放
 //                        $efficiency_f = $total_power * $d_efficiency * 60;
 
-                        $this->logger->info(PHP_EOL . PHP_EOL . $robot->id . '下轮充放电概率确认, 充:' . $c_efficiency);
+                        $this->logger->info(PHP_EOL . $robot->id . '下轮充放电概率确认, 充:' . $c_efficiency);
 
                         $is_charge = false;
                     }
@@ -281,20 +274,18 @@ class SckwChgrobotCrontab
                 if (!empty($res_data)) {
                     Db::beginTransaction();
                     try {
-                        var_dump(json_encode($res_data, JSON_UNESCAPED_UNICODE));
-//                        $res = (new \App\Model\RTradeOrderModel)->save($res_data);
                         $t_id = 0;
                         foreach ($res_data as $da) {
                             $t_id = RTradeOrderModel::insertGetId($da);
                         }
-                        $this->logger->info(datetime() . ' ' . $robot->id . ' 生成成功: ' . ($t_id ? 'true' : 'false'));
+                        $this->logger->info(datetime() . ' ' . $robot->id . ' 生成成功: ' . ($t_id ? 'true' : 'false') . PHP_EOL . PHP_EOL);
                         if ($robot->num == 1) {
                             RRobotAutoOrderModel::where('id', $robot->id)->update(['num' => 2]);
                         }
                         Db::commit();
                     } catch (\Exception $e) {
                         Db::rollBack();
-                        $this->logger->error(datetime() . ' ' . $robot->id . '生成失败: ' . $e->getMessage());
+                        $this->logger->error(datetime() . ' ' . $robot->id . '生成失败: ' . $e->getMessage() . PHP_EOL . PHP_EOL);
                     }
                 }
             }

+ 49 - 0
app/Model/RRobotAutoUserModel.php

@@ -0,0 +1,49 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+
+/**
+ * 充电站点订单自动补充任务用户
+ */
+class RRobotAutoUserModel extends Model
+{
+    /**
+     * The connection name for the model.
+     */
+    protected ?string $connection = 'robot';
+
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'r_robot_auto_user';
+
+    public bool $timestamps = true;
+
+    /**
+     * The name of the "created at" column.
+     *
+     * @var null|string
+     */
+    public const CREATED_AT = 'create_time';
+
+    /**
+     * The name of the "updated at" column.
+     *
+     * @var null|string
+     */
+    public const UPDATED_AT = 'update_time';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+
+}