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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.CommonTemplateVO;
import com.gree.mall.manager.bean.goods.*;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsNewsCategoryLogic;
import com.gree.mall.manager.plus.entity.GoodsNewsCategory;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "小程序首页活动", tags = {"首页活动"})
@RequestMapping(value = "/goods/news/category", produces = "application/json; charset=utf-8")
public class GoodsNewsCategoryController {

    @Autowired
    private GoodsNewsCategoryLogic goodsNewsCategoryLogic;

    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "分页获取商品新维度分类列表V2")
    public ResponseHelper<IPage<GoodsNewsCategoryVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<GoodsNewsCategoryVO> page = goodsNewsCategoryLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsNewsCategoryVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "分页获取商品新维度分类列表V2导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<GoodsNewsCategoryVO> page = goodsNewsCategoryLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }





    @ApiOperation(value = "分页获取商品新维度分类")
    @GetMapping("/page")
    public ResponseHelper<IPage<GoodsNewsCategory>> pageGoodsNewsCategory(
            HttpServletRequest request,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小(-1不分页)",required = true)@RequestParam Integer pageSize
    ){

        return ResponseHelper.success(goodsNewsCategoryLogic.pageGoodsNewsCategory(request, pageNum, pageSize));
    }


    @ApiOperation(value = "添加新维度分类")
    @PostMapping("/addGoodsNewsCategory")
    public ResponseHelper<GoodsNewsCategory> addGoodsNewsCategory(
            HttpServletRequest request,
            @ApiParam(value = "主标题",required = true)@RequestParam String mainTitle,
            @ApiParam(value = "副标题",required = true)@RequestParam String subTitle,
            @ApiParam(value = "1=普通商品 2=套购商品",required = true)@RequestParam int type,
            @ApiParam(value = "排序整数",required = true)@RequestParam Integer sortNum
    ){
        return ResponseHelper.success(goodsNewsCategoryLogic.addGoodsNewsCategory(request, mainTitle, subTitle,type,sortNum));
    }


    @ApiOperation(value = "编辑新维度分类")
    @PostMapping("/vimGoodsNewsCategory")
    public ResponseHelper vimGoodsNewsCategory(HttpServletRequest request, @RequestBody GoodsNewsCategoryVimBean reqDto){
        goodsNewsCategoryLogic.vimGoodsNewsCategory(request, reqDto);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "关闭或开启新维度分类")
    @PostMapping("/editStatus")
    public ResponseHelper editStatus(HttpServletRequest request,  @ApiParam(value = "id",required = true)@RequestParam String goodsNewsCategoryId,
                                                   @ApiParam(value = "状态 true:开启,false:关闭",required = true)@RequestParam Boolean status){
        goodsNewsCategoryLogic.editStatus(goodsNewsCategoryId, status,request);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除新维度分类")
    @PostMapping("/delete")
    public ResponseHelper delete( HttpServletRequest request,@ApiParam(value = "id",required = true)@RequestParam String goodsNewsCategoryId){
        goodsNewsCategoryLogic.delete(goodsNewsCategoryId,request);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "新维度分类详情")
    @GetMapping("/detail")
    public ResponseHelper<GoodsNewsCategoryDetailBean> detail(@ApiParam(value = "新维度分类id",required = true)@RequestParam String newsCategoryId){

        return ResponseHelper.success(goodsNewsCategoryLogic.detail(newsCategoryId));
    }

    @ApiOperation(value = "添加活动商品")
    @PostMapping("/addActiveGoods")
    public ResponseHelper addActiveGoods(
            HttpServletRequest request,
            @RequestBody ActiveGoodsAddReqBean activeGoodsAddReqBean
    ) {
        goodsNewsCategoryLogic.addActiveGoods(request, activeGoodsAddReqBean);
        return ResponseHelper.success();
    }


    @ApiOperation(value = "获取活动商品详情")
    @GetMapping("/getGoodsActiveDetail")
    public ResponseHelper<GoodsActiveDetail> getGoodsActiveDetail(

            @RequestParam @ApiParam(value = "唯一id(type>=2可取小程序模板id)",required = true)String objectId,
            @RequestParam @ApiParam(
                    value = "类型: 1=新维度分类，2=首页弹窗， 3=活动专区1，4=活动专区2左侧，5=活动专区2右侧，6=专场专区1，7=专场专区2 ， 8=专场专区3 ， 9=专场专区4",
                    required = true
            )Integer type
    ) {

        return ResponseHelper.success(goodsNewsCategoryLogic.getGoodsActiveDetail(objectId, type));
    }


}
