123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use app\main\constants\PayConstants;
- use app\main\service\GoodsService;
- use think\Model;
- class Goods extends Model
- {
- // 表名
- protected $table = 'goods';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'type_text',
- 'show_type_text',
- ];
- protected static function init()
- {
- self::afterInsert(function ($row) {
- $pk = $row->getPk();
- $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
- });
- }
- public function getTypeList()
- {
- return ['1' => '书币充值', '2' => 'VIP充值'];
- }
- public function getShowTypeList()
- {
- return ['0' => '全部用户', '1' => '已充值用户', '2' => '未充值用户', '3' => '新用户', '4' => '老用户'];
- }
- public function getTypeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['type'];
- $list = $this->getTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getShowTypeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['show_type'];
- $list = $this->getShowTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function category()
- {
- return $this->belongsTo('Category', 'category_id', 'id')->setEagerlyType(0);
- }
- /**
- * 获取分类列表
- */
- public function getCategoryList()
- {
- $category = model('Category');
- // 获取商品父类
- $goodsCategory = $category->field('id')->where('nickname', '=', 'goods')->findOrFail();
- // 充值下的分类
- $list = $category
- ->where('pid', '=', $goodsCategory['id'])
- ->where('status', '=', 'normal')
- ->order('weigh desc,id asc')
- ->select();
- return $list;
- }
- /**
- * 获取商品列表
- */
- public function getList()
- {
- $redis = Redis::instance();
- $key = CacheConstants::getGoodsAllKey();
- if ($redis->exists($key)) {
- return json_decode($redis->get($key), true);
- } else {
- $data = [];
- $categoryList = $this->getCategoryList();
- foreach ($categoryList as $key => $item) {
- $list = $this
- ->where('category_id', '=', $item['id'])
- ->order('weigh desc,id asc')
- ->select();
- $data[] = [
- 'category' => $item, //分类
- 'goods' => $list, //商品
- ];
- }
- $redis->setex($key, 300, json_encode($data, JSON_UNESCAPED_UNICODE));
- return $data;
- }
- }
- public function getGoodsInfoById($goods_id){
- $redis = Redis::instance();
- $key = CacheConstants::getGoodsInfoKey($goods_id);
- if ($redis->exists($key)) {
- $result = json_decode($redis->get($key), true);
- }else{
- $result = [];
- if($goods_info = $this->where('id',$goods_id)->find()){
- $result = $goods_info->toArray();
- }
- $redis->setex($key, 60, json_encode($result, JSON_UNESCAPED_UNICODE));
- }
- return $result;
- }
- public function getGoodsCategoryIds($category_name){
- $category_name = (array)$category_name;
- $category = model('Category');
- // 获取商品父类
- $goodsCategory = $category->field('id')->where('nickname', '=', 'goods')->findOrFail();
- $category->where('pid','=' ,$goodsCategory['id'])->where('status', '=', 'normal');
- // 充值下的分类
- if(!empty($category_name)){
- $category->wherein('nickname',implode(',',$category_name));
- }
- $list = $category->order('weigh desc,id asc')->column('id');
- return $list;
- }
- /**
- * @param null $business
- * @param null $is_test
- * @param null $is_pay
- * @return array
- */
- public function getGoodsListForApp($business = null, $is_test = null, $is_pay = null)
- {
- $customGoodsList = GoodsService::instance()->getChannelGoodsList(0);
- $ids = $this->getGoodsCategoryIds('test');
- foreach ($customGoodsList as $index=>$item) {
- $list_line = explode(',', $item['business_line']);
- if (!in_array($business, $list_line)) {
- unset($customGoodsList[$index]);
- continue;
- }
- //检查支付
- if ($item['show_type'] != '0') {
- if ($is_pay && $item['show_type'] == '2') {
- unset($customGoodsList[$index]);
- continue;
- }
- if (!$is_pay && $item['show_type'] == '1') {
- unset($customGoodsList[$index]);
- continue;
- }
- }
- //检查测试
- if (!$is_test) {
- if (in_array($item['category_id'], $ids)) {
- unset($customGoodsList[$index]);
- continue;
- }
- }
- $customGoodsList[$index]['temp_select'] = !empty($item['second_description']) ? 2 : 1;
- }
- return array_values($customGoodsList);
- }
- /**
- * 获取商品列表
- * @param string $category
- * @param int $business
- * @param int $is_test
- * @param int $is_pay
- * @param int $goods_type
- * @return array
- */
- public function getGoodsList($business = null,$category = null,$goods_type = null,$is_test = null,$is_pay = null){
- $redis = Redis::instance();
- $key = CacheConstants::getGoodsListKey($business,$category,$goods_type,$is_test);
- if ($redis->exists($key)) {
- $result = json_decode($redis->get($key), true);
- }else{
- $goods_category = [];
- //分类处理
- if($category !== null && !empty($category)){
- array_push($goods_category,$category);
- }
- //测试处理
- if($is_test !== null && $is_test){
- array_push($goods_category,PayConstants::GOODS_CATEGORY_TEST);
- }
- //业务线条件处理
- if($business !== null){
- $this->where("find_in_set({$business},business_line)");
- }
- //商品分类处理
- if($goods_type !== null){
- $this->where('type',$goods_type);
- }
- //分类条件处理
- if(!empty($goods_category)){
- $category_map = [];
- //分类查询map
- if($ids = $this->getGoodsCategoryIds($goods_category)){
- foreach($ids as $category_id){
- array_push($category_map,"find_in_set({$category_id},category_id)");
- }
- }
- //分类不为空时
- if(!empty($category_map)){
- if(count($category_map) > 1){
- $this->where("(".implode(' or ',$category_map).")");
- }else{
- $this->where(current($category_map));
- }
- }
- //不是测试时不展示测试商品
- if($is_test !== null && !$is_test){
- $test_cate_id = current($this->getGoodsCategoryIds([PayConstants::GOODS_CATEGORY_TEST]));
- $this->where("!find_in_set({$test_cate_id},category_id)");
- }
- }
- if($result = $this->order('weigh desc,id asc')->select()){
- $result = collection($result)->toArray();;
- }
- $redis->setex($key, 30, json_encode($result, JSON_UNESCAPED_UNICODE));
- }
- //检查支付
- if($is_pay !== null){
- foreach($result as $key => $val){
- if(intval($is_pay) == 0){
- if(intval($val['show_type']) == 1){
- unset($result[$key]);
- }
- }else{
- if(intval($val['show_type']) == 2){
- unset($result[$key]);
- }
- }
- }
- }
- return array_values($result);
- }
- /**
- * 根据分类nickname获取商品列表
- *
- * @param string $categoryNickname 分类nickname
- * @return array
- */
- public function getListByCategoryNickname($categoryNickname)
- {
- $list = $this->getList();
- foreach ($list as $key=>$item){
- if($item['category']['nickname'] == $categoryNickname){
- return [$item];
- }
- }
- return [];
- }
- /**
- * 根据分类id获取商品列表
- *
- * @param int $categoryId 分类Id
- * @return array
- */
- public function getListByCategoryId($categoryId)
- {
- $list = $this->getList();
- foreach ($list as $key=>$item){
- if($item['category']['id'] == $categoryId){
- return [$item];
- }
- }
- return [];
- }
- }
|