Tool.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace catchAdmin\api\controller;
  3. use catchAdmin\hydraulic\model\Hydraulic;
  4. use catchAdmin\hydraulic\model\maintenancemapper;
  5. use catchAdmin\hydraulic\model\Wrench as ModelWrench;
  6. use catchAdmin\worklocation\model\WorkRecord;
  7. use catcher\base\CatchRequest as Request;
  8. use catcher\CatchResponse;
  9. use catcher\base\CatchController;
  10. use PhpParser\Node\Expr\FuncCall;
  11. use think\facade\Db;
  12. use Workerman\Worker;
  13. use Wrench;
  14. class Tool extends Base
  15. {
  16. /**
  17. * @Descripttion: 下发设备 携带的参数 imei号 版本号
  18. * @name: likang
  19. * @return {*}
  20. */
  21. public function Issued()
  22. {
  23. $param = json_decode(file_get_contents("php://input"), true);
  24. //校验imei是否存在
  25. if (!isset($param['IMEI']) || $param['IMEI'] == '') {
  26. json_fail('缺少设备IMEI号参数');
  27. }
  28. $Imei = $param['IMEI'];
  29. if (!$this->IsImei($Imei)) {
  30. json_fail('该Imei号不存在');
  31. }
  32. if (!($param['SoftVersion']) || $param['SoftVersion'] == '') {
  33. $param['SoftVersion'] = 0;
  34. }
  35. $version = $param['SoftVersion'];
  36. return $this->DataIssued($Imei, $version);
  37. }
  38. //收到的数据
  39. public function receive()
  40. {
  41. $param = json_decode(file_get_contents("php://input"), true);
  42. //校验imei是否存在
  43. $where = [];
  44. if (!isset($param['Imei']) || $param['Imei'] == '') {
  45. json_fail('缺少设备IMEI号参数');
  46. }
  47. $Imei = $param['Imei'];
  48. //校验Imei是否存在
  49. if (!$this->IsImei($Imei)) {
  50. json_fail('该Imei号不存在');
  51. }
  52. if (!isset($param['CntVersion']) || $param['CntVersion'] == '') {
  53. $param['CntVersion'] = 0;
  54. }
  55. $version = $param['CntVersion'];
  56. if (!isset($param['Succ']) || $param['Succ'] == '') {
  57. json_fail('缺少返回结果', '', $Imei);
  58. }
  59. $Succ = $param['Succ'];
  60. if (!isset($param['Extra'])) {
  61. json_fail('缺少回复内容参数', '', $Imei);
  62. }
  63. $Extra = $param['Extra'];
  64. $where[] = ['PublishVersion', '=', $version];
  65. $where[] = ['Imei', '=', $Imei];
  66. $ack = Db::name("publish_ack")->where($where)->find();
  67. if (!$ack) {
  68. json_fail('没有找到下发记录', '', $Imei);
  69. }
  70. $data = [
  71. 'Succ' => $Succ,
  72. 'AckTime' => msectime(),
  73. 'Extra' => $Extra
  74. ];
  75. Db::name("publish_ack")->where($where)->update($data);
  76. return $this->DataIssued($Imei, $version);
  77. }
  78. /**
  79. * @Descripttion: 数据处理
  80. * @name: likang
  81. * @return {*}
  82. */
  83. private function DataIssued($Imei, $version)
  84. {
  85. $where1 = [
  86. // ['Imei', '<>', $Imei],
  87. ['Version', '>', $version]
  88. ];
  89. $where2 = [
  90. ['Imei', '=', $Imei],
  91. ['ContentType', '=', 'File'],
  92. ['Version', '>', $version]
  93. ];
  94. $where3 = [];
  95. $CntVersion = $version;
  96. $content = null;
  97. $list = Db::name('publish')
  98. ->whereOr([$where1, $where2])
  99. ->order('Version asc')->limit(5)->select();
  100. if ($list->isEmpty()) {
  101. return json_success('获取成功', $content, $Imei);
  102. }
  103. foreach ($list as $key => $value) {
  104. $da = json_decode($value['Content'], true);
  105. //如果是下发计划的话,或取当前长传记录 记录下数据
  106. if ($value['ContentType'] == 'WorkPlan') {
  107. $where3[] = ['wind_number', '=', $da['wnum']];
  108. $where3[] = ['fan_number', '=', $da['fnum']];
  109. $where3[] = ['parts', '=', $da['parts']];
  110. $where3[] = ['work_sign', '=', $da['work']];
  111. $da['lt'] = WorkRecord::where($where3)->order('fastening_time desc')->value('fastening_torque');
  112. }
  113. $da['id'] = strval($da['id']);
  114. $content[] = [
  115. 'OpType' => $value['Type'],
  116. 'CntType' => $value['ContentType'],
  117. 'CntVersion' => strval($value['Version']),
  118. 'id' => strval($value['ContentId']),
  119. 'Content' => $da
  120. ];
  121. $CntVersion = strval($value['Version']);
  122. }
  123. //保存下发的记录
  124. $ack = [
  125. 'PublishVersion' => $CntVersion,
  126. 'PublishContent' => json_encode($content),
  127. 'Imei' => $Imei,
  128. 'AddTime' => msectime()
  129. ];
  130. Db::name('publish_ack')->save($ack);
  131. return json_success('获取成功', $content, $Imei);
  132. }
  133. /**
  134. * @Descripttion: Imei 是否存在
  135. * @name: likang
  136. * @param {*} $imei
  137. * @return {*}
  138. */
  139. private function IsImei($imei)
  140. {
  141. $data = Hydraulic::where('imei', $imei)->find();
  142. if ($data) {
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. }