where($where)->count(); $lists = $model->field(true)->where($where) ->append(['prize_name']) ->order(['sort'=>'desc', 'id' => 'desc']) ->page($get['page'], $get['limit']) ->select(); foreach ($lists as &$item) { $item['number'] = $item['prize_type'] == 2 ? '-' : $item['number']; $item['probability'] = $item['probability'] . '%'; } return ['count'=>$count, 'list'=>$lists]; } public static function detail($id) { $model = new Luckdraw(); return $model->field(true) ->where('id', '=', (int)$id) ->find(); } /** * Notes: 新增奖品 * @param $post * @author 张无忌(2021/1/26 10:28) * @return bool */ public static function add($post) { try { $post['name'] = Luckdraw::getPrizeDesc($post['prize_type']); $post['image'] = !empty($post['image']) ? $post['image'] : ''; $model = new Luckdraw(); return $model->save([ 'prize_type' => $post['prize_type'], 'name' => $post['name'], 'image' => $post['image'], 'number' => self::getPrizeValue($post), 'sort' => $post['sort'], 'probability' => $post['probability'], 'status' => $post['status'], 'is_delete' => 0, 'create_time' => time(), 'update_time' => time() ]); } catch (\Exception $e) { static::$error = $e->getMessage(); return false; } } /** * Notes: 编辑奖品 * @param $post * @author 张无忌(2021/1/26 10:28) * @return bool */ public static function edit($post) { try { $post['name'] = Luckdraw::getPrizeDesc($post['prize_type']); $post['image'] = !empty($post['image']) ? $post['image'] : ''; $model = new Luckdraw(); $result = $model->where(['id'=>(int)$post['id']])->update([ 'prize_type' => $post['prize_type'], 'name' => $post['name'], 'image' => $post['image'], 'number' => self::getPrizeValue($post), 'sort' => $post['sort'], 'probability' => $post['probability'], 'status' => $post['status'], 'update_time' => time() ]); return $result ? true : false; } catch (\Exception $e) { static::$error = $e->getMessage(); return false; } } /** * Notes: 删除奖品 * @param $id * @author 张无忌(2021/1/26 11:17) * @return bool */ public static function del($id) { try { $model = new Luckdraw(); $result = $model->where(['id'=>(int)$id])->update([ 'is_delete' => 1, 'update_time' => time() ]); return $result ? true : false; } catch (\Exception $e) { static::$error = $e->getMessage(); return false; } } /** * Notes: 更新抽奖设置 * @param $post * @author 张无忌(2021/1/26 11:40) * @return bool * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public static function set($post) { if ($post['status'] == 1) { $model = new Luckdraw(); $prizes = $model->field(true) ->where(['is_delete' => 0, 'status'=>1]) ->order(['id'=>'desc', 'sort'=>'desc']) ->select(); if (count($prizes) != 8) { static::$error = '抽奖限制8个抽奖奖品'; return false; } $probability = array_column($prizes->toArray(), 'probability'); if (array_sum($probability) > 100) { static::$error = '中奖概率合计不能大于100%'; return false; } } ConfigServer::set('luckdraw', 'limit', $post['limit']); ConfigServer::set('luckdraw', 'need', $post['need']); ConfigServer::set('luckdraw', 'rule', $post['rule']); ConfigServer::set('luckdraw', 'status', $post['status']); ConfigServer::set('luckdraw', 'show_win', $post['show_win']); return true; } /** * Notes: 抽奖记录 * @param $get * @author 张无忌(2021/1/26 14:08) * @return array */ public static function record($get) { $model = new LuckdrawRecord(); $count = $model->count(); $lists = $model->field(true) ->with('user') ->order('id', 'desc') ->page($get['page'], $get['limit']) ->append(['send_desc']) ->select(); return ['count'=>$count, 'list'=>$lists]; } /** * Notes: 切换状态 * @param $post * @author 张无忌(2021/1/13 17:53) * @return bool */ public static function switchStatus($post) { try { $model = new Luckdraw(); $model->where(['id' => (int)$post['id']])->update([ 'status' => $post['status'], 'update_time' => time() ]); return true; } catch (\Exception $e) { static::$error = $e->getMessage(); return false; } } /** * Notes: 更新排序 * @param $post * @author 张无忌(2021/1/13 17:53) * @return bool */ public static function updateSort($post) { try { $model = new Luckdraw(); $model->where(['id' => (int)$post['id']])->update([ $post['field'] => $post['data'], 'update_time' => time() ]); return true; } catch (\Exception $e) { static::$error = $e->getMessage(); return false; } } /** * @notes 优惠券列表 * @return Coupon[] * @author 段誉 * @date 2022/2/15 10:18 */ public static function coupon() { $where[] = ['del', '=', 0]; $where[] = ['status', '=', 1]; $where[] = ['get_type', '=', 2]; return Coupon::where($where)->order('id desc')->select(); } /** * @notes 获取奖品数值 * @param $param * @return int|mixed * @author 段誉 * @date 2022/2/15 14:14 */ public static function getPrizeValue($param) { $number = 0; switch ($param['prize_type']) { case Luckdraw::PRIZE_INTEGRAL: $number = $param['integral']; break; case Luckdraw::PRIZE_COUPON: $number = $param['coupon']; break; case Luckdraw::PRIZE_BALANCE: $number = $param['balance']; break; } return $number; } }