package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.param.BaseQueryParam;
import com.fs.store.domain.*;
import com.fs.store.param.*;
import com.fs.store.service.*;
import com.fs.store.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Api("商品中心")
@RestController
@RequestMapping(value="/app/product")
public class ProductController extends  AppBaseController {

    @Autowired
    private IFsStoreProductService productService;
    @Autowired
    private IFsStoreProductCategoryService categoryService;
    @Autowired
    private IFsStoreProductAttrService attrService;
    @Autowired
    private IFsStoreProductAttrValueService attrValueService;
    @Autowired
    private IFsStoreCartService cartService;
    @Autowired
    private IFsStoreProductRelationService productRelationService;

    /**
     * 获取用户信息
     * @return R
     * */
    @ApiOperation("获取分类")
    @GetMapping("/getProductCate")
    public R getProductCate(){
        try {
            FsStoreProductCategory param=new FsStoreProductCategory();
            param.setIsShow(1);
            param.setIsDel(0);
            List<FsStoreProductCategory> list=categoryService.selectFsStoreProductCategoryListQuery(param);
            return R.ok().put("data",list);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }
    @ApiOperation("获取分类")
    @GetMapping("/getProductCateByPid")
    public R getProductCateByPid(@RequestParam(value="pid") Long pid){
        try {
            FsStoreProductCategory param=new FsStoreProductCategory();
            param.setIsShow(1);
            param.setIsDel(0);
            param.setPid(pid);
            List<FsStoreProductCategory> list=categoryService.selectFsStoreProductCategoryListQuery(param);
            return R.ok().put("data",list);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }

    @ApiOperation("获取商品列表")
    @GetMapping("/getProducts")
    public R getProducts(FsStoreProductQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        param.setIsDisplay(1);
        List<FsStoreProductListQueryVO> productList=productService.selectFsStoreProductListQuery(param);
        PageInfo<FsStoreProductListQueryVO> listPageInfo=new PageInfo<>(productList);
        return R.ok().put("data",listPageInfo);
    }
    @ApiOperation("获取商品详情")
    @GetMapping("/getProductDetails")
    public R getProductDetails(@RequestParam(value="productId") Long productId){
        FsStoreProductQueryVO product=productService.selectFsStoreProductByIdQuery(productId);
        if(product==null){
            return R.error("商品不存在或已下架");
        }
        List<FsStoreProductAttr> productAttr=attrService.selectFsStoreProductAttrByProductId(product.getProductId());
        List<FsStoreProductAttrValue> productValues=attrValueService.selectFsStoreProductAttrValueByProductId(product.getProductId());

        //获取用户的TOKEN写入足迹
        String userId=getUserId();
        if(userId!=null){
            FsStoreProductRelation productRelation=new FsStoreProductRelation();
            productRelation.setIsDel(0);
            productRelation.setUserId(Long.parseLong(userId));
            productRelation.setProductId(product.getProductId());
            productRelation.setType("foot");
            List<FsStoreProductRelation> productRelations=productRelationService.selectFsStoreProductRelationList(productRelation);
            if(productRelations!=null&&productRelations.size()>0){
                FsStoreProductRelation relation=productRelations.get(0);
                relation.setUpdateTime(new Date());
                productRelationService.updateFsStoreProductRelation(relation);
            }
            else{
                FsStoreProductRelation relation=new FsStoreProductRelation();
                relation.setUserId(Long.parseLong(userId));
                relation.setIsDel(0);
                relation.setProductId(product.getProductId());
                relation.setUpdateTime(new Date());
                relation.setType("foot");
                relation.setCreateTime(new Date());
                relation.setUpdateTime(new Date());
                productRelationService.insertFsStoreProductRelation(relation);
            }
        }
        return R.ok().put("product",product).put("productAttr",productAttr).put("productValues",productValues);
    }

    @Login
    @ApiOperation("添加购物车")
    @PostMapping("/addCart")
    public R addCart(@Validated @RequestBody FsStoreCartParam cartParam){
        return cartService.addCart(Long.parseLong(getUserId()),cartParam);
    }
    @Login
    @ApiOperation("获取购物车列表")
    @GetMapping("/getCarts")
    public R getCarts(){
        List<FsStoreCartVO> carts= cartService.selectFsStoreCartListByUid(Long.parseLong(getUserId()));
        return R.ok().put("carts",carts);
    }
    @Login
    @ApiOperation("改变购物车数量")
    @PostMapping("/cartNum")
    public R cartNum(@Validated @RequestBody FsStoreCartNumParam cartParam){
        return cartService.changeNum(Long.parseLong(getUserId()),cartParam);
    }
    @Login
    @ApiOperation("删除购物车")
    @PostMapping("/delCart")
    public R delCart(@Validated @RequestBody FsStoreCartDelParam cartParam){
        return cartService.delCart(Long.parseLong(getUserId()),cartParam);
    }


    @Login
    @ApiOperation("获取商品购物车数量")
    @GetMapping("/getCartCount")
    public R getCartCount(FsStoreCartCountParam param){
        param.setUserId(Long.parseLong(getUserId()));
        Integer count=cartService.selectFsStoreCartCount(param);
        return R.ok().put("data", count);
    }

    @ApiOperation("获取推荐商品列表")
    @GetMapping("/getTuiProduct")
    public R getTuiProduct(){
        List<FsStoreProductListQueryVO> productList=productService.selectFsStoreProductTuiQuery(10);
        return R.ok().put("data", productList);
    }
    @ApiOperation("获取喜欢商品列表")
    @GetMapping("/getGoodsProduct")
    public R getGoodsProduct(){
        List<FsStoreProductListQueryVO> productList=productService.selectFsStoreProductGoodQuery(10);
        return R.ok().put("data", productList);
    }
    @ApiOperation("获取推荐商品数据")
    @GetMapping("/getTuiProducts")
    public R getTuiProducts(BaseQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<FsStoreProductListQueryVO> list=productService.selectFsStoreProductTuiListQuery();
        PageInfo<FsStoreProductListQueryVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @ApiOperation("获取喜欢商品数据")
    @GetMapping("/getGoodsProducts")
    public R getGoodsProducts(BaseQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<FsStoreProductListQueryVO> list=productService.selectFsStoreProductGoodListQuery();
        PageInfo<FsStoreProductListQueryVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);

    }

    @ApiOperation("获取推广商品列表")
    @GetMapping("/getStoreProductAttrValueList")
    public R getStoreProductAttrValueList(FsStoreProductAttrValueQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<FsStoreProductAttrValueQueryVO> productList=attrValueService.selectStoreProductAttrValueListQuery(param);
        PageInfo<FsStoreProductAttrValueQueryVO> listPageInfo=new PageInfo<>(productList);
        return R.ok().put("data",listPageInfo);
    }



}
