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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.goods.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsLogic;
import com.gree.mall.manager.plus.entity.User;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(value = "商品列表", tags ={"商品列表"} )
@RequestMapping(value = "/goods", produces = "application/json; charset=utf-8")
public class GoodsController {
    @Resource
    GoodsLogic goodsLogic;


    @GetMapping("/count")
    @ApiOperation("分类统计")
    public ResponseHelper<GoodsTypeCount> count(HttpServletRequest request){
        GoodsTypeCount goodsTypeCount = goodsLogic.countByType(request);
        return ResponseHelper.success(goodsTypeCount);
    }

    @GetMapping("/list/page")
    @ApiOperation(value = "商品列表")
    public ResponseHelper<IPage<GoodsSpecBean>> page(
            @ApiParam(value = "商品标志 S=出售中 O=售罄 W=仓库中 A=活动中") @RequestParam(required = false) String flag,
            @ApiParam(value = "商品关键字") @RequestParam(required = false) String keyword,
            @ApiParam(value = "销售价格开始区间") @RequestParam(required = false) BigDecimal startPrice,
            @ApiParam(value = "销售价格结束区间") @RequestParam(required = false) BigDecimal endPrice,
            @ApiParam(value = "分销金额开始区间") @RequestParam(required = false) BigDecimal startShare,
            @ApiParam(value = "分销金额开始区间") @RequestParam(required = false) BigDecimal endShare,
            @ApiParam(value = "排序json格式[{sortColumn: 'PRICE', type: true },...] " +
                    "sortColumn:商品排序对应值 PRICE=销售价格 SHARE=分销金额 SOLD=销量 STOCK=库存, " +
                    "type: true=升序 false=倒序")
            @RequestParam(required = false) String sortJson,
            @ApiParam(value = "商品分类 传入商品小类") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "商品状态 true:上架  false:下架") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "商品类型：COMMON=普通商品 PACKAGE=套购商品,不传默认为普通商品,支持传多个",required = false)
            @RequestParam(required = false) List<String> goodsTypes,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        IPage<GoodsSpecBean> goodsSpecBeanIPage = goodsLogic.page(flag, keyword, startPrice, endPrice,
                startShare, endShare, sortJson, categoryId, status,goodsTypes, pageNum, pageSize , request);
        return ResponseHelper.success(goodsSpecBeanIPage);
    }

    @GetMapping("/list/export")
    @ApiOperation("导出excel")
    public void downloadFile (HttpServletRequest request, HttpServletResponse response,
                              @ApiParam(value = "商品标志 S=出售中 O=售罄 W=仓库中 A=活动中") @RequestParam(required = false) String flag,
                              @ApiParam(value = "商品关键字") @RequestParam(required = false) String keyword,
                              @ApiParam(value = "销售价格开始区间") @RequestParam(required = false) BigDecimal startPrice,
                              @ApiParam(value = "销售价格结束区间") @RequestParam(required = false) BigDecimal endPrice,
                              @ApiParam(value = "分销金额开始区间") @RequestParam(required = false) BigDecimal startShare,
                              @ApiParam(value = "分销金额开始区间") @RequestParam(required = false) BigDecimal endShare,
                              @ApiParam(value = "排序json格式[{sortColumn: 'PRICE', type: true },...] " +
                                      "sortColumn:商品排序对应值 PRICE=销售价格 SHARE=分销金额 SOLD=销量 STOCK=库存, " +
                                      "type: true=升序 false=倒序")
                                  @RequestParam(required = false) String sortJson,
                              @ApiParam(value = "商品分类 传入商品小类") @RequestParam(required = false) String categoryId,
                              @ApiParam(value = "商品状态 true:上架  false:下架") @RequestParam(required = false) Boolean status,
                              @ApiParam(value = "商品类型：COMMON=普通商品 PACKAGE=套购商品,不传默认为普通商品,支持传多个",required = false) @RequestParam(required = false) List<String> goodsTypes
    ) throws Exception {
        goodsLogic.export( request,  response, flag, keyword, startPrice, endPrice,
                startShare, endShare, sortJson, categoryId, status,goodsTypes);
    }


    @GetMapping("/package/list")
    @ApiOperation(value = "套餐商品-列表")
    public ResponseHelper<IPage<GoodsPackageList>> packageList(
            @ApiParam(value = "商品关键字") @RequestParam(required = false) String keyword,
            @ApiParam(value = "销售价格开始区间") @RequestParam(required = false) BigDecimal startPrice,
            @ApiParam(value = "销售价格结束区间") @RequestParam(required = false) BigDecimal endPrice,
            @ApiParam(value = "分销金额开始区间") @RequestParam(required = false) BigDecimal startShare,
            @ApiParam(value = "分销金额开始区间") @RequestParam(required = false) BigDecimal endShare,
            @ApiParam(value = "排序json格式[{sortColumn: 'PRICE', type: true },...] " +
                    "sortColumn:商品排序对应值 PRICE=销售价格 SHARE=分销金额 SOLD=销量 STOCK=库存, " +
                    "type: true=升序 false=倒序")
            @RequestParam(required = false) String sortJson,
            @ApiParam(value = "商品分类 传入商品小类") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "商品状态 true:上架  false:下架") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        IPage<GoodsPackageList> goodsPackageListIPage = goodsLogic.packageList(keyword, startPrice, endPrice,
                startShare, endShare, sortJson, categoryId, status, pageNum, pageSize, request);
        return ResponseHelper.success(goodsPackageListIPage);
    }


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "商品列表V2")
    public ResponseHelper<IPage<GoodsVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<GoodsVO> adminDeptVOIPage = goodsLogic.goodsList(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(adminDeptVOIPage, new TypeReference<GoodsVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "商品列表V2导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<GoodsVO> adminDeptVOIPage = goodsLogic.goodsList(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(adminDeptVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }




    @GetMapping("/listbycategory")
    @ApiOperation(value = "商品详情")
    public ResponseHelper<List<GoodsSpecBean>> listbycategory(
            @ApiParam(value = "商品分类 传入商品小类") @RequestParam(required = false) List<String> categoryIds,
            HttpServletRequest request
    ) throws RemoteServiceException {
        List<GoodsSpecBean> listbycategory = goodsLogic.listbycategory(categoryIds,request);
        return ResponseHelper.success(listbycategory);
    }

    @PostMapping("/batch/category/update")
    @ApiOperation("批量修改分类")
    public ResponseHelper batchUpdate(@RequestBody GoodsCategoryUpdateBean goodsCategoryUpdateBean){
        goodsLogic.updateCategory(goodsCategoryUpdateBean);
        return ResponseHelper.success();
    }


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


    @GetMapping("/package/poplist")
    @ApiOperation("套购商品-配置商品列表")
    public ResponseHelper<List<GoodsPackageBean>> popList(
            @ApiParam(value = "商品id",required = true) @RequestParam String goodsId,
            @ApiParam(value = "第几个商品框",required = true) @RequestParam Integer type
    ) throws RemoteServiceException {
        List<GoodsPackageBean> goodsPackageBeans = goodsLogic.popList(goodsId, type);
        return ResponseHelper.success(goodsPackageBeans);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增商品")
    public ResponseHelper add(
            @ApiParam(value = "object",required = true) @RequestBody GoodsBean goodsBean,HttpServletRequest request)
            throws RemoteServiceException {
        goodsLogic.add(goodsBean,request);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改商品")
    public ResponseHelper update(
            @ApiParam(value = "object",required = true) @RequestBody GoodsBean goodsBean,HttpServletRequest request)
            throws RemoteServiceException {
        goodsLogic.update(goodsBean,request);
        return ResponseHelper.success();
    }

    @PostMapping("/status/update")
    @ApiOperation(value = "商品上下架")
    public ResponseHelper change(
            @ApiParam(value = "商品id",required = true) @RequestParam String goodsId,
            @ApiParam(value = "商品状态:true上架 false下架",required = true) @RequestParam Boolean status
    ) throws RemoteServiceException {
        goodsLogic.updateStatus(goodsId, status);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除商品")
    public ResponseHelper delete(
            @ApiParam(value = "商品id",required = true) @RequestParam String goodsId
    ) throws RemoteServiceException {
        goodsLogic.delete(goodsId);
        return ResponseHelper.success();
    }
    @PostMapping("/clone")
    @ApiOperation(value = "克隆商品")
    public ResponseHelper clone(
            @ApiParam(value = "商品id",required = true) @RequestParam String goodsId
    ) throws RemoteServiceException {
        return ResponseHelper.success(goodsLogic.clone(goodsId));
    }


    @PostMapping("/goods/off/check")
    @ApiOperation(value = "下架商品前,检测是否秒杀商品")
    public ResponseHelper SecCheck(
            @ApiParam(value = "商品id",required = true) @RequestParam String goodsId)
            throws RemoteServiceException {
        return ResponseHelper.success(goodsLogic.secCheck(goodsId));
    }

    @PostMapping("/goods/off/check/batch")
    @ApiOperation(value = "下架商品前,批量检测是否秒杀商品,返回秒杀的商品id列表")
    public ResponseHelper SecCheckBatch(
            @ApiParam(value = "商品id",required = true) @RequestBody List<String> goodsIdList)
            throws RemoteServiceException {
        return ResponseHelper.success(goodsLogic.secCheckBatch(goodsIdList));
    }



    @PostMapping("/goods/on")
    @ApiOperation(value = "批量上架商品")
    public ResponseHelper goodsOn(
            @ApiParam(value = "object",required = true) @RequestBody List<String> goodsIdList)
            throws RemoteServiceException {
        goodsLogic.goodsOnOff(goodsIdList,true);
        return ResponseHelper.success();
    }

    @PostMapping("/goods/off")
    @ApiOperation(value = "批量下架商品")
    public ResponseHelper goodsOf(
            @ApiParam(value = "object",required = true) @RequestBody List<String> goodsIdList)
            throws RemoteServiceException {
        goodsLogic.goodsOnOff(goodsIdList,false);
        return ResponseHelper.success();
    }

    @PostMapping("/goods/template")
    @ApiOperation(value = "批量修改商品模板")
    public ResponseHelper goodsTemplate(
            @ApiParam(value = "object",required = true) @RequestBody GoodsTemplateBean goodsTemplateBean,HttpServletRequest request)
            throws RemoteServiceException {
        goodsLogic.goodsTemplate(goodsTemplateBean,request);
        return ResponseHelper.success();
    }

    @PostMapping("/goods/batch/freight")
    @ApiOperation(value = "批量修改运费模板")
    public ResponseHelper goodsFreight(
            @ApiParam(value = "object",required = true) @RequestBody GoodsFreightBean goodsFreightBean)
            throws RemoteServiceException {
        goodsLogic.goodsFreight(goodsFreightBean);
        return ResponseHelper.success();
    }

    //@ApiOperation(" 用 商品规格的 分销金额 覆盖其 为null的 内部分销金额")
    //@PostMapping("/syncGoodsSpecInnerShareMsg")
    public ResponseHelper<Void> syncGoodsSpecInnerShareMsg(HttpServletRequest request){
        goodsLogic.syncGoodsSpecInnerShareMsg(request);
        return ResponseHelper.success();
    }

    @GetMapping("/package/user")
    @ApiOperation(value = "套购商品-指定人员列表")
    public ResponseHelper<List<User>> packageUserList(
            @ApiParam(value = "商品id",required = true) @RequestParam(required = true) String goodsId,
            @ApiParam(value = "商品id",required = false) @RequestParam(required = false) String mobile
    ) throws Exception {
        List<User> packageUseUserList = goodsLogic.getPackageUseUserList(goodsId, mobile);
        return ResponseHelper.success(packageUseUserList);
    }


    @PostMapping("/package/import")
    @ApiOperation(value = "套购商品-导入指定人员")
    public ResponseHelper<List<User>> packageUserList(
            MultipartFile multipartFile
    ) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(multipartFile);
        List<User> users = goodsLogic.importPackageUser(objects);
        return ResponseHelper.success(users);
    }


    @ApiNotAuth
    @ApiOperation("商品列表-外部")
    @PostMapping("/outside/list")
    public ResponseHelper<List<GoodsBean>> outsideList(
            @RequestParam Date time,
            @RequestParam String sign
    ){
        if(!StringUtils.equals(sign,"asfo&ndEdl1dfx(dk123xxckk@fx1kas#xdfA21")){
            throw new RemoteServiceException("签名检验不通过");
        }
        List<GoodsBean> goodsBeans = goodsLogic.outsideList(time);
        return ResponseHelper.success(goodsBeans);
    }

    @ApiOperation("复制分类商品信息")
    @GetMapping("/copy/categoryAndGoods")
    public ResponseHelper categoryAndGoods(
            @RequestParam String sourceCompanyId,
            @RequestParam String targetCompanyId,
            @RequestParam String targetCompanyName
    ){
        goodsLogic.categoryAndGoods(sourceCompanyId, targetCompanyId, targetCompanyName);
        return ResponseHelper.success();
    }

    @ApiOperation("复制运费信息")
    @GetMapping("/copy/freightTemplate")
    public ResponseHelper copyFreightTemplate(
            @RequestParam String sourceCompanyId,
            @RequestParam String targetCompanyId,
            @RequestParam String targetCompanyName
    ){
        goodsLogic.copyFreightTemplate(sourceCompanyId, targetCompanyId, targetCompanyName);
        return ResponseHelper.success();
    }
}
