Index.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_gsmc同步
  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. /**
  195. * 同步bach
  196. * @throws \think\Exception
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\ModelNotFoundException
  199. * @throws \think\exception\DbException
  200. */
  201. public function vo5()
  202. {
  203. $num1 = 0;
  204. $num2 = 1;
  205. if($num2<=$num1){
  206. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  207. }
  208. // 连接到其他数据库
  209. $config = [
  210. 'type' => 'mysql',
  211. 'hostname' => '127.0.0.1',
  212. 'database' => 'dm_7in6_com',
  213. 'username' => 'root',
  214. 'password' => 'root',
  215. 'charset' => 'utf8mb4',
  216. 'prefix' => 'qr_',
  217. ];
  218. $db = Db::connect($config);
  219. //查询主表记录
  220. $rows1 = $db->name('qcode_bach')
  221. ->limit($num1,$num2-$num1)
  222. ->order('id desc')
  223. ->select();
  224. $userInfo = Session::get('admin');
  225. $company_id = (int)$userInfo['company'];
  226. foreach($rows1 as $v){
  227. //1. 查询mongodb中是否存在该条记录
  228. $bool = $this->model->name($company_id.'_'."qcode_bach")->where('oid_id',$v['id'])->find();
  229. if ($bool) continue;
  230. //2. 获取设置主表数据
  231. $row = $v;
  232. unset($row['id']);
  233. $row = array_merge(['oid_id'=>$v['id']],$row);
  234. //3. 插入主表记录到mongodb中
  235. $qcodeBach = new \app\admin\model\QcodeBach();
  236. $qcodeBach->save($row);
  237. $pid = $qcodeBach->getLastInsID();
  238. $rows2 = $db->name('qcode_large')
  239. ->where('bach_id',$v['id'])
  240. ->select();
  241. foreach ($rows2 as $value){
  242. //1. 查询mongodb中是否存在该条记录
  243. $qcodeLarge = new QcodeLarge();
  244. $bool = $qcodeLarge->name($company_id.'_'."qcode_large")->where('oid_id',$value['id'])->find();
  245. if ($bool) continue;
  246. $row = $value;
  247. $row['old_bach_id'] = $value['bach_id'];
  248. $row['bach_id'] = $pid;
  249. $row['old_id'] = $value['id'];
  250. unset($row['id']);
  251. $qcodeLarge->save($row);
  252. $pid3 = $qcodeLarge->getLastInsID();
  253. $rows3 = $db->name('qcode_small')
  254. ->where('large_id',$value['id'])
  255. ->select();
  256. foreach ($rows3 as $value3){
  257. //1. 查询mongodb中是否存在该条记录
  258. $qcodeSmall = new QcodeSmall();
  259. $bool = $qcodeSmall->name($company_id.'_'."qcode_large")->where('oid_id',$value3['id'])->find();
  260. if ($bool) continue;
  261. $row = $value3;
  262. $row['old_bach_id'] = $value3['bach_id'];
  263. $row['old_large_id'] = $value3['large_id'];
  264. $row['old_id'] = $value3['id'];
  265. $row['bach_id'] = $pid;
  266. $row['large_id'] = $pid3;
  267. unset($row['id']);
  268. $qcodeSmall->save($row);
  269. }
  270. }
  271. }
  272. $this->success('成功');
  273. }
  274. }