123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace JinDouYun\Controller\Cron;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Dao\Enterprise\DEnterprise;
- use JinDouYun\Dao\Order\DOrder;
- use JinDouYun\Dao\Order\DOrderIndex;
- use JinDouYun\Model\Stock\MInventory;
- use Mall\Framework\Core\StatusCode;
- /**
- * 订单
- * 基本设置增加配置:
- * orderAutoCloseSec (货到付款的订单未审核,自动取消订单的时间节点 单位:秒)
- * orderAutoFinishSec (订单出库后用户未确认收货. 自动收货节点 单位:秒)
- * Class OrderCron
- * @package JinDouYun\Controller\Cron
- */
- class OrderCron
- {
- /**
- * 切割数量
- * @var int
- */
- private $cutTable = 200000;
- /**
- * 企业列表
- * @var array|null
- */
- private $enterprises = null;
- /**
- * 当前时间戳
- * @var int
- */
- private $nowTime;
- /**
- * @var DOrderIndex
- */
- private $objDOrderIndex;
- /**
- * @var DOrder
- */
- private $objDOrder;
- /**
- * OrderCron constructor.
- * @throws \Exception
- */
- public function __construct()
- {
- echo date('Y-m-d H:i:s') . ':订单定时任务执行开始' . PHP_EOL;
- $this->nowTime = time();
- $this->objDOrderIndex = new DOrderIndex();
- $this->objDOrder = new DOrder();
- $this->objDOrder->setSearchIndex('order_search')->setType('order');
- //获取企业
- $fields = "e.id,e.enterpriseName,b.basicData";
- $sql = 'SELECT ' . $fields . ' FROM qianniao_enterprise_1 as e
- LEFT JOIN qianniao_basic_setup as b ON e.id=b.enterpriseId
- WHERE b.id IS NOT NULL';
- $objDEnterprise = new DEnterprise();
- $enterprises = $objDEnterprise->query($sql);
- if ($enterprises === false) {
- die('查询企业列表失败');
- }
- foreach ($enterprises as $val){
- $json = json_decode($val['basicData'],true);
- $this->enterprises[] = [
- 'enterpriseId' => $val['id'],
- 'enterpriseName' => $val['enterpriseName'],
- 'orderAutoCloseSec' => isset($json['orderAutoCloseSec']) ? $json['orderAutoCloseSec'] : null, //自动取消订单设置
- 'orderAutoFinishSec' => isset($json['orderAutoFinishSec']) ? $json['orderAutoFinishSec'] : null, //自动收货订单设置
- ];
- }
- if (empty($this->enterprises)) {
- die('没有企业订单数据待处理');
- }
- }
- /**
- * 自动关闭订单
- * condition 订单创建时间 < 当前时间戳-自动关闭时间秒, payType = 3, auditStatus=1, payStatus=4,orderStatus != 6
- * @throws \Exception
- */
- public function autoClose()
- {
-
- $field = 'id,userCenterId';
- foreach ($this->enterprises as $item) {
- //检查企业是否配置订单未支付自动关闭
- if (empty((int) $item['orderAutoCloseSec'])) {
- file_put_contents('/www/wwwroot/logs/apiqnys.liuniukj.com/cron.log', date('Y-m-d H:i:s') . $item['enterpriseName'] . '未配置订单自动关闭时间,不进行处理' . PHP_EOL, FILE_APPEND);
- echo $item['enterpriseName'].'未配置自动取消时间'.PHP_EOL;
- continue;
- }
- //检查过期时间
- $expire = $this->nowTime - $item['orderAutoCloseSec'];
-
- $sql = 'SELECT ' . $field . ' FROM qianniao_order_index_' . $item['enterpriseId'] . '
- WHERE createTime>1593446400 AND
- auditStatus=' . StatusCode::$auditStatus['auditing'] . ' AND
- payStatus=' . StatusCode::$delete . ' AND
- orderStatus!=' . StatusCode::$orderStatus['close'] . ' AND
- createTime < ' . $expire;
- //查询数据
- // 根据索引表查询订单索引数据
- $orderList = $this->objDOrderIndex->query($sql);
- if ($orderList === false) {
- file_put_contents('/www/wwwroot/logs/apiqnys.liuniukj.com/cron.log', date('Y-m-d H:i:s') . $item['enterpriseName'] . '查询订单时出错' . $this->objDOrderIndex->error() . PHP_EOL, FILE_APPEND);
- echo '查询订单时出错'.$this->objDOrderIndex->error().PHP_EOL;
- continue;
- }
- //没有数据
- if (empty($orderList)) {
- echo $item['enterpriseName'] . '-没有需要处理的订单' . PHP_EOL;
- continue;
- }
-
- $count = count((array)$orderList);
- //处理数据,计算表后缀
- $allGroupOrder = [];
- foreach ($orderList as $value) {
- $tableNum = ceil($value['userCenterId'] / $this->cutTable);
- $allGroupOrder[$tableNum][] = $value['id'];
- }
- //更新操作
- $this->objDOrderIndex->beginTransaction();
- //更新订单表
- foreach ($allGroupOrder as $fix => $row) {
- //切换订单分表
- $this->objDOrder->setTable('qianniao_order_' . $item['enterpriseId'] . '_' . $fix);
- //更新数据
- //根据订单编号修改订单数据
- $orderUpdate = $this->objDOrder->update(
- [
- 'orderStatus' => StatusCode::$orderStatus['close'],
- 'updateTime' => $this->nowTime
- ],
- ['id' => $row]
- );
- if ($orderUpdate === false) {
- file_put_contents('/www/wwwroot/logs/apiqnys.liuniukj.com/cron.log', date('Y-m-d H:i:s') . $item['enterpriseName'] . 'update订单时出错' . $this->objDOrder->error() . PHP_EOL, FILE_APPEND);
- $this->objDOrderIndex->rollBack();
- die('更新订单时出错');
- }
- //解锁库存
- $objMInventory = new MInventory($item['enterpriseId'], '');
- $unlockResult = $objMInventory->unlockInventory($row, StatusCode::$orderType['saleOrder'], $this->nowTime);
- if (!$unlockResult->isSuccess()) {
- file_put_contents('/www/wwwroot/logs/apiqnys.liuniukj.com/cron.log', date('Y-m-d H:i:s') . $item['enterpriseName'] . '解锁库存时出错' . $unlockResult->getData() . PHP_EOL, FILE_APPEND);
- $this->objDOrderIndex->rollBack();
- die('关闭订单时解锁库存失败');
- }
- }
- //更新订单索引表
- $this->objDOrderIndex->setTable('qianniao_order_index_' . $item['enterpriseId']);
- $orderIndexUpdate = $this->objDOrderIndex->update(
- [
- 'orderStatus' => StatusCode::$orderStatus['close'],
- 'updateTime' => $this->nowTime
- ],
- [
- 'id' => array_values(array_column((array)$orderList, 'id'))
- ]
- );
- if ($orderIndexUpdate === false) {
- file_put_contents('/www/wwwroot/logs/apiqnys.liuniukj.com/cron.log', date('Y-m-d H:i:s') . $item['enterpriseName'] . '解锁库存时出错' . $this->objDOrderIndex->error() . PHP_EOL, FILE_APPEND);
- $this->objDOrderIndex->rollBack();
- die('更新订单索引表时出错');
- }
- echo date('Y-m-d H:i:s') . 'SUCCESS:' . $item['enterpriseName'] . '处理成功,共处理' . $count . '条数据' . PHP_EOL;
- $this->objDOrderIndex->commit();
- // //在ES更新订单状态
- // foreach ($orderList as $list) {
- // $_id = self::createEsDocumentId($list['id'], $item['enterpriseId']);
- // $this->objDOrder->esupdateTypeFieldVaule(['orderStatus' => StatusCode::$orderStatus['close']], $_id);
- // }
- }
- }
- /**
- * 自动完成订单
- * 订单出库后用户未确认收货. 自动收货
- *
- * @throws \Exception
- */
- public function autoFinish()
- {
- $field = 'id,userCenterId';
- foreach ($this->enterprises as $item) {
- //检查企业是否配置订单自动完成
- if (empty((int)$item['orderAutoFinishSec'])) {
- echo $item['enterpriseName'].'未配置自动收货时间'.PHP_EOL;
- continue;
- }
- $expire = $this->nowTime - $item['orderAutoFinishSec'];
- $sql = 'SELECT ' . $field . ' FROM qianniao_order_index_' . $item['enterpriseId'] . '
- WHERE createTime>1620907760 AND
- outStatus=' . StatusCode::$standard . ' AND
- (orderStatus !=' . StatusCode::$orderStatus['finish'] . ' or isRet = '.StatusCode::$standard.') AND
- outTime < ' . $expire;
- //查询数据
- $orderList = $this->objDOrderIndex->query($sql);
- if ($orderList === false) {
- echo '查询订单时出错'.$this->objDOrderIndex->error().PHP_EOL;
- continue;
- }
- //没有数据
- if (empty($orderList)) {
- echo $item['enterpriseName'] . '-没有需要处理的订单' . PHP_EOL;
- continue;
- }
- $count = count((array)$orderList);
- //处理数据,计算表后缀
- $allGroupOrder = [];
- foreach ($orderList as $value) {
- $tableNum = ceil($value['userCenterId'] / $this->cutTable);
- $allGroupOrder[$tableNum][] = $value['id'];
- }
- //更新操作
- $this->objDOrderIndex->beginTransaction();
- //更新订单表
- foreach ($allGroupOrder as $fix => $row) {
- //切换订单分表
- $this->objDOrder->setTable('qianniao_order_' . $item['enterpriseId'] . '_' . $fix);
- //更新数据
- $orderUpdate = $this->objDOrder->update(
- [
- 'orderStatus' => StatusCode::$orderStatus['finish'],
- 'updateTime' => $this->nowTime,
- 'successFullyTime' => $this->nowTime
- ],
- ['id' => $row]
- );
- if ($orderUpdate === false) {
- echo $item['enterpriseName'] . 'update订单时出错' . $this->objDOrder->error() . PHP_EOL;
- $this->objDOrderIndex->rollBack();
- die('更新订单时出错');
- }
- }
- $this->objDOrderIndex->setTable('qianniao_order_index_' . $item['enterpriseId']);
- $orderIndexUpdate = $this->objDOrderIndex->update(
- [
- 'orderStatus' => StatusCode::$orderStatus['finish'],
- 'updateTime' => $this->nowTime
- ],
- [
- 'id' => array_values(array_column((array)$orderList, 'id'))
- ]
- );
- if ($orderIndexUpdate === false) {
- echo $item['enterpriseName'] . '解锁库存时出错' . $this->objDOrderIndex->error() . PHP_EOL;
- $this->objDOrderIndex->rollBack();
- die('更新订单索引表时出错');
- }
- echo date('Y-m-d H:i:s') . 'autoFinish-SUCCESS:' . $item['enterpriseName'] . '处理成功,共处理' . $count . '条数据' . PHP_EOL;
- $this->objDOrderIndex->commit();
- //在ES更新订单审核状态
- /*foreach ($orderList as $list) {
- $_id = self::createEsDocumentId($list['orderId'], $item['enterpriseId']);
- $this->objDOrder->esupdateTypeFieldVaule(['orderStatus' => StatusCode::$orderStatus['finish']], $_id);
- }*/
- }
- }
- /**
- * 创建文档id
- * @param $orderIndexId
- * @param $enterpriseId
- * @return string
- */
- private function createEsDocumentId(int $orderIndexId, int $enterpriseId)
- {
- return 'EnterpriseId_' . $enterpriseId . '_OrderIndexId_' . $orderIndexId;
- }
- public function __destruct()
- {
- // TODO: Implement __destruct() method.
- echo date('Y-m-d H:i:s') . ':订单定时任务执行结束' . PHP_EOL;
- }
- }
|