Index.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\QcodeClassification;
  4. use app\admin\model\QcodeGsmc;
  5. use app\admin\model\QcodeProduct;
  6. use app\admin\model\QcodeUnit;
  7. use app\common\controller\Api;
  8. use app\admin\controller\Sample;
  9. use think\Log;
  10. use think\Db;
  11. use think\Model;
  12. /**
  13. * 首页接口
  14. */
  15. class Index extends Api
  16. {
  17. protected $noNeedLogin = ['*'];
  18. protected $noNeedRight = ['*'];
  19. /**
  20. * 首页
  21. *
  22. */
  23. public function index()
  24. {
  25. $this->success('请求成功');
  26. }
  27. /**
  28. * 产品表qr_qcode_product同步
  29. */
  30. public function vo1()
  31. {
  32. $num1 = $this->request->param('num1');
  33. $num2 = $this->request->param('num2');
  34. if($num2<=$num1){
  35. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  36. }
  37. $qcodeProduct = new QcodeProduct();
  38. // 连接到其他数据库
  39. $config = [
  40. 'type' => 'mysql',
  41. 'hostname' => '127.0.0.1',
  42. 'database' => 'dm_7in6_com',
  43. 'username' => 'root',
  44. 'password' => 'root',
  45. 'charset' => 'utf8mb4',
  46. 'prefix' => 'qr_',
  47. ];
  48. $db = Db::connect($config);
  49. //查询主表记录
  50. $rows1 = $db->name('qcode_product')
  51. ->limit($num1,$num2-$num1)
  52. ->select();
  53. foreach($rows1 as $v){
  54. //1. 查询mongodb中是否存在该条记录
  55. $bool = $qcodeProduct->where('oid_id',$v['id'])->find();
  56. if ($bool) continue;
  57. //2. 获取设置主表数据
  58. $row['oid_id'] = $v['id'];
  59. $row['product_name'] = $v['product_name'];
  60. $row['product_code'] = $v['product_code'];
  61. $row['temple'] = $v['temple'];
  62. // unset($row['id']);
  63. // $row = array_merge(['oid_id'=>$v['id']],$row);
  64. //查询unit表数据
  65. $rows2 = $db->name('qcode_unit')
  66. ->where('code',$v['product_code'])
  67. ->find();
  68. if($rows2){
  69. if($rows2['main_unit']!=null){
  70. $row['main_unit'] = $rows2['main_unit'];
  71. }else{
  72. $row['main_unit'] = '';
  73. }
  74. if($rows2['sec_unit']!=null){
  75. $row['sec_unit'] = $rows2['sec_unit'];
  76. }else{
  77. $row['sec_unit'] = '';
  78. }
  79. if($rows2['proportion']!=null){
  80. $row['proportion'] = $rows2['proportion'];
  81. }else{
  82. $row['proportion'] = '';
  83. }
  84. }else{
  85. $row['main_unit'] = '';
  86. $row['sec_unit'] = '';
  87. $row['proportion'] = '';
  88. }
  89. //3. 插入主表记录到mongodb中
  90. $qcodeProduct = new QcodeProduct();
  91. $qcodeProduct->save($row);
  92. }
  93. $this->success('成功');
  94. }
  95. /**
  96. * 产品表qr_qcode_unit同步
  97. */
  98. public function vo2()
  99. {
  100. $num1 = $this->request->param('num1');
  101. $num2 = $this->request->param('num2');
  102. if($num2<=$num1){
  103. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  104. }
  105. $qcodeGsmc = new QcodeGsmc();
  106. // 连接到其他数据库
  107. $config = [
  108. 'type' => 'mysql',
  109. 'hostname' => '127.0.0.1',
  110. 'database' => 'dm_7in6_com',
  111. 'username' => 'root',
  112. 'password' => 'root',
  113. 'charset' => 'utf8mb4',
  114. 'prefix' => 'qr_',
  115. ];
  116. $db = Db::connect($config);
  117. //查询主表记录
  118. $rows1 = $db->name('qcode_gsmc')
  119. ->limit($num1,$num2-$num1)
  120. ->select();
  121. foreach($rows1 as $v){
  122. //1. 查询mongodb中是否存在该条记录
  123. $bool = $qcodeGsmc->where('oid_id',$v['id'])->find();
  124. if ($bool) continue;
  125. //2. 获取设置主表数据
  126. $row = $v;
  127. unset($row['id']);
  128. $row = array_merge(['oid_id'=>$v['id']],$row);
  129. //3. 插入主表记录到mongodb中
  130. $qcodeGsmc = new QcodeGsmc();
  131. $qcodeGsmc->save($row);
  132. }
  133. $this->success('成功');
  134. }
  135. /**
  136. * 产品表qr_qcode_classification同步
  137. */
  138. public function vo3()
  139. {
  140. $num1 = $this->request->param('num1');
  141. $num2 = $this->request->param('num2');
  142. if($num2<=$num1){
  143. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  144. }
  145. $qcodeClassification = new QcodeClassification();
  146. // 连接到其他数据库
  147. $config = [
  148. 'type' => 'mysql',
  149. 'hostname' => '127.0.0.1',
  150. 'database' => 'dm_7in6_com',
  151. 'username' => 'root',
  152. 'password' => 'root',
  153. 'charset' => 'utf8mb4',
  154. 'prefix' => 'qr_',
  155. ];
  156. $db = Db::connect($config);
  157. //查询主表记录
  158. $rows1 = $db->name('qcode_classification')
  159. ->limit($num1,$num2-$num1)
  160. ->select();
  161. foreach($rows1 as $v){
  162. //1. 查询mongodb中是否存在该条记录
  163. $bool = $qcodeClassification->where('oid_id',$v['id'])->find();
  164. if ($bool) continue;
  165. //2. 获取设置主表数据
  166. $row['oid_id'] = $v['id'];
  167. $row['code'] = $v['code'];
  168. $row['name'] = $v['name'];
  169. $row['status'] = $v['status'];
  170. //3. 插入主表记录到mongodb中
  171. $qcodeClassification = new QcodeClassification();
  172. $qcodeClassification->save($row);
  173. }
  174. $this->success('成功');
  175. }
  176. /**
  177. * 设置产品表qr_qcode_classification
  178. */
  179. public function vo4()
  180. {
  181. $qcodeClassification = new QcodeProduct();
  182. //查询主表记录
  183. $rows1 = $qcodeClassification->select();
  184. foreach($rows1 as $v){
  185. //2. 获取设置主表数据
  186. $row = [];
  187. $row['code'] = substr($v['product_code'],0,4);
  188. //3. 插入主表记录到mongodb中
  189. $qcodeClassification = new QcodeProduct();
  190. $row = $qcodeClassification->where('oid_id',$v['oid_id'])->update($row);
  191. }
  192. $this->success('成功');
  193. }
  194. }