123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- /**
- * Award.php UTF-8
- * 奖品
- *
- * @date : 2018/5/10 22:49
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\controller\shop;
- use huo\controller\common\Base;
- use huo\controller\member\MemCache;
- use huo\model\integral\AwardsModel;
- use huolib\constant\MemItgConst;
- use huolib\status\ShopStatus;
- use huolib\tool\Rand;
- use think\Cache;
- class Award extends Base {
- /**
- * 实例化
- *
- * @return static
- */
- public static function ins() {
- return new static();
- }
- /**
- * @param int $mem_id
- * @param int $act_id
- * @param int $cost_integral
- *
- * @return array
- */
- public function draw($mem_id, $act_id = 0, $cost_integral = 0) {
- $_award_datas = $this->getAwards($act_id);
- $_arr = [];
- foreach ($_award_datas as $_k => $_v) {
- $_arr[$_k] = $_v['rate'];
- }
- $_key = Rand::getRand($_arr);
- $_award_data = $_award_datas[$_key];
- $_goods_id = $_award_data['goods_id'];
- $_cost_integral = $cost_integral;
- $_rs = (new ItgOrder())->drawExchange($mem_id, $_goods_id, [], $act_id, $cost_integral);
- if (ShopStatus::NO_ERROR != $_rs['code']) {
- return $this->huoError($_rs['code'], $_rs['msg']);
- }
- $_goods_data = GoodsCache::ins()->getInfoByGoodsId($_goods_id);
- if (empty($_cost_integral)) {
- $_cost_integral = $_goods_data['integral'];
- }
- $_me_data = MemCache::ins()->getMeInfoById($mem_id);
- $_rdata['my_integral'] = $_me_data['my_integral'];
- $_rdata['cost_integral'] = $_cost_integral;
- $_rdata['award_id'] = $_award_data['id'];
- $_rdata['award_name'] = $_award_data['award_name'];
- $_rdata['has_award'] = $_goods_data['gain_integral'] < 0.01 ? MemItgConst::AWARD_NO_GOODS
- : MemItgConst::AWARD_HAS_GOODS;
- $_code = ShopStatus::NO_ERROR;
- return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
- }
- /**
- * 获取活动奖品KEY
- *
- * @param int $act_id 活动ID
- *
- * @return string
- */
- public function getAwardsKey($act_id) {
- return 'act_awards_'.$act_id;
- }
- /**
- * 获取活动奖品信息
- *
- * @param $act_id
- *
- * @return array|bool|mixed
- */
- public function getAwards($act_id = 0) {
- $_key = $this->getAwardsKey($act_id);
- $_awards_data_json = Cache::get($_key);
- $_awards_data = json_decode($_awards_data_json, true);
- if (!is_array($_awards_data)) {
- $_awards_data = $_awards_data_json;
- }
- if (!is_array($_awards_data) || empty($_awards_data)) {
- $_awards_data = (new AwardsModel())->getAwards($act_id);
- if (empty($_awards_data)) {
- return false;
- }
- $this->saveAwardsCache($act_id, $_awards_data);
- }
- return $_awards_data;
- }
- /**
- * 保存活动奖品信息
- *
- * @param int $act_id
- * @param array $awards_data
- * @param int $ttl
- */
- public function saveAwardsCache($act_id, $awards_data, $ttl = 3600) {
- $_key = $this->getAwardsKey($act_id);
- Cache::set($_key, json_encode($awards_data), $ttl);
- }
- /**
- * 更新奖品
- *
- * @param $award_id
- * @param $award_data
- *
- * @return bool
- */
- public function updateAward($award_id, $award_data) {
- $_key = $this->getAwardsKey($award_data['act_id']);
- Cache::rm($_key);
- return (new AwardsModel())->updateAward($award_data, $award_id);
- }
- /**
- * 添加奖品
- *
- * @param $award_data
- *
- * @return bool|mixed
- */
- public function addAward($award_data) {
- $_key = $this->getAwardsKey($award_data['act_id']);
- Cache::rm($_key);
- return (new AwardsModel())->addAward($award_data);
- }
- /**
- * 排序 排序字段为list_orders数组 POST 排序字段为:list_order
- *
- * @param $_act_id 活动id
- * @param $ids //排序值
- *
- * @return bool
- */
- public function listOrders($_act_id, $ids) {
- $_key = $this->getAwardsKey($_act_id);
- Cache::rm($_key);
- $model = new AwardsModel();
- $pk = $model->getPk(); //获取主键名称
- if (!empty($ids)) {
- foreach ($ids as $key => $r) {
- $data['list_order'] = $r;
- $model->where([$pk => $key])->update($data);
- }
- }
- return true;
- }
- }
|