Commit 2e783da6 authored by zhz's avatar zhz

Merge branch 'master_dev' of git.shenbd.com:qm-develop/shenbd into master_dev

parents 1e73b3c9 37b9ca43
......@@ -5,6 +5,7 @@ namespace Business\Store;
use DAO\StoreModel;
use DAO\MemberModel;
use DAO\StoreStatisticsModel;
use Elastics\ElasticsToolModel;
use Error\CodeConfigModel;
use Error\ErrorModel;
use Our\ApiConst;
......@@ -479,15 +480,84 @@ class ShopkeeperServiceModel extends \Business\AbstractModel
$beginTime=$gmtCreate;
$this->taskIndex=PageConst::taskPageBegin;
$this->taskSize=PageConst::taskPageSize;
$elasticsTool=ElasticsToolModel::getInstance('orders');
$res= $elasticsTool->indexExists();
echo json_encode($res);exit;
do{
$addDatas=array();
$where['add_time']=Common::format(" add_time>={0} and add_time<",$beginTime,TIMESTAMP);
$orders=$orderDao->getList($where, $orderDao->_primaryKey, $this->taskIndex, $this->taskSize, $orderField = 'add_time', $sort = 'desc');
$where=Common::format(" add_time>={0} and add_time<{1} ",$beginTime,TIMESTAMP);
$orders=$orderDao->getList($where, 'order_id as orderId,delete_state as deleteState,order_sn as orderSn,store_id as storeId,buyer_id as buyerId', $this->taskIndex, $this->taskSize, array( 'add_time'=>'desc'));
if(!empty($orders['list'])){
$orderIds=array_column($orders['list'],$orderDao->_primaryKey);
$resultOrders=array();
foreach($orders as $order){
$indexs=array();
$orderIds=array_column($orders['list'],'orderId');
$orderGoods=$orderGoodDao->getOrderGoodsByOrderIds($orderIds,'order_id as orderId,goods_name as goodsName');
$resultOrders=Common::intergrateOneToMany($orders['list'], $orderGoods, 'orderId', 'orderId', 'orderGoods');
foreach($resultOrders as $result){
$one['orderId']=$result['orderId'];
$one['orderSn']=$result['orderSn'];
$namesArr=array_column($result['orderGoods'],'goodsName');
$nameStr=implode(',',$namesArr);
$one['goodsName']=$nameStr;
$one['orderId']=$result['orderId'];
array_push($indexs,$one);
}
}
$storeMemberStatisticData= $orderGoodDao->getMemberStoreStaticsByCreateTime($beginTime,$this->taskIndex,$this->taskSize);
$storeMemberStatisticData=isset($storeMemberStatisticData['list'])?$storeMemberStatisticData['list']:false;
if($storeMemberStatisticData){
$this->taskIndex++;
foreach($storeMemberStatisticData as $value){
$addDatas[$value['buyerId'].'-'.$value['storeId']]['storeId']=$value['storeId'];
$addDatas[$value['buyerId'].'-'.$value['storeId']]['memberId']=$value['buyerId'];
if(isset($addDatas[$value['buyerId'].'-'.$value['storeId']]['orderGoodsCount'])){
$addDatas[$value['buyerId'].'-'.$value['storeId']]['orderGoodsCount']+=$value['goodsNum'];
}else{
$addDatas[$value['buyerId'].'-'.$value['storeId']]['orderGoodsCount']=$value['goodsNum'];
}
if(isset($addDatas[$value['buyerId'].'-'.$value['storeId']]['orderTotal'])){
$addDatas[$value['buyerId'].'-'.$value['storeId']]['orderTotal']+=$value['goodsPrice'];
}else{
$addDatas[$value['buyerId'].'-'.$value['storeId']]['orderTotal']=$value['goodsPrice'];
}
}
}
$addDatas=array_values($addDatas);
$storeMemberStatisticDao->addAll($addDatas);
}while($orders);
file_put_contents($this->baseDir . PathConst::orderLastCreateTime, TIMESTAMP);
}
//grow orderIndex
//创建索引
public function growOrdersIndex()
{
$orderGoodDao=\DAO\Order\OrderGoodsModel::getInstance(DbNameConst::salveDBConnectName);
$orderDao=\DAO\Order\OrderModel::getInstance(DbNameConst::salveDBConnectName);
$gmtCreate = file_get_contents($this->baseDir . PathConst::orderLastCreateTime);
$gmtCreate = !empty($gmtCreate)?$gmtCreate:ApiConst::zero;
$beginTime=$gmtCreate;
$this->taskIndex=PageConst::taskPageBegin;
$this->taskSize=PageConst::taskPageSize;
$elasticsTool=ElasticsToolModel::getInstance('orders');
$res= $elasticsTool->indexExists();
echo json_encode($res);exit;
do{
$addDatas=array();
$where=Common::format(" add_time>={0} and add_time<{1} ",$beginTime,TIMESTAMP);
$orders=$orderDao->getList($where, 'order_id as orderId,delete_state as deleteState,order_sn as orderSn,store_id as storeId,buyer_id as buyerId', $this->taskIndex, $this->taskSize, array( 'add_time'=>'desc'));
if(!empty($orders['list'])){
$indexs=array();
$orderIds=array_column($orders['list'],'orderId');
$orderGoods=$orderGoodDao->getOrderGoodsByOrderIds($orderIds,'order_id as orderId,goods_name as goodsName');
$resultOrders=Common::intergrateOneToMany($orders['list'], $orderGoods, 'orderId', 'orderId', 'orderGoods');
foreach($resultOrders as $result){
$one['orderId']=$result['orderId'];
$one['orderSn']=$result['orderSn'];
$namesArr=array_column($result['orderGoods'],'goodsName');
$nameStr=implode(',',$namesArr);
$one['goodsName']=$nameStr;
$one['orderId']=$result['orderId'];
array_push($indexs,$one);
}
}
$storeMemberStatisticData= $orderGoodDao->getMemberStoreStaticsByCreateTime($beginTime,$this->taskIndex,$this->taskSize);
......
......@@ -43,6 +43,7 @@ class ElasticsToolModel
'type' => $this->tbName
];
}
/**
* 创建索引 结果["acknowledged": true]
*/
......@@ -74,7 +75,7 @@ class ElasticsToolModel
],
]
],
$this->tbName=> [
$this->tbName => [
'properties' => [
'phone' => [
'type' => 'string',
......@@ -87,6 +88,7 @@ class ElasticsToolModel
$res = $this->client->indices()->create($params);
return $res;
}
/*
* 陈传文
* 创建索引在设置mapping之前要创建索引
......@@ -153,25 +155,26 @@ class ElasticsToolModel
$response = $this->client->update($updateParams);
}
//一般传where就可以了,
public function searchMatch($where,$pageIndex=ApiConst::pageIndex,$pageSize=ApiConst::pageSize,$sort=array('_score' => array('order' => 'desc')))
public function searchMatch($where, $pageIndex = ApiConst::pageIndex, $pageSize = ApiConst::pageSize, $sort = array('_score' => array('order' => 'desc')))
{
$searchParams['index'] = $this->dbName;
$searchParams['type'] = $this->tbName;
$from=($pageIndex)*$pageSize;
$from = ($pageIndex) * $pageSize;
$searchParams['from'] = $from;
$searchParams['size'] = $pageSize;
$searchParams['sort'] =$sort;
$searchParams['body']=array(
'query'=>array('match'=>$where),
$searchParams['sort'] = $sort;
$searchParams['body'] = array(
'query' => array('match' => $where),
);
$retDoc = $this->client->search($searchParams);
if(!isset($retDoc['hits'])){
if (!isset($retDoc['hits'])) {
ErrorModel::throwException(CodeConfigModel::elasticsError);
}
$htis=$retDoc['hits']['hits'];
$returnData['list']=array_column($htis,'_source');
$returnData['total']=$retDoc['hits']['total'];
$htis = $retDoc['hits']['hits'];
$returnData['list'] = array_column($htis, '_source');
$returnData['total'] = $retDoc['hits']['total'];
return $returnData;
}
......@@ -215,12 +218,16 @@ class ElasticsToolModel
*/
public function deleteIndex()
{
$params = [
'index' => $this->dbName
];
try {
$params = [
'index' => $this->dbName
];
$res = $this->client->indices()->delete($params);
return $res;
} catch (\Exception $ex) {
throw new \Exception($ex->getMessage(), $ex->getCode());
}
$res = $this->client->indices()->delete($params);
return $res;
}
/**
......@@ -236,6 +243,7 @@ class ElasticsToolModel
$res = $this->client->indices()->delete($params);
return $res;
}
/**
* 删除索引
*/
......@@ -249,30 +257,34 @@ class ElasticsToolModel
$res = $this->client->index($params);
return $res;
}
//插入多条 Document:
public function postBulkDoc($rows){
public function postBulkDoc($rows)
{
foreach($rows as $row){
foreach ($rows as $row) {
$params['body'][] = [
'index' => [
'_index' => $this->dbName,
'_type' => $this->tbName,
]
];
$params['body'][]=$row;
$params['body'][] = $row;
}
$res = $this->client->bulk($params);
return $res;
}
public function createIkField(){
public function createIkField()
{
$params = [
'index' => $this->dbName, //索引名称
'body' => [
'settings' => [ //配置
'analysis' => array(
'analyzer'=>array(
'ik'=>[
'tokenizer'=>'ik_max_word',
'analyzer' => array(
'ik' => [
'tokenizer' => 'ik_max_word',
]
)
),
......@@ -284,8 +296,8 @@ class ElasticsToolModel
],
'properties' => [ //配置数据结构与类型
'goodsName' => [ //字段1
"type" =>"string",
"analyzer"=> "ik_max_word"
"type" => "string",
"analyzer" => "ik_max_word"
],
]
],
......@@ -326,17 +338,33 @@ class ElasticsToolModel
$res = $this->client->search($params);
return $res;
}
//查看mapping
public function getMappings()
public function indexExists()
{
$params = [
'index' =>$this->dbName,
];
$res = $this->es->indices()->getMapping($params);
try{
$res =$this->client->exists(array('index'=>$this->dbName));
}catch(\Exception $ex){
return false;
}
return $res;
}
/**
//查看mapping
public function getMappings()
{
try {
$params = [
'index' => $this->dbName,
];
$res = $this->client->indices()->getMapping($params);
return $res;
} catch (\Exception $ex) {
throw new \Exception($ex->getMessage(), $ex->getCode());
}
}
/**
* 类实例
*
* @var \DAO\UserModel
......
<?php
/**
* 生成店铺订单索引
* 执行时间:每分钟执行一次
*
* @author chenjiebin <sjlinyu@qq.com>
*/
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../../../')); //指向public的上一级
require APPLICATION_PATH . '/scripts/crontab/baseCli.php';
require APPLICATION_PATH . '/scripts/crontab/common.php';
error_reporting(E_ALL ^ E_NOTICE);
class cliShopkeeperOrders extends basecli
{
const CLI_ADMIN_ID = 255;
private $bDoUnLock = FALSE; // 是否允许释放 LOCK 文件
private $_debug = 0;
private $lockFileName;
public $key='';
private $fromState;
private function mkdirs($dir, $mode = 0777)
{
if (is_dir($dir) || @mkdir($dir, $mode)) {
return TRUE;
}
if (!$this->mkdirs(dirname($dir), $mode)) {
return FALSE;
}
return @mkdir($dir, $mode);
}
/**
* 析构
*/
public function __destruct()
{
parent::__destruct();
if ($this->bDoUnLock) {
@unlink($this->lockFileName);
}
}
protected function _runCli()
{
$this->_debug = isset($this->aArgv[1]) ? intval($this->aArgv[1]) : 0;
if ($this->_debug) {
echo "*** Debug mode ***\n";
}
// Step: 02 检查是否已有相同CLI在运行中
$lockDir = $this->_getBaseFileName('shopkeeper');
if (!$this->mkdirs($lockDir)) {
echo '****create dir fail ****';
exit;
}
$this->lockFileName = $lockDir . DS . 'orders.locks';
if (file_exists($this->lockFileName)) {
$stat = stat($this->lockFileName);
if ((TIMESTAMP - $stat['mtime']) > 1800) {
echo "文件被锁超过1800秒,被强制删除";
@unlink($this->lockFileName);
} else {
$this->halt('[' . date('Y-m-d H:i:s') . '] The CLI is running' . "\n");
}
}
$this->bDoUnLock = true;
file_put_contents($this->lockFileName, "running"); // CLI 独占锁
$shopkeeperService = \Business\Store\ShopkeeperServiceModel::getInstance();
$shopkeeperService->baseDir= \Our\Common::getConfig('out.config');
try {
$shopkeeperService->createOrdersIndex();
} catch (\Exception $ex) {
throw new \Exception($ex->getMessage(),$ex->getCode());
}
echo '店铺订单索引生成成功' .date('Y-m-d H:i:s',TIMESTAMP). "\r\n";
}
}
$oCli = new cliShopkeeperOrders(TRUE);
EXIT;
?>
......@@ -58,13 +58,13 @@ $serv->on('Message', function($server, $frame) use($conf){
$rev_data=$rev_data['data'];
$realKey = KEY_REDIS_PRE.$key;
$sessData=$redis->get($realKey);
$sessDataSpilt1=explode(';',$sessData);
$sessDataSpilt2=explode('|',$sessDataSpilt1[1]);
$sessDataSpilt3=explode(':', $sessDataSpilt2[1]);
$res= $sessDataSpilt3[1];
// $sessDataSpilt1=explode(';',$sessData);
// $sessDataSpilt2=explode('|',$sessDataSpilt1[1]);
// $sessDataSpilt3=explode(':', $sessDataSpilt2[1]);
// $res= $sessDataSpilt3[1];
$matchs=array();
preg_match("/\d+/i",$res,$matchs);
$memberId= $matchs[0];
preg_match("/member_id\|i:(\d+)/i",$sessData,$matchs);
$memberId= $matchs[1];
$messageService=\Business\Message\MessageServiceModel::getInstance();
$sendFd=$redis->hGet(KEY_PRE.$rev_data['toId'],'fd');
......@@ -97,7 +97,7 @@ $serv->on('Message', function($server, $frame) use($conf){
$sendMessage['fromId']=$memberId;
$sendMessage['fromUserId']= $rev_data['fromUserId'];
$sendMessage['fromUserName']= $rev_data['fromUserName'];
$sendMessage['fromAvatar']= $messageService->getAvatar( $rev_data['fromType'],$frame->fd);
$sendMessage['fromAvatar']= $messageService->getAvatar( $rev_data['fromType'],$rev_data['fromUserId']);
$sendMessage['fromType']= $rev_data['fromType'];
$sendMessage['toId']= $rev_data['toId'];
$sendMessage['toUserId']=$rev_data['toUserId'];
......
......@@ -73,8 +73,8 @@ class cliShopkeeperOrders extends basecli
try {
$shopkeeperService->createOrdersIndex();
} catch (Exception $ex) {
throw new Exception($ex->getCode() . '|' . $ex->getMessage());
} catch (\Exception $ex) {
throw new \Exception($ex->getMessage(),$ex->getCode());
}
echo '店铺订单索引生成成功' .date('Y-m-d H:i:s',TIMESTAMP). "\r\n";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment