package com.gree.mall.miniapp.controller.goods;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.annotation.ApiNotAuth;
import com.gree.mall.miniapp.bean.goods.*;
import com.gree.mall.miniapp.bean.user.UserWxBean;
import com.gree.mall.miniapp.enums.QrCodeEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.common.outside.WechatLogic;
import com.gree.mall.miniapp.logic.goods.CategoryLogic;
import com.gree.mall.miniapp.logic.goods.GoodsLogic;
import com.gree.mall.miniapp.bean.goods.GoodsBean;
import com.gree.mall.miniapp.logic.user.UserLogic;
import com.gree.mall.miniapp.plus.entity.Brand;
import com.gree.mall.miniapp.plus.entity.Goods;
import com.gree.mall.miniapp.plus.entity.GoodsCategory;
import com.gree.mall.miniapp.utils.oss.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@Api(value = "商品列表", tags ={"商品列表"} )
@RequestMapping(value = "/goods", produces = "application/json; charset=utf-8")
public class GoodsController {
    @Resource
    GoodsLogic goodsLogic;
    @Autowired
    UserLogic userLogic;
    @Autowired
    CategoryLogic categoryLogic;
    @Autowired
    WechatLogic wechatLogic;
    @Autowired
    OSSUtil ossUtil;


    @GetMapping("/category/list/all")
    @ApiOperation(value = "分类列表(树形)")
    public ResponseHelper<List<CategoryBean>> category(
            HttpServletRequest request,
            @ApiParam(value = "是否显示在服务页 true/false",required = false) @RequestParam(required = false) Boolean isServiceShow
    ) throws Exception {
        List<CategoryBean> categoryBeans = categoryLogic.queryAll(request,isServiceShow);
        return ResponseHelper.success(categoryBeans);
    }

    @GetMapping("/category/list/first")
    @ApiOperation(value = "获取一级分类列表")
    public ResponseHelper<List<GoodsCategory>> categoryFirst(
            HttpServletRequest request,
            @ApiParam(value = "是否显示在服务页",required = false) @RequestParam(required = false) Boolean isServiceShow) throws Exception {
        List<GoodsCategory> categoryBeans = categoryLogic.queryFirst(request,isServiceShow);
        return ResponseHelper.success(categoryBeans);
    }

    @GetMapping("/category/list/second")
    @ApiOperation(value = "获取二级分类列表")
    public ResponseHelper<List<GoodsCategory>> categorySecond(
            HttpServletRequest request,
            @ApiParam(value = "一级分类id",required = true) @RequestParam(required = true) String categoryId
    ) throws Exception {
        List<GoodsCategory> categoryBeans = categoryLogic.querySecond(request , categoryId);
        return ResponseHelper.success(categoryBeans);
    }


    @ApiNotAuth
    @GetMapping("/category/list")
    @ApiOperation(value = "分类列表")
    public ResponseHelper<List<GoodsCategory>> category(
            HttpServletRequest request,
            @ApiParam(value = "分类id",required = false) @RequestParam(required = false) String parentId,
            @ApiParam(value = "类型 1=商品分类 2=服务分类",required = false) @RequestParam(required = false) Integer type,
            @ApiParam(value = "分类名称",required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "是否只查询以旧换新的分类",required = false) @RequestParam(required = false) Boolean yjhx
    ) throws Exception {
        List<GoodsCategory> list = categoryLogic.list(request,parentId,type,name,yjhx);
        return ResponseHelper.success(list);
    }

    @GetMapping("/category/brand")
    @ApiOperation(value = "品牌列表-根据一级分类查询")
    public ResponseHelper<List<Brand>> brand(
            @ApiParam(value = "一级分类id",required = false) @RequestParam(required = false) String categoryId
    ) throws Exception {
        //List<CategoryBean> categoryBeans = categoryLogic.queryAll(request);
        List<Brand> brands = categoryLogic.brandList(categoryId);
        return ResponseHelper.success(brands);
    }

    @GetMapping("/list/level1")
    @ApiOperation(value = "根据一级分类查询商品列表")
    public ResponseHelper<IPage<Goods>> listByLevel1(
            HttpServletRequest request,
            @ApiParam(value = "一级分类id",required = true) @RequestParam(required = true) String categoryId,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<Goods> categoryGoodBeans = categoryLogic.categoryGoodsList(request,categoryId,pageNum,pageSize);
        return ResponseHelper.success(categoryGoodBeans);
    }

    @GetMapping("/list/page")
    @ApiOperation(value = "商品列表")
    public ResponseHelper<IPage<GoodsNewBean>> page(
            HttpServletRequest request,
            @ApiParam(value = "品牌id",required = false) @RequestParam(required = false) List<String> brandId,
            @ApiParam(value = "最低价",required = false) @RequestParam(required = false) BigDecimal minPrice,
            @ApiParam(value = "最高价",required = false) @RequestParam(required = false) BigDecimal maxPrice,
            @ApiParam(value = "商品标签",required = false) @RequestParam(required = false) List<String> tag,
            @ApiParam(value = "商品关键字") @RequestParam(required = false) String keyword,
            @ApiParam(value = "排序 0=综合 1=销量 2=价格升 3=价格降 4=上新") @RequestParam(required = false,defaultValue = "0") Integer sort,
            @ApiParam(value = "分类id") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "是否只查询以旧换新的商品",required = false) @RequestParam(required = false) Boolean yjhx,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<GoodsNewBean> page = goodsLogic.page(request,brandId,minPrice,maxPrice,tag,keyword,categoryId,sort,yjhx, pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @GetMapping("/list/sort/page")
    @ApiOperation(value = "商品列表")
    public ResponseHelper<IPage<GoodsNewBean>> page1(
            HttpServletRequest request,
            @ApiParam(value = "品牌id",required = false) @RequestParam(required = false) List<String> brandId,
            @ApiParam(value = "最低价",required = false) @RequestParam(required = false) BigDecimal minPrice,
            @ApiParam(value = "最高价",required = false) @RequestParam(required = false) BigDecimal maxPrice,
            @ApiParam(value = "商品标签",required = false) @RequestParam(required = false) List<String> tag,
            @ApiParam(value = "商品关键字") @RequestParam(required = false) String keyword,
            @ApiParam(value = "排序 0=综合 1=销量 2=价格升 3=价格降 4=上新") @RequestParam(required = false,defaultValue = "0") Integer sort,
            @ApiParam(value = "分类id") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "是否只查询以旧换新的商品",required = false) @RequestParam(required = false) Boolean yjhx,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<GoodsNewBean> page = goodsLogic.page(request,brandId,minPrice,maxPrice,tag,keyword,categoryId,sort, yjhx,pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @ApiNotAuth
    @GetMapping("/detail")
    @ApiOperation(value = "商品详情")
    public ResponseHelper<GoodsBean> detail(
            HttpServletRequest request,
            @ApiParam(value = "id",required = true) @RequestParam String goodsId,
            @ApiParam(value = "userId",required = false) @RequestParam(required = false) String userId,
            @ApiParam(value = "wxSceneId",required = false) @RequestParam(required = false) String wxSceneId
    ) throws RemoteServiceException {
        GoodsBean detail = goodsLogic.detail(request, goodsId,userId,wxSceneId);
        return ResponseHelper.success(detail);
    }

//    @GetMapping("/qrcode")
//    @ApiOperation("获取商品二维码")
//    public ResponseHelper<String> qrcode(
//            @ApiParam(value = "用户id",required = true) @RequestParam String userId,
//            @ApiParam(value = "商品id",required = true) @RequestParam String goodsId,
//            HttpServletRequest request
//    ) throws IOException, RemoteServiceException {
//        UserWxBean userWxBean = userLogic.userDetail(userId,request);
//        String qrcode = wechatLogic.getQrcode(QrCodeEnum.GOODS.toString().toLowerCase(),goodsId,userId,userWxBean.getCompanyWechatId());
//        return ResponseHelper.success(qrcode);
//    }

    @GetMapping("/qrcode")
    @ApiOperation("获取商品二维码")
    public ResponseHelper<String> qrcode(
            @ApiParam(value = "url",required = true) @RequestParam String url
    ) throws IOException, RemoteServiceException {

        byte[] bytes = QrCodeUtil.generatePng(url, 300, 300);

        long currentTime = System.currentTimeMillis();
        String filePath = ossUtil.getFilePath() + "/" + currentTime + UUID.randomUUID() + ".png";
        ossUtil.uploadFile(filePath, bytes);
        return ResponseHelper.success(ossUtil.getAccessUrl() + filePath);
    }



    @GetMapping("/promotion/group/list")
    @ApiOperation("团购商品列表")
    public ResponseHelper<IPage<PromotionGoodsBean>> grouplist(
            @ApiParam(value = "用户id",required = true) @RequestParam String userId,
            @ApiParam(value = "商品类型id",required = false) @RequestParam(required = false) String goodsCategoryId,
            @ApiParam(value = "搜索内容",required = false) @RequestParam(required = false) String keyword,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNo,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) throws IOException, RemoteServiceException {
        IPage<PromotionGoodsBean> promotionGoodsBeanIPage = goodsLogic.myGroupGoods(userId, goodsCategoryId,keyword, pageNo, pageSize,request);
        return ResponseHelper.success(promotionGoodsBeanIPage);
    }

    @GetMapping("/promotion/share/qrcode")
    @ApiOperation("团购活动分享二维码")
    public ResponseHelper<PromotionShareQrCode> shareQrcode(
            @ApiParam(value = "用户id",required = true) @RequestParam String userId,
            HttpServletRequest request
    ) throws IOException {
        PromotionShareQrCode promotionShareQrCode = goodsLogic.queryShareQrcode(userId,request);
        return ResponseHelper.success(promotionShareQrCode);
    }


//    @PostMapping("/cut")
//    @ApiOperation("让利二维码或链接")
//    public ResponseHelper<String> cut(
//            @RequestBody List<CutGoodsBean> cutGoodsBeans,
//            HttpServletRequest request
//    ) throws IOException {
//        String cutQrcode = goodsLogic.getCutQrcode(cutGoodsBeans, request);
//        return ResponseHelper.success(cutQrcode);
//    }

    @GetMapping("/package/choice")
    @ApiOperation("团购商品-选购列表")
    public ResponseHelper<List<List<GoodsPackageBean>>> choicePackage(
            @ApiParam(value = "套购商品id",required = true) @RequestParam String goodsId,
            HttpServletRequest request
    ) throws IOException {
        List<List<GoodsPackageBean>> lists = goodsLogic.choicePackage(goodsId, request);
        return ResponseHelper.success(lists);
    }


}
