Goods.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use app\main\constants\CacheConstants;
  5. use app\main\constants\PayConstants;
  6. use app\main\service\GoodsService;
  7. use think\Model;
  8. class Goods extends Model
  9. {
  10. // 表名
  11. protected $table = 'goods';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. // 追加属性
  18. protected $append = [
  19. 'type_text',
  20. 'show_type_text',
  21. ];
  22. protected static function init()
  23. {
  24. self::afterInsert(function ($row) {
  25. $pk = $row->getPk();
  26. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  27. });
  28. }
  29. public function getTypeList()
  30. {
  31. return ['1' => '书币充值', '2' => 'VIP充值'];
  32. }
  33. public function getShowTypeList()
  34. {
  35. return ['0' => '全部用户', '1' => '已充值用户', '2' => '未充值用户', '3' => '新用户', '4' => '老用户'];
  36. }
  37. public function getTypeTextAttr($value, $data)
  38. {
  39. $value = $value ? $value : $data['type'];
  40. $list = $this->getTypeList();
  41. return isset($list[$value]) ? $list[$value] : '';
  42. }
  43. public function getShowTypeTextAttr($value, $data)
  44. {
  45. $value = $value ? $value : $data['show_type'];
  46. $list = $this->getShowTypeList();
  47. return isset($list[$value]) ? $list[$value] : '';
  48. }
  49. public function category()
  50. {
  51. return $this->belongsTo('Category', 'category_id', 'id')->setEagerlyType(0);
  52. }
  53. /**
  54. * 获取分类列表
  55. */
  56. public function getCategoryList()
  57. {
  58. $category = model('Category');
  59. // 获取商品父类
  60. $goodsCategory = $category->field('id')->where('nickname', '=', 'goods')->findOrFail();
  61. // 充值下的分类
  62. $list = $category
  63. ->where('pid', '=', $goodsCategory['id'])
  64. ->where('status', '=', 'normal')
  65. ->order('weigh desc,id asc')
  66. ->select();
  67. return $list;
  68. }
  69. /**
  70. * 获取商品列表
  71. */
  72. public function getList()
  73. {
  74. $redis = Redis::instance();
  75. $key = CacheConstants::getGoodsAllKey();
  76. if ($redis->exists($key)) {
  77. return json_decode($redis->get($key), true);
  78. } else {
  79. $data = [];
  80. $categoryList = $this->getCategoryList();
  81. foreach ($categoryList as $key => $item) {
  82. $list = $this
  83. ->where('category_id', '=', $item['id'])
  84. ->order('weigh desc,id asc')
  85. ->select();
  86. $data[] = [
  87. 'category' => $item, //分类
  88. 'goods' => $list, //商品
  89. ];
  90. }
  91. $redis->setex($key, 300, json_encode($data, JSON_UNESCAPED_UNICODE));
  92. return $data;
  93. }
  94. }
  95. public function getGoodsInfoById($goods_id){
  96. $redis = Redis::instance();
  97. $key = CacheConstants::getGoodsInfoKey($goods_id);
  98. if ($redis->exists($key)) {
  99. $result = json_decode($redis->get($key), true);
  100. }else{
  101. $result = [];
  102. if($goods_info = $this->where('id',$goods_id)->find()){
  103. $result = $goods_info->toArray();
  104. }
  105. $redis->setex($key, 60, json_encode($result, JSON_UNESCAPED_UNICODE));
  106. }
  107. return $result;
  108. }
  109. public function getGoodsCategoryIds($category_name){
  110. $category_name = (array)$category_name;
  111. $category = model('Category');
  112. // 获取商品父类
  113. $goodsCategory = $category->field('id')->where('nickname', '=', 'goods')->findOrFail();
  114. $category->where('pid','=' ,$goodsCategory['id'])->where('status', '=', 'normal');
  115. // 充值下的分类
  116. if(!empty($category_name)){
  117. $category->wherein('nickname',implode(',',$category_name));
  118. }
  119. $list = $category->order('weigh desc,id asc')->column('id');
  120. return $list;
  121. }
  122. /**
  123. * @param null $business
  124. * @param null $is_test
  125. * @param null $is_pay
  126. * @return array
  127. */
  128. public function getGoodsListForApp($business = null, $is_test = null, $is_pay = null)
  129. {
  130. $customGoodsList = GoodsService::instance()->getChannelGoodsList(0);
  131. $ids = $this->getGoodsCategoryIds('test');
  132. foreach ($customGoodsList as $index=>$item) {
  133. $list_line = explode(',', $item['business_line']);
  134. if (!in_array($business, $list_line)) {
  135. unset($customGoodsList[$index]);
  136. continue;
  137. }
  138. //检查支付
  139. if ($item['show_type'] != '0') {
  140. if ($is_pay && $item['show_type'] == '2') {
  141. unset($customGoodsList[$index]);
  142. continue;
  143. }
  144. if (!$is_pay && $item['show_type'] == '1') {
  145. unset($customGoodsList[$index]);
  146. continue;
  147. }
  148. }
  149. //检查测试
  150. if (!$is_test) {
  151. if (in_array($item['category_id'], $ids)) {
  152. unset($customGoodsList[$index]);
  153. continue;
  154. }
  155. }
  156. $customGoodsList[$index]['temp_select'] = !empty($item['second_description']) ? 2 : 1;
  157. }
  158. return array_values($customGoodsList);
  159. }
  160. /**
  161. * 获取商品列表
  162. * @param string $category
  163. * @param int $business
  164. * @param int $is_test
  165. * @param int $is_pay
  166. * @param int $goods_type
  167. * @return array
  168. */
  169. public function getGoodsList($business = null,$category = null,$goods_type = null,$is_test = null,$is_pay = null){
  170. $redis = Redis::instance();
  171. $key = CacheConstants::getGoodsListKey($business,$category,$goods_type,$is_test);
  172. if ($redis->exists($key)) {
  173. $result = json_decode($redis->get($key), true);
  174. }else{
  175. $goods_category = [];
  176. //分类处理
  177. if($category !== null && !empty($category)){
  178. array_push($goods_category,$category);
  179. }
  180. //测试处理
  181. if($is_test !== null && $is_test){
  182. array_push($goods_category,PayConstants::GOODS_CATEGORY_TEST);
  183. }
  184. //业务线条件处理
  185. if($business !== null){
  186. $this->where("find_in_set({$business},business_line)");
  187. }
  188. //商品分类处理
  189. if($goods_type !== null){
  190. $this->where('type',$goods_type);
  191. }
  192. //分类条件处理
  193. if(!empty($goods_category)){
  194. $category_map = [];
  195. //分类查询map
  196. if($ids = $this->getGoodsCategoryIds($goods_category)){
  197. foreach($ids as $category_id){
  198. array_push($category_map,"find_in_set({$category_id},category_id)");
  199. }
  200. }
  201. //分类不为空时
  202. if(!empty($category_map)){
  203. if(count($category_map) > 1){
  204. $this->where("(".implode(' or ',$category_map).")");
  205. }else{
  206. $this->where(current($category_map));
  207. }
  208. }
  209. //不是测试时不展示测试商品
  210. if($is_test !== null && !$is_test){
  211. $test_cate_id = current($this->getGoodsCategoryIds([PayConstants::GOODS_CATEGORY_TEST]));
  212. $this->where("!find_in_set({$test_cate_id},category_id)");
  213. }
  214. }
  215. if($result = $this->order('weigh desc,id asc')->select()){
  216. $result = collection($result)->toArray();;
  217. }
  218. $redis->setex($key, 30, json_encode($result, JSON_UNESCAPED_UNICODE));
  219. }
  220. //检查支付
  221. if($is_pay !== null){
  222. foreach($result as $key => $val){
  223. if(intval($is_pay) == 0){
  224. if(intval($val['show_type']) == 1){
  225. unset($result[$key]);
  226. }
  227. }else{
  228. if(intval($val['show_type']) == 2){
  229. unset($result[$key]);
  230. }
  231. }
  232. }
  233. }
  234. return array_values($result);
  235. }
  236. /**
  237. * 根据分类nickname获取商品列表
  238. *
  239. * @param string $categoryNickname 分类nickname
  240. * @return array
  241. */
  242. public function getListByCategoryNickname($categoryNickname)
  243. {
  244. $list = $this->getList();
  245. foreach ($list as $key=>$item){
  246. if($item['category']['nickname'] == $categoryNickname){
  247. return [$item];
  248. }
  249. }
  250. return [];
  251. }
  252. /**
  253. * 根据分类id获取商品列表
  254. *
  255. * @param int $categoryId 分类Id
  256. * @return array
  257. */
  258. public function getListByCategoryId($categoryId)
  259. {
  260. $list = $this->getList();
  261. foreach ($list as $key=>$item){
  262. if($item['category']['id'] == $categoryId){
  263. return [$item];
  264. }
  265. }
  266. return [];
  267. }
  268. }