IndexController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace addons\Mch\console\controllers;
  3. use addons\Mch\common\enums\MchEnum;
  4. use addons\Mch\common\models\Mch;
  5. use common\enums\AddonsEnum;
  6. use common\models\goods\Goods;
  7. use common\models\mall\MallAddons;
  8. use yii\console\Controller;
  9. use common\models\mall\Mall;
  10. use common\enums\StatusEnum;
  11. use yii\console\ExitCode;
  12. use addons\Mch\common\models\SettlementLog;
  13. class IndexController extends Controller
  14. {
  15. public function actionIndex()
  16. {
  17. try {
  18. $mall_list = Mall::getEnableMallList(true);
  19. $mall_ids = array_column($mall_list, 'id');
  20. if (empty($mall_ids)) {
  21. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  22. return ExitCode::UNSPECIFIED_ERROR;
  23. }
  24. foreach ($mall_ids as $mall_id) {
  25. $is_install = MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_STORE);
  26. if(empty($is_install)) continue;
  27. $mch_list = Mch::find()
  28. ->select('id,store_name,logo_img,praise')
  29. ->where( ['mall_id' => $mall_id,'status' => StatusEnum::ENABLED, 'check_status' => MchEnum::CHECK_ADOPT])
  30. ->andWhere(['<', 'expire_time', time()])
  31. ->andWhere(['>', 'expire_time', 0])
  32. ->all();
  33. $mch_ids = array_column($mch_list,'id');
  34. Mch::updateAll(['status'=>StatusEnum::DISABLED],['id'=>$mch_ids]);
  35. Goods::updateAll(['is_on_sale'=>0],['mch_id'=>$mch_ids]);
  36. }
  37. return ExitCode::OK;
  38. } catch (\Exception $e) {
  39. return ExitCode::UNSPECIFIED_ERROR;
  40. }
  41. }
  42. public function actionSettlementLog()
  43. {
  44. try {
  45. $logs = SettlementLog::find()
  46. ->where(['is_settlement'=>0])
  47. ->andWhere(['<','settlement_at',time()])
  48. ->all();
  49. if(!empty($logs)){
  50. foreach($logs as $log){
  51. $trans = \Yii::$app->db->beginTransaction();
  52. try{
  53. $log->is_settlement = 1;
  54. if(!$log->save()){
  55. throw new \Exception('门店订单结算异常,id:'.$log->id);
  56. }
  57. //结算货款
  58. Mch::settlementStoreCommission($log['order_id'], $log['store_id'], $log['order_no']);
  59. $trans->commit();
  60. }catch(\Exception $e){
  61. $trans->rollBack();
  62. throw new \Exception($e->getMessage());
  63. }
  64. }
  65. }
  66. return ExitCode::OK;
  67. } catch (\Exception $e) {
  68. return ExitCode::UNSPECIFIED_ERROR;
  69. }
  70. }
  71. }