TestController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Model\DispatchSub;
  4. use App\Model\Equipment;
  5. use App\Model\SystemL;
  6. use App\Service\Box\BoxService;
  7. use App\Service\ProductionOrderService;
  8. use Illuminate\Support\Facades\Config;
  9. use Illuminate\Support\Facades\DB;
  10. class TestController extends BaseController
  11. {
  12. public function delTestQ(){
  13. (new BoxService())->delBoxLock();
  14. dd(1);
  15. }
  16. public function delTestQ2(){
  17. (new ProductionOrderService())->delBoxLock();
  18. dd(1);
  19. }
  20. public function tt(){dd(1);
  21. }
  22. public function test(){dd(1);
  23. $dispatch = DispatchSub::where('del_time',0)
  24. ->where('finished_num', '>',0)
  25. ->where('process_id',14)
  26. ->select('finished_num','dispatch_time_start as upd_time','device_id')
  27. ->orderBy('dispatch_time_start','asc')
  28. ->get()->toArray();
  29. foreach ($dispatch as $key => $value){
  30. if(! $value['device_id']) $dispatch[$key]['device_id'] = 12;
  31. }
  32. // 生成数据
  33. $generatedData = (new FeederDataGenerator)->generateData($dispatch);
  34. dd("ok");
  35. }
  36. }
  37. class FeederDataGenerator
  38. {
  39. // 设备配置参数
  40. const CAPACITY = 0.6; // 容器容量(吨)
  41. const SUCTION_CAPACITY = 0.1; // 容器容量(吨) 下料
  42. const SUCTION_WEIGHT = 0.1; // 每次吸料重量(吨)
  43. const SUCTION_TIME_MIN = 300; // 吸料最短时间(秒) - 5分钟
  44. const SUCTION_TIME_MAX = 360; // 吸料最长时间(秒) - 6分钟
  45. const SUCTION_INTERVAL_MIN = 300; // 下次吸料最小间隔(秒) -
  46. const SUCTION_INTERVAL_MAX = 360; // 下次吸料最大间隔(秒) -
  47. const DISCHARGE_TIME_MIN = 240; // 下料最短时间(秒) - 4分钟
  48. const DISCHARGE_TIME_MAX = 300; // 下料最长时间(秒) - 5分钟
  49. const DISCHARGE_INTERVAL_MIN = 300; // 下次下料最小间隔(秒) -
  50. const DISCHARGE_INTERVAL_MAX = 360; // 下次下料最大间隔(秒) -
  51. private $devices = []; // 存储设备数据
  52. /**
  53. * 处理原始数据并生成设备运行明细
  54. * @param array $rawData 原始数据源
  55. * @return array 生成的设备运行明细数据
  56. */
  57. public function generateData(array $rawData): array
  58. {
  59. // 首先按设备和日期分组汇总产量
  60. $this->processRawData($rawData);
  61. dd($this->devices);
  62. // 为每个设备生成运行明细
  63. $result = [];
  64. foreach ($this->devices as $deviceId => $deviceData) {
  65. // 为每一天生成数据
  66. foreach ($deviceData['daily_totals'] as $date => $total) {
  67. $details = $this->generateDeviceDayData($deviceData['device'], $date, $total);
  68. $result = array_merge($result, $details);
  69. }
  70. }
  71. try {
  72. DB::beginTransaction();
  73. if(! empty($result)){
  74. $chunkSize = 200; // 每次插入的数量
  75. foreach (array_chunk($result, $chunkSize) as $chunk) {
  76. SystemL::insert($chunk); // 使用模型的 insert 方法批量插入
  77. echo '200条写入成功' . "\n";
  78. }
  79. }
  80. DB::commit();
  81. }catch (\Throwable $exception){
  82. DB::rollBack();
  83. return [false, $exception->getMessage()];
  84. }
  85. return [true, ''];
  86. }
  87. /**
  88. * 处理原始数据,按设备和日期汇总产量
  89. * @param array $rawData 原始数据源
  90. */
  91. private function processRawData(array $rawData)
  92. {
  93. $map = Equipment::select('id','code','title')->get()->toArray();
  94. foreach ($map as $value){
  95. $map1[$value['id']] = $value;
  96. }
  97. foreach ($rawData as $item) {
  98. $deviceId = $item['device_id'];
  99. $t = $map1[$deviceId];
  100. $timestamp = $item['upd_time'];
  101. $finishedNum = floatval($item['finished_num']);
  102. // 解析日期
  103. $date = date('Y-m-d', $timestamp);
  104. // 初始化设备数据
  105. if (!isset($this->devices[$deviceId])) {
  106. $this->devices[$deviceId] = [
  107. 'device' => $t,
  108. 'total_production' => 0,
  109. 'daily_totals' => []
  110. ];
  111. }
  112. // 累加总产量和每日产量
  113. $this->devices[$deviceId]['total_production'] += $finishedNum;
  114. if (!isset($this->devices[$deviceId]['daily_totals'][$date])) {
  115. $this->devices[$deviceId]['daily_totals'][$date] = 0;
  116. }
  117. $this->devices[$deviceId]['daily_totals'][$date] += $finishedNum;
  118. }
  119. }
  120. /**
  121. * 为单个设备的一天生成详细的运行数据
  122. * @param int $deviceId 设备ID
  123. * @param string $date 日期(格式:YYYY-MM-DD)
  124. * @param float $totalProduction 当天总产量
  125. * @return array 生成的详细数据条目
  126. */
  127. private function generateDeviceDayData(array $device, string $date, float $totalProduction): array
  128. {
  129. $result = [];
  130. // 计算当天需要完成多少轮完整的吸料-下料循环
  131. $fullCycles = floor($totalProduction / self::SUCTION_CAPACITY);
  132. $filledAmount = $fullCycles * self::SUCTION_CAPACITY;
  133. $remainingWeight = bcsub($totalProduction, $filledAmount,2);
  134. // 设置当天的起始时间戳(早上7:30)
  135. $currentTime = strtotime($date . ' 07:30:00');
  136. // 执行完整循环
  137. for ($i = 0; $i < $fullCycles; $i++) {
  138. // 执行一次完整的吸料-下料过程
  139. $cycleData = $this->generateFullCycle($device, $currentTime);
  140. $result = array_merge($result, $cycleData);
  141. // 更新当前时间
  142. $currentTime = end($cycleData)['time'];
  143. // // 添加随机间隔时间
  144. // $interval = rand(self::SUCTION_INTERVAL_MIN, self::SUCTION_INTERVAL_MAX);
  145. // $currentTime += $interval;
  146. }
  147. // 如果有剩余量,执行部分吸料过程
  148. if ($remainingWeight > 0) {
  149. $lastKey = array_key_last($result); // 获取最后一个键
  150. $result[$lastKey]['value'] += $remainingWeight;
  151. // $remainingCyleData = $this->generatePartialCycle($device, $currentTime, $remainingWeight);
  152. // $result = array_merge($result, $remainingCyleData);
  153. }
  154. return $result;
  155. }
  156. /**
  157. * 随机插入急停事件,并模拟急停持续时间
  158. *
  159. * @param int $deviceId 设备ID
  160. * @param int &$currentTime 当前时间戳(引用传递,用于更新)
  161. * @param array &$result 结果数组(引用传递,用于添加急停记录)
  162. */
  163. private function maybeInsertEmergencyStop(array $device, int &$currentTime, array &$result, $data_point_id, $data_point_name): void
  164. {
  165. if (rand(1, 100) <= 3) { // 3% 的概率触发急停
  166. $emergencyStopTime = $currentTime;
  167. // 插入急停开始事件
  168. $result[] = [
  169. 'time' => $emergencyStopTime,
  170. 'value' => -1, // 标识为急停
  171. 'device_no' => $device['code'],
  172. 'device_name' => $device['title'],
  173. 'data_point_id' => $data_point_id,
  174. 'data_point_name' => $data_point_name,
  175. 'push_time' => $emergencyStopTime,
  176. // 'push_time_day' => date("Y-m-d H:i:s",$suctionStartTime)
  177. ];
  178. // 模拟急停持续时间(10~15秒)
  179. $stopDuration = rand(10, 15);
  180. $currentTime += $stopDuration;
  181. // 可选:插入急停结束事件(也可以不插,只用时间推移表示)
  182. // $result[] = [
  183. // 'time' => $currentTime,
  184. // 'value' => -2, // 急停恢复
  185. // 'device_no' => $deviceId,
  186. // 'push_time' => $currentTime
  187. // ];
  188. }
  189. }
  190. /**
  191. * 生成一个完整的吸料-下料循环数据
  192. * @param int $deviceId 设备ID
  193. * @param int $startTime 起始时间戳
  194. * @return array 生成的数据条目
  195. */
  196. private function generateFullCycle(array $device, int $startTime): array
  197. {
  198. $result = [];
  199. $currentTime = $startTime;
  200. // 吸料阶段
  201. for ($i = 0; $i < 1; $i++) {
  202. // 【吸料前】尝试插入急停
  203. $this->maybeInsertEmergencyStop($device, $currentTime, $result,5,"吸料急停");
  204. // 吸料开始
  205. $suctionStartTime = $currentTime;
  206. $result[] = [
  207. 'time' => $suctionStartTime,
  208. 'value' => 1,
  209. 'device_no' => $device['code'],
  210. 'device_name' => $device['title'],
  211. 'data_point_id' => 1,
  212. 'data_point_name' => "吸料",
  213. 'push_time' => $suctionStartTime,
  214. // 'push_time_day' => date("Y-m-d H:i:s",$suctionStartTime)
  215. ];
  216. // 吸料结束
  217. $suctionDuration = rand(self::SUCTION_TIME_MIN, self::SUCTION_TIME_MAX);
  218. $currentTime += $suctionDuration;
  219. // 【吸料后】尝试插入急停
  220. $this->maybeInsertEmergencyStop($device, $currentTime, $result,5,"吸料急停");
  221. $result[] = [
  222. 'time' => $currentTime,
  223. 'value' => self::SUCTION_WEIGHT,
  224. 'device_no' => $device['code'],
  225. 'device_name' => $device['title'],
  226. 'data_point_id' => 2,
  227. 'data_point_name' => "吸料结束且本次吸料重量",
  228. 'push_time' => $currentTime,
  229. // 'push_time_day' => date("Y-m-d H:i:s",$currentTime)
  230. ];
  231. // 添加随机间隔时间
  232. if ($i < 1) { // 最后一次吸料后不需要等待
  233. // $interval = rand(self::SUCTION_INTERVAL_MIN, self::SUCTION_INTERVAL_MAX);
  234. $interval = rand(10, 15);
  235. $currentTime += $interval;
  236. }
  237. }
  238. // 【下料前】尝试插入急停
  239. $this->maybeInsertEmergencyStop($device, $currentTime, $result,6,"下料急停");
  240. // 下料阶段
  241. $dischargeStartTime = $currentTime;
  242. $result[] = [
  243. 'time' => $dischargeStartTime,
  244. 'value' => 1,
  245. 'device_no' => $device['code'],
  246. 'device_name' => $device['title'],
  247. 'data_point_id' => 3,
  248. 'data_point_name' => "下料",
  249. 'push_time' => $dischargeStartTime,
  250. // 'push_time_day' => date("Y-m-d H:i:s",$dischargeStartTime)
  251. ];
  252. $dischargeDuration = rand(self::DISCHARGE_TIME_MIN, self::DISCHARGE_TIME_MAX);
  253. $currentTime += $dischargeDuration;
  254. // 【下料后】尝试插入急停
  255. $this->maybeInsertEmergencyStop($device, $currentTime, $result,6,"下料急停");
  256. $result[] = [
  257. 'time' => $currentTime,
  258. 'value' => self::SUCTION_CAPACITY,
  259. 'device_no' => $device['code'],
  260. 'device_name' => $device['title'],
  261. 'data_point_id' => 4,
  262. 'data_point_name' => "下料结束且本次下料重量",
  263. 'push_time' => $currentTime,
  264. // 'push_time_day' => date("Y-m-d H:i:s",$currentTime)
  265. ];
  266. return $result;
  267. }
  268. /**
  269. * 生成一个不完整的循环(只有吸料部分)
  270. * @param int $deviceId 设备ID
  271. * @param int $startTime 起始时间戳
  272. * @param float $requiredWeight 需要吸的重量
  273. * @return array 生成的数据条目
  274. */
  275. private function generatePartialCycle(array $device, int $startTime, float $requiredWeight): array
  276. {
  277. $result = [];
  278. $currentTime = $startTime;
  279. // 计算需要多少次吸料
  280. $suctionsNeeded = ceil($requiredWeight / self::SUCTION_WEIGHT);
  281. $totalSuctionWeight = 0;
  282. for ($i = 0; $i < $suctionsNeeded; $i++) {
  283. // 吸料开始
  284. $suctionStartTime = $currentTime;
  285. $result[] = [
  286. 'time' => $suctionStartTime,
  287. 'value' => 1,
  288. 'device_no' => $device['code'],
  289. 'device_name' => $device['title'],
  290. 'data_point_id' => 1,
  291. 'data_point_name' => "吸料",
  292. 'push_time' => $suctionStartTime,
  293. // 'push_time_day' => => date("Y-m-d H:i:s",$suctionStartTime)
  294. ];
  295. // 吸料结束
  296. $suctionDuration = rand(self::SUCTION_TIME_MIN, self::SUCTION_TIME_MAX);
  297. $currentTime += $suctionDuration;
  298. // 判断这次吸料是否会导致超过所需总量
  299. $addedWeight = ($i == $suctionsNeeded - 1) ? $requiredWeight - $totalSuctionWeight : self::SUCTION_WEIGHT;
  300. $totalSuctionWeight += $addedWeight;
  301. $result[] = [
  302. 'time' => $currentTime,
  303. 'value' => $addedWeight,
  304. 'device_no' => $device['code'],
  305. 'device_name' => $device['title'],
  306. 'data_point_id' => 2,
  307. 'data_point_name' => "吸料结束且本次吸料重量",
  308. 'push_time' => $currentTime,
  309. // 'push_time_day' => => date("Y-m-d H:i:s",$currentTime)
  310. ];
  311. // 添加随机间隔时间(在40-50分钟之间)
  312. if ($i < $suctionsNeeded - 1) {
  313. $interval = rand(self::SUCTION_INTERVAL_MIN, self::SUCTION_INTERVAL_MAX);
  314. $currentTime += $interval;
  315. }
  316. }
  317. return $result;
  318. }
  319. }