Index.php 4.2 KB

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