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.
169 lines
5.8 KiB
169 lines
5.8 KiB
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
use app\model\ZoneGoods;
|
|
use app\model\ZoneGoodsParam;
|
|
use app\validate\Zone as ZoneValidate;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Request;
|
|
|
|
class AdminZoneManage extends BaseController
|
|
{
|
|
|
|
/**
|
|
* 刮奖专区列表
|
|
* @return array
|
|
*/
|
|
public function zoneGoodsList()
|
|
{
|
|
$param = Request::param();
|
|
return $this->renderSuccess('数据返回成功',ZoneGoods::getList($param,true));
|
|
}
|
|
|
|
/**
|
|
* 刮奖专区启用禁用
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function zoneGoodsChangeStatus()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
validate(ZoneValidate::class)->scene('changeStatus')->check($param);
|
|
|
|
$id = $param['zone_goods_id'];
|
|
$status = $param['status'];
|
|
|
|
$ZoneGoods = new ZoneGoods();
|
|
$ZoneGoodsUp = $ZoneGoods->find($id);
|
|
$ZoneGoodsUp->status = $status;
|
|
$ZoneGoodsUp->save();
|
|
|
|
return $this->renderSuccess($status == 1 ? '已启用' : '已禁用');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 奖金设置列表
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function goodsParamList()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
validate(ZoneValidate::class)->scene('goodsParamList')->check($param);
|
|
|
|
$zone_goods_id = $param['zone_goods_id'];
|
|
|
|
$ZoneGoodsParam = new ZoneGoodsParam();
|
|
$list = $ZoneGoodsParam->where('zone_goods_id',$zone_goods_id)
|
|
->field('id,amount,image,probability')
|
|
->order('amount','desc')
|
|
->select()
|
|
->toArray();
|
|
foreach ($list as &$item) {
|
|
$item['probability_str'] = ($item['probability'] * 100) .'%';
|
|
$item['probability'] = $item['probability'] * 100;
|
|
$item['amount'] = round($item['amount']);
|
|
$item['amount_str'] = format_money($item['amount']);
|
|
$item['image'] = get_image_url($item['image']);
|
|
}
|
|
|
|
return $this->renderSuccess('数据返回成功',['list' => $list]);
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 添加|编辑奖金
|
|
* @return array
|
|
*/
|
|
public function goodsParamSave()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
$id = $param['z_g_p_id'] ?? 0;
|
|
$zone_goods_id = $param['zone_goods_id'] ?? '';
|
|
|
|
$ZoneGoodsParam = new ZoneGoodsParam();
|
|
|
|
# 填写总概率不能超出 1
|
|
$queryWhere = [['zone_goods_id','=',$zone_goods_id],['status','=',1]];
|
|
# 编辑时不算当前id
|
|
if (empty($id) && is_numeric($id)) $queryWhere[] = ['id','<>',$id];
|
|
$query = $ZoneGoodsParam->where($queryWhere)
|
|
->field('SUM(probability) as sum_p')
|
|
->find();
|
|
if (!empty($query->sum_p)) {
|
|
$sumProbability = $query->sum_p + ($param['probability'] / 100);
|
|
if ($sumProbability > 1) {
|
|
throw new ValidateException('奖金总概率数不得超出100%,现已超出' . (($sumProbability - 1) * 100) . '%' );
|
|
}
|
|
}
|
|
|
|
if ($id) {
|
|
validate(ZoneValidate::class)->scene('goodsParamEdit')->check($param);
|
|
$Save = $ZoneGoodsParam->find($id);
|
|
$Save->amount = $param['amount'];
|
|
$Save->probability = $param['probability'] / 100;
|
|
$Save->update_time = date("Y-m-d H:i:s",time());
|
|
$Save->save();
|
|
} else {
|
|
validate(ZoneValidate::class)->scene('goodsParamAdd')->check($param);
|
|
|
|
$zoneGoodsInfo =ZoneGoods::field('cover_image')->find($zone_goods_id);
|
|
$ZoneGoodsParam->save([
|
|
'zone_goods_id' => $zone_goods_id,
|
|
'amount' => $param['amount'],
|
|
'image' => $zoneGoodsInfo['cover_image'],
|
|
'probability' => $param['probability'] / 100,
|
|
'create_time' => date("Y-m-d H:i:s",time())
|
|
]);
|
|
}
|
|
return $this->renderSuccess($id ? '编辑成功' : '添加成功');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除奖金设置
|
|
* @return array
|
|
*/
|
|
public function goodsParamDel()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
|
|
validate(ZoneValidate::class)->scene('goodsParamDel')->check($param);
|
|
|
|
$result = ZoneGoodsParam::destroy($param['z_g_p_id']);
|
|
|
|
if (!$result) throw new ValidateException('删除失败');
|
|
|
|
return $this->renderSuccess('已删除');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
}
|
|
|