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

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.goods.GoodsCategoryBean;
import com.gree.mall.manager.bean.trade.GoodsCategoryItemVO;
import com.gree.mall.manager.bean.trade.GoodsCategoryOtherItemVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsCategoryLogic;
import com.gree.mall.manager.logic.trade.TradeLogic;
import com.gree.mall.manager.plus.entity.GoodsCategory;
import com.gree.mall.manager.plus.entity.GoodsCategoryItem;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@Api(value = "以旧换新配置", tags = {"以旧换新配置API"})
@RequestMapping(value = "/trade", produces = "application/json; charset=utf-8")
public class TradeController {

    @Autowired
    TradeLogic tradeLogic;

    @Autowired
    GoodsCategoryLogic goodsCategoryLogic;



    @GetMapping("/list")
    @ApiOperation(value = "分类管理列表")
    public ResponseHelper<List<GoodsCategoryBean>> page(
            HttpServletRequest request
    ) throws RemoteServiceException {
        List<GoodsCategoryBean> list = tradeLogic.list(request);
        return ResponseHelper.success(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增分类")
    public ResponseHelper add(
            @ApiParam(value = "object",required = true) @RequestBody GoodsCategory goodsCategory,HttpServletRequest request) {
        tradeLogic.add(goodsCategory,request);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改分类")
    public ResponseHelper update(
            @ApiParam(value = "object",required = true) @RequestBody GoodsCategory goodsCategory) {
        goodsCategoryLogic.update(goodsCategory);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除分类")
    public ResponseHelper delete(
            @ApiParam(value = "分类id",required = true) @RequestParam String categoryId
    ) {
        goodsCategoryLogic.delete(categoryId);
        return ResponseHelper.success();
    }

    @GetMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<GoodsCategory> detail(
            @ApiParam(value = "分类id",required = true) @RequestParam String categoryId
    ){
        GoodsCategory detail = goodsCategoryLogic.detail(categoryId);
        return ResponseHelper.success(detail);
    }


    @ZfireList
    @PostMapping("/list/page")
    @ApiOperation(value = "属性item型号列表")
    public ResponseHelper<IPage<GoodsCategoryItemVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<GoodsCategoryItemVO> page = tradeLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsCategoryItemVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "属性item型号列表导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<GoodsCategoryItemVO> page = tradeLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/listOther/page")
    @ApiOperation(value = "属性item其他列表")
    public ResponseHelper<IPage<GoodsCategoryOtherItemVO>> listOther(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<GoodsCategoryOtherItemVO> page = tradeLogic.listOther(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsCategoryOtherItemVO>() {});
    }

    @PostMapping("/pageOtherExport")
    @ApiOperation(value = "属性item其他列表导出")
    public void pageOtherExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<GoodsCategoryOtherItemVO> page = tradeLogic.listOther(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/addItem")
    @ApiOperation(value = "新增分类")
    public ResponseHelper addItem(
            @ApiParam(value = "object",required = true) @RequestBody GoodsCategoryItem goodsCategoryItem, HttpServletRequest request) {
        tradeLogic.addItem(goodsCategoryItem,request);
        return ResponseHelper.success();
    }

    @PostMapping("/updateItem")
    @ApiOperation(value = "修改分类")
    public ResponseHelper updateItem(
            @ApiParam(value = "object",required = true) @RequestBody GoodsCategoryItem goodsCategoryItem) {
        tradeLogic.updateItem(goodsCategoryItem);
        return ResponseHelper.success();
    }

    @PostMapping("/deleteItem")
    @ApiOperation(value = "删除分类")
    public ResponseHelper deleteItem(
            @ApiParam(value = "分类Itemid",required = true) @RequestParam String categoryId
    ) {
        tradeLogic.deleteItem(categoryId);
        return ResponseHelper.success();
    }

    @GetMapping("/detailItem")
    @ApiOperation("详情")
    public ResponseHelper<GoodsCategoryItem> detailItem(
            @ApiParam(value = "分类Itemid",required = true) @RequestParam String categoryItemId
    ){
        GoodsCategoryItem detail = tradeLogic.detailItem(categoryItemId);
        return ResponseHelper.success(detail);
    }


}
