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.
161 lines
5.5 KiB
161 lines
5.5 KiB
3 months ago
|
<?php
|
||
|
|
||
|
namespace app\admin\service\setting;
|
||
|
|
||
|
use app\admin\service\AdminBaseService;
|
||
|
use app\admin\validate\setting\MarketValidate;
|
||
|
use app\model\ContractMarketModel;
|
||
|
use think\exception\ValidateException;
|
||
|
|
||
|
class MarketService extends AdminBaseService
|
||
|
{
|
||
|
public function index()
|
||
|
{
|
||
|
try {
|
||
|
$list = ContractMarketModel::where('type', 1)
|
||
|
->order('id', 'desc')
|
||
|
->select();
|
||
|
$rows = [];
|
||
|
if(!$list->isEmpty()){
|
||
|
foreach ($list as $item){
|
||
|
if(empty($rows[$item['trade_name']])){
|
||
|
$rows[$item['trade_name']] = [
|
||
|
'trade_name' => $item['trade_name'],
|
||
|
'begin_time' => $item['begin_time'],
|
||
|
'end_time' => $item['end_time'],
|
||
|
'max_price' => $item['max_price'],
|
||
|
'step' => $item['step'],
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$rows = array_values($rows);
|
||
|
return $this->toData('0','SUCCSS',['list' => $rows, 'total' => count($rows)]);
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', []);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 详情
|
||
|
public function detail($param)
|
||
|
{
|
||
|
try {
|
||
|
if(empty($param['trade_name']) || !is_string($param['trade_name'])){
|
||
|
return $this->toData('1', '参数错误');
|
||
|
}
|
||
|
$list = ContractMarketModel::where('type', 1)
|
||
|
->where('trade_name', $param['trade_name'])
|
||
|
->order('id', 'desc')->select();
|
||
|
$rows = [];
|
||
|
if(!$list->isEmpty()){
|
||
|
foreach ($list as $item){
|
||
|
$rows[] = [
|
||
|
'id' => $item['id'],
|
||
|
'trade_name' => $item['trade_name'],
|
||
|
'end_time' => $item['end_time'],
|
||
|
'begin_time' => $item['begin_time'],
|
||
|
'max_price' => $item['max_price'],
|
||
|
'step' => $item['step'],
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
return $this->toData('0','SUCCSS',['list' => $rows]);
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 新增
|
||
|
public function add($param)
|
||
|
{
|
||
|
try {
|
||
|
// 参数校验
|
||
|
validate(MarketValidate::class)->scene('add')->check($param);
|
||
|
|
||
|
// 必须大于当前时间
|
||
|
if(strtotime($param['end_time']) - time() <= 0){
|
||
|
return $this->toData('1','时间无效');
|
||
|
}
|
||
|
|
||
|
|
||
|
if((strtotime($param['begin_time']) - time()) <= 0 || (strtotime($param['end_time']) - strtotime($param['begin_time'])) <= 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->type = 1;
|
||
|
$market->save();
|
||
|
|
||
|
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(MarketValidate::class)->scene('edit')->check($param);
|
||
|
|
||
|
// 目标
|
||
|
$market = ContractMarketModel::where('id', $param['id'])->where('type', 1)->find();
|
||
|
if(empty($market)){
|
||
|
return $this->toData('1','目标不存在');
|
||
|
}
|
||
|
|
||
|
// 必须大于当前时间
|
||
|
if(strtotime($param['end_time']) - time() <= 0){
|
||
|
return $this->toData('1','时间无效');
|
||
|
}
|
||
|
|
||
|
if((strtotime($param['begin_time']) - time()) <= 0 || (strtotime($param['end_time']) - strtotime($param['begin_time'])) <= 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->save();
|
||
|
|
||
|
return $this->toData('0','SUCCESS');
|
||
|
}catch (ValidateException $validateException){
|
||
|
return $this->toData('1', $validateException->getMessage(), []);
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', []);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 删除
|
||
|
public function del($param)
|
||
|
{
|
||
|
try {
|
||
|
// 参数校验
|
||
|
validate(MarketValidate::class)->scene('del')->check($param);
|
||
|
|
||
|
// 目标
|
||
|
$market = ContractMarketModel::where('id', $param['id'])->where('type',1)->find();
|
||
|
if(empty($market)){
|
||
|
return $this->toData('1','目标不存在');
|
||
|
}
|
||
|
|
||
|
$market->delete();
|
||
|
return $this->toData('0','SUCCESS');
|
||
|
}catch (ValidateException $validateException){
|
||
|
return $this->toData('1', $validateException->getMessage(), []);
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', []);
|
||
|
}
|
||
|
}
|
||
|
}
|