package com.gree.mall.manager.controller;

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.ZfireList;
import com.gree.mall.manager.bean.carouselMap.CarouselMapVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.CarouselMapLogic;
import com.gree.mall.manager.plus.entity.CarouselMap;
import com.gree.mall.manager.plus.entity.Goods;
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.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@Api(value = "轮播图管理", tags ={"轮播图管理"} )
@RequestMapping(value = "/carouselMap", produces = "application/json; charset=utf-8")
public class CarouselMapController {

    @Resource
    CarouselMapLogic carouselMapLogic;




    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "轮播图列表V2")
    public ResponseHelper<IPage<CarouselMapVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<CarouselMapVO> page = carouselMapLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<CarouselMapVO>() {});
    }

    @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<CarouselMapVO> page = carouselMapLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }




    @GetMapping("/list/page")
    @ApiOperation(value = "轮播图列表")
    public ResponseHelper<Page<CarouselMap>> page(
            HttpServletRequest request,
            @ApiParam(value = "状态(true：启用 false:禁用") @RequestParam(required = false) Boolean state,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<CarouselMap> page = carouselMapLogic.page(request, state,pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "轮播详情")
    public ResponseHelper<CarouselMap> detail(
            @ApiParam(value = "id",required = true) @RequestParam() String id
    ) throws RemoteServiceException {
        CarouselMap detail = carouselMapLogic.detail(id);
        return ResponseHelper.success(detail);
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增轮播图")
    public ResponseHelper add(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody CarouselMap carouselMapBean) {
        carouselMapLogic.add(request, carouselMapBean);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改轮播图")
    public ResponseHelper update(
            @ApiParam(value = "object",required = true) @RequestBody CarouselMap carouselMapBean) {
        carouselMapLogic.update(carouselMapBean);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除轮播图")
    public ResponseHelper delete(
            @ApiParam(value = "轮播图id",required = true) @RequestParam String carouselMapId
    ) {
        carouselMapLogic.delete(carouselMapId);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/delete")
    @ApiOperation(value = "删除轮播图")
    public ResponseHelper delete(
            @ApiParam(value = "轮播图id",required = true) @RequestBody List<String> ids
    ) {
        carouselMapLogic.delete(ids);
        return ResponseHelper.success();
    }


    @PostMapping("/state/update")
    @ApiOperation(value = "禁用/正常")
    public ResponseHelper change(
            @ApiParam(value = "轮播图id",required = true) @RequestParam String carouselMapId,
            @ApiParam(value = "状态:true上架 false下架",required = true) @RequestParam Boolean state
    ) {
        carouselMapLogic.updateState(carouselMapId, state);
        return ResponseHelper.success();
    }

    @GetMapping("/list/goods")
    @ApiOperation(value = "商品列表")
    public ResponseHelper<Page<Goods>> page(
            HttpServletRequest request,
            @ApiParam(value = "商品id") @RequestParam(required = false) String goodsId,
            @ApiParam(value = "商品名称") @RequestParam(required = false) String goodsName,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<Goods> page = carouselMapLogic.getGoods(request, goodsId, goodsName,pageNum, pageSize);
        return ResponseHelper.success(page);
    }
}
