/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.CacheBizConstant;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.desk.entity.Notice;
import org.springblade.modules.desk.service.INoticeService;
import org.springblade.modules.desk.vo.NoticeVO;
import org.springblade.modules.desk.wrapper.NoticeWrapper;
import org.springblade.modules.shopping.entity.Brands;
import org.springblade.modules.shopping.entity.Goods;
import org.springblade.modules.shopping.service.IBrandsService;
import org.springblade.modules.shopping.service.IConsignConfigService;
import org.springblade.modules.shopping.service.IConsignService;
import org.springblade.modules.shopping.service.IGoodsService;
import org.springblade.modules.system.service.IRegionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 控制器
 *
 * @author xuwei
 * @since 2022-02-12
 */
@RestController
@AllArgsConstructor
@RequestMapping(CommonConstant.API_URL)
@Api(value = "商城", tags = "11.商城接口")
public class Api06Controller extends BladeController {

	private final IGoodsService goodsService;
	private final IBrandsService brandsService;
	private final INoticeService noticeService;
	public static IRegionService regionService;
	private final BladeRedis bladeRedis;

	/**
	 * 商品分页查询
	 */
	@GetMapping("/goods-page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "商品分页查询", notes = "传入类型： 1新品推荐，2人气推荐，3酒票专区，4猜我喜欢,  不传类型是传入搜索关键字则搜索相关商品")
	public R<List<Goods>> goodsPage(@ApiParam(value = "传入类型", required = false) @RequestParam(required = false) Integer type,
									 @ApiParam(value = "用户ID", required = false) @RequestParam(required = false) Long userId,
									 @ApiParam(value = "所属关键字", required = false) @RequestParam(required = false) String searchName,
									 Query query) {
		Goods goods = new Goods();
		goods.setIsSale(2);//上架
		if (type == 1) {
			goods.setIsNew(2);//新品
		}else if (type == 2) {
			goods.setIsHot(2);//热销-人气推荐
		}else if (type == 3) {
			goods.setIsTicket(2);//酒票专区
		}else if (type == 4) {
			if (userId == null){
				return R.fail("传入参数错误！");
			}
			IPage<Goods> list = goodsService.page(Condition.getPage(query),
				Condition.getQueryWrapper(goods).lambda()
					.exists("select 1 from t_user_browse b where b.user_id = " + userId + " and b.goods_id = goods_id order by update_time, times")
					.orderByAsc(Goods::getSort));
			return R.data(list.getRecords());
		}else{
			if (StringUtil.isNotBlank(searchName)){
				//商品搜索
				IPage<Goods> list = goodsService.page(Condition.getPage(query),
					Condition.getQueryWrapper(goods).lambda()
						.like(Goods::getGoodsDesc, searchName).or()
						.like(Goods::getGoodsName, searchName).or()
						.like(Goods::getGoodsLabel, searchName)
						.orderByAsc(Goods::getSort));
				return R.data(list.getRecords());
			}
		}
		IPage<Goods> list = goodsService.page(Condition.getPage(query),
			Condition.getQueryWrapper(goods).lambda()
				.gt(Goods::getGoodsStock, 10)
				.orderByAsc(Goods::getSort));
		return R.data(list.getRecords());
	}

	@GetMapping("/brands-list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "商品品牌", notes = "商品品牌")
	public R<List<Brands>> brandsList() {
		List<Brands> list = bladeRedis.get(CacheBizConstant.CACHE_GOODS_BRAND);
		if (list == null || list.size() == 0){
			list = brandsService.list(Wrappers.<Brands>lambdaQuery());
			bladeRedis.setEx(CacheBizConstant.CACHE_GOODS_BRAND, list, CacheBizConstant.CACHE_TIME);
		}
		return R.data(list);
	}

	/**
	 * 通知公告
	 */
	@GetMapping("/notice-list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "通知公告", notes = "通知公告")
	public R<List<NoticeVO>> noticeList() {
		List<Notice> list = noticeService.list(Wrappers.<Notice>lambdaQuery()
			.eq(Notice::getStatus, 1)
				.eq(Notice::getCategory, 6)
			.orderByDesc(Notice::getUpdateTime));
		return R.data(NoticeWrapper.build().listVO(list));
	}
}
