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.
175 lines
5.9 KiB
175 lines
5.9 KiB
<?php
|
|
|
|
namespace app\admin\service\setting;
|
|
|
|
use app\admin\service\AdminBaseService;
|
|
use app\admin\validate\setting\HqValidate;
|
|
use app\model\ContractMarketModel;
|
|
use think\exception\ValidateException;
|
|
|
|
class HqService extends AdminBaseService
|
|
{
|
|
|
|
public function index()
|
|
{
|
|
try {
|
|
$list = ContractMarketModel::where('type', 0)->order('id', 'desc')->select();
|
|
$rows = [];
|
|
if(!$list->isEmpty()){
|
|
$rows = $list->toArray();
|
|
}
|
|
return $this->toData('0','SUCCSS',['list' => $rows, 'total' => count($rows)]);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
|
}
|
|
}
|
|
|
|
|
|
// 新增
|
|
public function add($param)
|
|
{
|
|
try {
|
|
// 参数校验
|
|
validate(HqValidate::class)->scene('add')->check($param);
|
|
|
|
// 已存在未执行的 不能再插入
|
|
$count = ContractMarketModel::where('trade_name', $param['trade_name'])
|
|
->where('is_get',1)
|
|
->where('type', 0)
|
|
->count();
|
|
if($count > 0){
|
|
return $this->toData('1','已存在未执行');
|
|
}
|
|
|
|
//判断
|
|
$diff = strtotime($param['end_time']) - strtotime($param['begin_time']);
|
|
if($diff > 600 || $diff <=0){
|
|
return $this->toData('1','时间无效-结束时间必须在开始时间十分钟以内');
|
|
}
|
|
|
|
// 不能有交叉时间
|
|
$map1 = [
|
|
['trade_name', '=',$param['trade_name']],
|
|
['begin_time','between', [$param['begin_time'], $param['end_time']]]
|
|
];
|
|
$map2 = [
|
|
['trade_name', '=',$param['trade_name']],
|
|
['end_time','between', [$param['begin_time'], $param['end_time']]]
|
|
];
|
|
|
|
$count = ContractMarketModel::whereOr([$map1,$map2])
|
|
->count();
|
|
if($count > 0){
|
|
return $this->toData('1','时间无效-时间不能有交叉');
|
|
}
|
|
|
|
$market = new ContractMarketModel;
|
|
$market->trade_name = $param['trade_name'];
|
|
$market->begin_time = $param['begin_time'];
|
|
$market->end_time = $param['end_time'];
|
|
$market->max_price = $param['max_price'];
|
|
$market->step = $param['step'];
|
|
$market->type = 0;
|
|
$market->save();
|
|
|
|
// 写入redis
|
|
$this->initContractHqData();
|
|
|
|
return $this->toData('0','SUCCESS');
|
|
}catch (ValidateException $validateException){
|
|
return $this->toData('1', $validateException->getMessage(), []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
|
}
|
|
}
|
|
|
|
|
|
// 编辑
|
|
public function edit($param)
|
|
{
|
|
try {
|
|
// 参数校验
|
|
validate(HqValidate::class)->scene('edit')->check($param);
|
|
|
|
// 已存在未执行的 不能再插入
|
|
$count = ContractMarketModel::where('trade_name', $param['trade_name'])
|
|
->where('id', '<>', $param['id'])
|
|
->where('is_get',1)
|
|
->where('type', 0)
|
|
->count();
|
|
|
|
if($count > 0){
|
|
return $this->toData('1','已存在未执行');
|
|
}
|
|
|
|
// 目标
|
|
$market = ContractMarketModel::where('id', $param['id'])->where('type', 0)->find();
|
|
if(empty($market)){
|
|
return $this->toData('1','目标不存在');
|
|
}
|
|
|
|
//判断
|
|
$diff = strtotime($param['end_time']) - strtotime($param['begin_time']);
|
|
if($diff > 600 || $diff <=0){
|
|
return $this->toData('1','时间无效');
|
|
}
|
|
|
|
// 不能有交叉时间
|
|
$map1 = [
|
|
['trade_name', '=',$param['trade_name']],
|
|
['id', '<>',$param['id']],
|
|
['begin_time','between', [$param['begin_time'], $param['end_time']]]
|
|
];
|
|
$map2 = [
|
|
['trade_name', '=',$param['trade_name']],
|
|
['id', '<>',$param['id']],
|
|
['end_time','between', [$param['begin_time'], $param['end_time']]]
|
|
];
|
|
|
|
$count = ContractMarketModel::whereOr([$map1,$map2])
|
|
->count();
|
|
if($count > 0){
|
|
return $this->toData('1','时间无效');
|
|
}
|
|
|
|
$market->trade_name = $param['trade_name'];
|
|
$market->begin_time = $param['begin_time'];
|
|
$market->end_time = $param['end_time'];
|
|
$market->max_price = $param['max_price'];
|
|
$market->step = $param['step'];
|
|
$market->save();
|
|
|
|
// redis
|
|
$this->initContractHqData();
|
|
return $this->toData('0','SUCCESS');
|
|
}catch (ValidateException $validateException){
|
|
return $this->toData('1', $validateException->getMessage(), []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('1', '系统繁忙', [$exception->getMessage(),$exception->getTrace()]);
|
|
}
|
|
}
|
|
|
|
// 删除
|
|
public function del($param)
|
|
{
|
|
try {
|
|
// 参数校验
|
|
validate(HqValidate::class)->scene('del')->check($param);
|
|
|
|
// 目标
|
|
$market = ContractMarketModel::where('id', $param['id'])->find();
|
|
if(empty($market)){
|
|
return $this->toData('1','目标不存在');
|
|
}
|
|
|
|
$market->delete();
|
|
|
|
$this->initContractHqData();
|
|
return $this->toData('0','SUCCESS');
|
|
}catch (ValidateException $validateException){
|
|
return $this->toData('12', $validateException->getMessage(), []);
|
|
}catch (\Exception $exception){
|
|
return $this->toData('11', '系统繁忙', [$exception->getMessage()]);
|
|
}
|
|
}
|
|
}
|