PriceCron.Class.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace JinDouYun\Controller\Cron;
  3. use JinDouYun\Dao\Price\DNationalUnifiedPrice;
  4. use JinDouYun\Dao\Price\DPriceRangeSheet;
  5. /**
  6. * 同步价格数据到最高/低价格表
  7. * Class PriceCron
  8. * @package JinDouYun\Controller\Cron
  9. */
  10. class PriceCron
  11. {
  12. public function __construct()
  13. {
  14. echo date('Y-m-d H:i:s') . ':同步价格数据开始' . PHP_EOL;
  15. }
  16. /**
  17. * @throws \Exception
  18. */
  19. public function syncRangePrice()
  20. {
  21. $enterpriseIds = [1,3,4,10,11,13,15,16];//dev
  22. //$enterpriseIds = [1,4,7,8,9,13,14,17,18,19,21,23,24,25,27];//master
  23. $objDNationalUnifiedPrice = new DNationalUnifiedPrice();
  24. $objDPriceRangeSheet = new DPriceRangeSheet();
  25. foreach ($enterpriseIds as $enterpriseId){
  26. // 按照企业分表
  27. $tableName = 'qianniao_nationalUnifiedPrice_' . $enterpriseId;
  28. $objDNationalUnifiedPrice->setTable($tableName);
  29. $dbResult = $objDNationalUnifiedPrice->select([],'goodsId,shopId,salePrice');
  30. if ($dbResult === false){
  31. echo '获取'.$enterpriseId.'企业价格表数据失败'.$objDNationalUnifiedPrice->error().PHP_EOL;
  32. exit;
  33. }
  34. if (empty($dbResult)){
  35. echo '企业'.$enterpriseId.'价格表数据为空不需要同步'.PHP_EOL;
  36. continue;
  37. }
  38. // 按照企业分表
  39. $objDPriceRangeSheet->setTable('qianniao_priceRangeSheet_'.$enterpriseId);
  40. $add = [];
  41. foreach ($dbResult as $row){
  42. $salePrice = json_decode($row['salePrice'],true);
  43. $allSalePrice = array_column($salePrice,'salePrice');
  44. $sale = [
  45. 'minSalePrice' => min($allSalePrice),
  46. 'maxSalePrice' => max($allSalePrice),
  47. ];
  48. $add[] = [
  49. 'goodsCode' => isset($row['goodsCode']) ? $row['goodsCode'] : '',
  50. 'goodsName' => isset($row['goodsName']) ? $row['goodsName'] : '',
  51. 'goodsId' => $row['goodsId'],
  52. 'shopId' => $row['shopId'],
  53. 'createTime' => time(),
  54. 'salePrice' => json_encode($sale),
  55. ];
  56. }
  57. $result = $objDPriceRangeSheet->replace($add,true);
  58. if ($result === false){
  59. echo '企业'.$enterpriseId.'写入最低/高表价格失败'.$objDPriceRangeSheet->error().PHP_EOL;
  60. exit;
  61. }
  62. echo '企业'.$enterpriseId.'写入最低/高价格表成功,共写入'.count($add).'条数据'.PHP_EOL;
  63. }
  64. }
  65. public function __destruct()
  66. {
  67. echo date('Y-m-d H:i:s') . ':同步价格数据完成' . PHP_EOL;
  68. }
  69. }