/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.discount.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.param.ProductParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.discount.comment.model.Discount;
import com.yami.shop.discount.comment.model.DiscountProd;
import com.yami.shop.discount.comment.service.DiscountService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;


/**
 *
 * @author lgh on 2018/11/21.
 */
@RestController
@RequestMapping("/admin/discountProd")
public class DiscountProdController {

    @Autowired
    private DiscountService discountService;

    @Autowired
    private ProductService productService;

    /**
	 * 获取信息
	 */
	@GetMapping("/info/{discountId}")
	public ResponseEntity<IPage<Product>> info(@PathVariable("discountId") Long discountId, ProductParam product, PageParam<Product> page){
//		List<DiscountProd> discountProds = discountService.getDiscountAndItemAndProdById(discountId).getDiscountProds();
		Discount discount = discountService.getById(discountId);
		if(Objects.equals(discount.getSuitableProdType(),0)){
			IPage<Product> products = productService.page(page,
					new LambdaQueryWrapper<Product>()
							.eq(Product::getShopId, SecurityUtils.getShopUser().getShopId())
							.eq(Product::getStatus, 1)
							.like(StrUtil.isNotBlank(product.getProdName()),Product::getProdName,product.getProdName())
							.eq(Objects.nonNull(product.getShopCategoryId()),Product::getShopCategoryId, product.getShopCategoryId())
							.orderByDesc(Product::getPutawayTime));
			return ResponseEntity.ok(products);
		}else{
			IPage<Product> products = discountService.pageProdByDiscount(page,product,discount);
			return ResponseEntity.ok(products);
		}
	}



}
