diff --git a/addons/shopro/controller/zy/Activity.php b/addons/shopro/controller/zy/Activity.php index da9d83f..2c0d40a 100644 --- a/addons/shopro/controller/zy/Activity.php +++ b/addons/shopro/controller/zy/Activity.php @@ -3,20 +3,24 @@ namespace addons\shopro\controller\zy; use think\Db; -use think\Cache; use think\Exception; use app\admin\model\zy\Club; use app\admin\model\zy\Stadium; use app\admin\model\zy\game\Game; use think\exception\PDOException; +use app\admin\model\shopro\Category; use app\admin\model\zy\game\GameJoin; use think\exception\ValidateException; +use app\admin\model\shopro\goods\Goods; use addons\shopro\service\order\OrderCreate; - +use app\admin\controller\shopro\traits\SkuPrice; class Activity extends Base { + use SkuPrice; + protected $noNeedLogin = ['index', 'test']; + public function __construct() { $this->model = new \app\admin\model\zy\game\Activity; @@ -79,22 +83,46 @@ class Activity extends Base public function add() { - $result = false; $params = $this->request->param(); - $params['public_time'] = json_encode($params['public_time'] ?? []); - $params['join_start_time'] = json_encode($params['join_start_time'] ?? []); - $params['join_end_time'] = json_encode($params['join_end_time'] ?? []); - $params['quit_time'] = json_encode($params['quit_time'] ?? []); - $params['cost'] = json_encode($params['cost'] ?? []); Db::startTrans(); try { + $category = new Category; + $category->name = $params['name']; + $category->parent_id = 157; + $category->description = '报名费商品'; + $category->save(); + foreach ($this->model->costKey as $key => $val) { + if (isset($params['cost'][$key])) { + $goods = new Goods; + $goods->category_ids = $category->id; + $goods->subtitle = $key; + $goods->title = $val; + $goods->type = 'virtual'; + $goods->limit_type = 'none'; + $goods->dispatch_type = 'autosend'; + $goods->dispatch_id = 2; + $goods->is_sku = 0; + $goods->original_price = $params['price'][$key]; + $goods->price = $params['cost'][$key]; + $goods->save(); + $this->zySku($goods, 'add'); + } else { + throw new Exception('请填写' . $val); + } + } + $params['public_time'] = json_encode($params['public_time'] ?? []); + $params['join_start_time'] = json_encode($params['join_start_time'] ?? []); + $params['join_end_time'] = json_encode($params['join_end_time'] ?? []); + $params['quit_time'] = json_encode($params['quit_time'] ?? []); + $params['cost']['goods_category_id'] = $category->id; + $params['cost'] = json_encode($params['cost'] ?? []); $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } - if ($result === false) { + if (empty($result)) { $this->error('操作失败'); } $this->success('Success'); @@ -102,16 +130,30 @@ class Activity extends Base public function update() { - $result = false; $params = $this->request->param(); $model = $this->model->get($params['id']); if (empty($model)) { $this->error(__('No rows were found')); } + $cost = json_decode($model->cost, true); + $goods = Goods::where('category_ids', $cost['goods_category_id'])->select(); + foreach ($this->model->costKey as $key => $val) { + if (isset($params['cost'][$key])) { + foreach ($goods as $g) { //更新报名费价格 + if ($g['title'] == $val && $g['price'] != $params['cost'][$key]) { + $g->save(['price' => $params['cost'][$key]]); + $this->zySku($g, 'edit'); + } + } + } else { + throw new Exception('请填写' . $val); + } + } $params['public_time'] = json_encode($params['public_time'] ?? []); $params['join_start_time'] = json_encode($params['join_start_time'] ?? []); $params['join_end_time'] = json_encode($params['join_end_time'] ?? []); $params['quit_time'] = json_encode($params['quit_time'] ?? []); + $params['cost']['goods_category_id'] = $cost['goods_category_id']; $params['cost'] = json_encode($params['cost'] ?? []); Db::startTrans(); try { @@ -121,7 +163,7 @@ class Activity extends Base Db::rollback(); $this->error($e->getMessage()); } - if ($result === false) { + if (empty($result)) { $this->error('操作失败'); } $this->success('Success'); @@ -158,12 +200,15 @@ class Activity extends Base if ($game['join_start_time'] > date('Y-m-d H:i:s')) { $this->error('活动报名时间未开始'); } - - $this->svalidate($params, ".create"); + if (empty($params['users']) || !is_array($params['users']) || empty($params['goods_list']) || !is_array($params['goods_list'])) { + $this->error('请核对报名人员'); + } + if (GameJoin::get(['game_id' => $game['id'], 'act_id' => $game['act_id'], 'user_id' => $this->auth->id])) { + $this->error('您已报名此活动'); + } $orderCreate = new OrderCreate($params); $result = $orderCreate->calc('create'); $order = $orderCreate->create($result); - $join = new GameJoin; $join->allowField(true)->save([ 'act_id' => $game['act_id'], @@ -180,6 +225,6 @@ class Activity extends Base $this->error($e->getMessage(), $e); } - $this->success('Success', $join); + $this->success('Success'); } } diff --git a/addons/shopro/traits/StockWarning.php b/addons/shopro/traits/StockWarning.php index a57d8af..b28b05e 100644 --- a/addons/shopro/traits/StockWarning.php +++ b/addons/shopro/traits/StockWarning.php @@ -195,7 +195,7 @@ trait StockWarning $stockWarning = new StockLogModel(); $stockWarning->goods_id = $goodsSkuPrice['goods_id']; - $stockWarning->admin_id = $admin['id']; + $stockWarning->admin_id = $admin['id'] ?? 0; $stockWarning->goods_sku_price_id = $goodsSkuPrice['id']; $stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text']; $stockWarning->before = $before; diff --git a/application/admin/controller/shopro/traits/SkuPrice.php b/application/admin/controller/shopro/traits/SkuPrice.php index a76f13b..df632a7 100644 --- a/application/admin/controller/shopro/traits/SkuPrice.php +++ b/application/admin/controller/shopro/traits/SkuPrice.php @@ -40,7 +40,13 @@ trait SkuPrice protected function editSimSku($goods, $type = 'add') { $params = $this->request->only([ - 'stock', 'stock_warning', 'sn', 'weight', 'cost_price', 'original_price', 'price' + 'stock', + 'stock_warning', + 'sn', + 'weight', + 'cost_price', + 'original_price', + 'price' ]); $data = [ @@ -83,7 +89,36 @@ trait SkuPrice // 检测库存预警 $this->checkStockWarning($skuPrice, $type); } + } + // 中羿体育项目报名费sku处理 + private function zySku($goods, $type) + { + $data = [ + 'goods_id' => $goods->id, + 'stock' => 999, + 'cost_price' => 0, + 'original_price' => 0, + 'price' => $goods['price'][0] ?? 0, + 'original_price' => $goods['price'][0] ?? 0, + 'stock_warning' => null + ]; + if ($type == 'edit') { + $skuPrice = SkuPriceModel::where('goods_id', $goods->id)->order('id', 'asc')->find(); + if ($skuPrice) { + SkuPriceModel::where('goods_id', $goods->id)->where('id', '<>', $skuPrice->id)->delete(); + SkuModel::where('goods_id', $goods->id)->delete(); + } + unset($data['stock']); + } + if (!isset($skuPrice) || !$skuPrice) { + $skuPrice = new SkuPriceModel(); + } + $skuPrice->save($data); + if ($type == 'add') { + $this->addStockLog($skuPrice, 0, $data['stock'], $type); + $this->checkStockWarning($skuPrice, $type); + } } @@ -97,7 +132,8 @@ trait SkuPrice protected function editMultSku($goods, $type = 'add') { $params = $this->request->only([ - 'skus', 'sku_prices' + 'skus', + 'sku_prices' ]); $skus = $params['skus'] ?? []; $skuPrices = $params['sku_prices'] ?? []; @@ -176,7 +212,7 @@ trait SkuPrice * @param array $skuPrices * @return void */ - private function checkMultSku($skus, $skuPrices) + private function checkMultSku($skus, $skuPrices) { if (count($skus) < 1) { error_stop('请填写规格列表'); @@ -300,4 +336,4 @@ trait SkuPrice return $allChildrenSku; } -} \ No newline at end of file +} diff --git a/application/admin/model/zy/game/Activity.php b/application/admin/model/zy/game/Activity.php index e30ba62..fbe497d 100644 --- a/application/admin/model/zy/game/Activity.php +++ b/application/admin/model/zy/game/Activity.php @@ -8,14 +8,14 @@ use think\Model; class Activity extends Model { - - + + // 表名 protected $table = 'zy_activity'; public static $tableName = 'zy_activity'; - + // 自动写入时间戳字段 protected $autoWriteTimestamp = false; @@ -25,15 +25,14 @@ class Activity extends Model protected $deleteTime = false; // 追加属性 - protected $append = [ - ]; - - - + protected $append = []; + + + + public $costKey = ['man' => '男报名费', 'woman' => '女报名费', 'extra' => '附加费', 'server' => '服务费']; - public function stadium() {