Index.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\QcodeProduct;
  4. use app\admin\model\QcodeUnit;
  5. use app\common\controller\Api;
  6. use app\admin\controller\Sample;
  7. use think\Log;
  8. use think\Db;
  9. /**
  10. * 首页接口
  11. */
  12. class Index extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 首页
  18. *
  19. */
  20. public function index()
  21. {
  22. $this->success('请求成功');
  23. }
  24. /**
  25. * 产品表qr_code_product同步
  26. */
  27. public function vo1()
  28. {
  29. $num1 = $this->request->param('num1');
  30. $num2 = $this->request->param('num2');
  31. if($num2<=$num1){
  32. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  33. }
  34. $qcodeProduct = new QcodeProduct();
  35. // 连接到其他数据库
  36. $config = [
  37. 'type' => 'mysql',
  38. 'hostname' => '127.0.0.1',
  39. 'database' => 'dm_7in6_com',
  40. 'username' => 'root',
  41. 'password' => 'root',
  42. 'charset' => 'utf8mb4',
  43. 'prefix' => 'qr_',
  44. ];
  45. $db = Db::connect($config);
  46. //查询主表记录
  47. $rows1 = $db->name('qcode_product')
  48. ->limit($num1,$num2-$num1)
  49. ->select();
  50. foreach($rows1 as $v){
  51. //1. 查询mongodb中是否存在该条记录
  52. $bool = $qcodeProduct->where('oid_id',$v['id'])->find();
  53. if ($bool) continue;
  54. //2. 获取设置主表数据
  55. $row['oid_id'] = $v['id'];
  56. $row['product_name'] = $v['product_name'];
  57. $row['product_code'] = $v['product_code'];
  58. $row['temple'] = $v['temple'];
  59. // unset($row['id']);
  60. // $row = array_merge(['oid_id'=>$v['id']],$row);
  61. //查询unit表数据
  62. $rows2 = $db->name('qcode_unit')
  63. ->where('code',$v['product_code'])
  64. ->find();
  65. if($rows2){
  66. if($rows2['main_unit']!=null){
  67. $row['main_unit'] = $rows2['main_unit'];
  68. }else{
  69. $row['main_unit'] = '';
  70. }
  71. if($rows2['sec_unit']!=null){
  72. $row['sec_unit'] = $rows2['sec_unit'];
  73. }else{
  74. $row['sec_unit'] = '';
  75. }
  76. if($rows2['proportion']!=null){
  77. $row['proportion'] = $rows2['proportion'];
  78. }else{
  79. $row['proportion'] = '';
  80. }
  81. }else{
  82. $row['main_unit'] = '';
  83. $row['sec_unit'] = '';
  84. $row['proportion'] = '';
  85. }
  86. //3. 插入主表记录到mongodb中
  87. $qcodeProduct = new QcodeProduct();
  88. $qcodeProduct->save($row);
  89. }
  90. $this->success('成功');
  91. }
  92. /**
  93. * 产品表qr_code_unit同步
  94. */
  95. public function vo2()
  96. {
  97. $num1 = $this->request->param('num1');
  98. $num2 = $this->request->param('num2');
  99. if($num2<=$num1){
  100. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  101. }
  102. $qcodeUnit = new QcodeUnit();
  103. // 连接到其他数据库
  104. $config = [
  105. 'type' => 'mysql',
  106. 'hostname' => '127.0.0.1',
  107. 'database' => 'dm_7in6_com',
  108. 'username' => 'root',
  109. 'password' => 'root',
  110. 'charset' => 'utf8mb4',
  111. 'prefix' => 'qr_',
  112. ];
  113. $db = Db::connect($config);
  114. //查询主表记录
  115. $rows1 = $db->name('qcode_unit')
  116. ->limit($num1,$num2-$num1)
  117. ->select();
  118. foreach($rows1 as $v){
  119. //1. 查询mongodb中是否存在该条记录
  120. $bool = $qcodeUnit->where('oid_id',$v['id'])->find();
  121. if ($bool) continue;
  122. //2. 获取设置主表数据
  123. $row = $v;
  124. unset($row['id']);
  125. $row = array_merge(['oid_id'=>$v['id']],$row);
  126. //3. 插入主表记录到mongodb中
  127. $qcodeUnit = new QcodeUnit();
  128. $qcodeUnit->save($row);
  129. }
  130. $this->success('成功');
  131. }
  132. }