You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
237 lines
8.3 KiB
237 lines
8.3 KiB
<?php
|
|
namespace app\home\service;
|
|
|
|
use app\home\validate\MarketValidate;
|
|
use app\model\ContractListMode;
|
|
use app\model\ContractSettingModel;
|
|
use app\model\ContractTradeModel;
|
|
use app\model\DigitalListModel;
|
|
use app\model\DigitalTradeModel;
|
|
use app\model\FeeSettingModel;
|
|
use app\model\ForexListModel;
|
|
use app\model\StockListModel;
|
|
use app\model\StockMarketModel;
|
|
use app\model\StockTradeModel;
|
|
use app\model\UserMarketModel;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Cache;
|
|
|
|
/**
|
|
* 处理行情业务
|
|
*/
|
|
class MarketService extends BaseHomeService
|
|
{
|
|
/**
|
|
* 获取行情交易对列表
|
|
* @param int $market_type
|
|
* @return array
|
|
*/
|
|
public function getMarketList(array $data):array
|
|
{
|
|
try{
|
|
validate(MarketValidate::class)->scene('getMarketList')->check($data);
|
|
//1现货,2合约,3美股
|
|
switch ($data['market_type']){
|
|
case 1:
|
|
$result = $this->getDigitalList();
|
|
break;
|
|
case 2:
|
|
$result = $this->getContractFaceList();
|
|
break;
|
|
case 3:
|
|
$result = StockListModel::getMarketList($data);
|
|
break;
|
|
default:
|
|
$result = $this->getDigitalList();
|
|
break;
|
|
}
|
|
return $this->toData(0,'Request successful.',$result);
|
|
}catch (ValidateException $validateException){
|
|
$message = $validateException->getMessage();
|
|
return $this->toData('1', $message, []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('100500', 'The system is busy. Please try again later.', [$exception->getMessage(),$exception->getTrace()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取用户自选数据
|
|
* @param int $user_id
|
|
* @param int $market_type
|
|
* @return array
|
|
*/
|
|
public function getUserMarket(array $data): array
|
|
{
|
|
try{
|
|
validate(MarketValidate::class)->scene('getMarket')->check($data);
|
|
$result=UserMarketModel::getUserMarket($data);
|
|
return $this->toData(0,'Request successful.',$result);
|
|
}catch (ValidateException $validateException){
|
|
$message = $validateException->getMessage();
|
|
return $this->toData('1', $message, []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('100500', 'The system is busy. Please try again later.', [$exception->getMessage(),$exception->getTrace()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 添加用户自选
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
|
|
public function addUserMarket(array $data):array
|
|
{
|
|
try{
|
|
validate(MarketValidate::class)->scene('setMarket')->check($data);
|
|
// 不做大写处理
|
|
// $data['trade_name']=strtoupper($data['trade_name']);
|
|
// $data['market_name']=strtoupper($data['market_name']);
|
|
|
|
$id= UserMarketModel::checkExistMarket($data);
|
|
if(!$id){
|
|
UserMarketModel::insertUserMarket($data);
|
|
}
|
|
$this->pushUserMarket($data);
|
|
return $this->toData(0,'Request successful.');
|
|
}catch (ValidateException $validateException){
|
|
$message = $validateException->getMessage();
|
|
return $this->toData('1', $message, []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('100500', 'The system is busy. Please try again later.', [$exception->getMessage(),$exception->getTrace()]);
|
|
}
|
|
|
|
|
|
}
|
|
public function getUserIsCollect(array $data): array
|
|
{
|
|
$id = UserMarketModel::checkExistMarket($data);
|
|
return $this->toData(0, 'Request successful.',[
|
|
'is_collet'=>($id > 0)
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 删除用户自选
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function delUserMarket(array $data):array
|
|
{
|
|
try{
|
|
validate(MarketValidate::class)->scene('delMarket')->check($data);
|
|
$id= UserMarketModel::checkExistMarket($data);
|
|
if($id){
|
|
UserMarketModel::delUserMarket($id);
|
|
}
|
|
$this->pushUserMarket($data);
|
|
return $this->toData(0,'Request successful.');
|
|
}catch (ValidateException $validateException){
|
|
$message = $validateException->getMessage();
|
|
return $this->toData('1', $message, []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('100500', 'The system is busy. Please try again later.', [$exception->getMessage(),$exception->getTrace()]);
|
|
}
|
|
}
|
|
private function pushUserMarket($data){
|
|
$list=UserMarketModel::getUserMarket(['page'=>1,'page_size'=>1000,'user_id'=>$data['user_id']]);
|
|
$tokenUserKey="USER:MARKET:".$data['user_id'];
|
|
$redis=$this->getRedis();
|
|
$redis->set($tokenUserKey,json_encode($list['list']));
|
|
}
|
|
private function checkMarket($market_type,$trade_name):bool
|
|
{
|
|
switch ($market_type){
|
|
case 1:
|
|
return DigitalListModel::existMarket($trade_name);
|
|
break;
|
|
case 2:
|
|
return ContractListMode::existMarket($trade_name);
|
|
break;
|
|
case 3:
|
|
return StockListModel::existMarket($trade_name);
|
|
break;
|
|
default:
|
|
return DigitalListModel::existMarket($trade_name);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getTradeFee(array $data): array
|
|
{
|
|
try{
|
|
validate(MarketValidate::class)->scene('getTradeFee')->check($data);
|
|
//$fee_info=$this->initTradeFeeSetting($data['market_type']);
|
|
$fee_info=FeeSettingModel::getTradeFeeById($data['market_type']);
|
|
return $this->toData(0,'Request successful.',$fee_info);
|
|
}catch (ValidateException $validateException){
|
|
$message = $validateException->getMessage();
|
|
return $this->toData('1', $message, []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('100500', 'The system is busy. Please try again later.', [$exception->getMessage(),$exception->getTrace()]);
|
|
}
|
|
}
|
|
public function getMarketTradeList(array $data):array
|
|
{
|
|
try{
|
|
validate(MarketValidate::class)->scene('getTrade')->check($data);
|
|
switch ($data['market_type']){
|
|
case 2:
|
|
$list=ContractTradeModel::getTradeList($data['trade_name'],intval($data['num']));
|
|
break;
|
|
case 3:
|
|
$list=StockTradeModel::getTradeList($data['trade_name'],intval($data['num']));
|
|
break;
|
|
default:
|
|
$list=DigitalTradeModel::getTradeList($data['trade_name'],intval($data['num']));
|
|
break;
|
|
|
|
}
|
|
return $this->toData(0,'Request successful.',$list);
|
|
}catch (ValidateException $validateException){
|
|
$message = $validateException->getMessage();
|
|
return $this->toData('1', $message, []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('100500', 'The system is busy. Please try again later.', [$exception->getMessage(),$exception->getTrace()]);
|
|
}
|
|
}
|
|
public function getTradeTypeList():array
|
|
{
|
|
$list=$this->getCapitalTypeList();
|
|
return $this->toData(0,'Request successful.',$list);
|
|
}
|
|
public function getContractFace():array
|
|
{
|
|
$list=$this->getContractFaceList(1);
|
|
return $this->toData(0,'Request successful.',$list);
|
|
}
|
|
public function getForexFace($data):array
|
|
{
|
|
$info=ForexListModel::where('trade_name',$data['trade_name'])->find();
|
|
if(!empty($info)){
|
|
$arr=$info->toArray();
|
|
}else{
|
|
$arr=[];
|
|
}
|
|
return $this->toData(0,'Request successful.',$arr);
|
|
}
|
|
public function getContractSetting():array
|
|
{
|
|
$list=ContractSettingModel::getSettingList();
|
|
return $this->toData(0,'Request successful.',$list);
|
|
}
|
|
public function getMarketRate():array
|
|
{
|
|
$list=StockMarketModel::where('status',1)->field('stock_market_type as market_type,unit,symbol,rate')->select();
|
|
if(!empty($list)){
|
|
$list_arr=$list->toArray();
|
|
}else{
|
|
$list_arr=[];
|
|
}
|
|
return $this->toData(0,'Request successful.',$list_arr);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|