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.ZfireList;
import com.gree.mall.manager.bean.goods.GoodsMaterialBean;
import com.gree.mall.manager.bean.goods.GoodsMaterialItemSendVO;
import com.gree.mall.manager.bean.goods.GoodsMaterialItemVO;
import com.gree.mall.manager.bean.goods.GoodsMaterialVO;
import com.gree.mall.manager.enums.material.StateEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsMaterialLogic;
import com.gree.mall.manager.zfire.bean.WebsitGoodsParamBean;
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 = "商品物料API", tags ={"商品物料API"} )
@RequestMapping(value = "/goods/material", produces = "application/json; charset=utf-8")
public class GoodsMaterialController {

    @Resource
    GoodsMaterialLogic goodsMaterialLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "商品物料-列表")
    public ResponseHelper<IPage<GoodsMaterialVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws Exception {
        IPage<GoodsMaterialVO> page = goodsMaterialLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsMaterialVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("商品物料-导出")
    public void listExport(
            @RequestBody WebsitGoodsParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<GoodsMaterialVO> baseVOIPage = goodsMaterialLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "商品物料-详情")
    public ResponseHelper<GoodsMaterialBean> detail(
            @ApiParam(value = "id",required = true) @RequestParam String id
    ) throws RemoteServiceException {
        GoodsMaterialBean detail = goodsMaterialLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/add")
    @ApiOperation(value = "商品物料-新增商品")
    public ResponseHelper add(
            @ApiParam(value = "object",required = true) @RequestBody GoodsMaterialBean goodsBean
    ) throws Exception {
        goodsMaterialLogic.add(goodsBean);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "商品物料-修改商品")
    public ResponseHelper update(
            @ApiParam(value = "object",required = true) @RequestBody GoodsMaterialBean goodsBean
    ) throws Exception {
        goodsMaterialLogic.update(goodsBean);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/update/status")
    @ApiOperation(value = "商品物料-批量更新状态")
    public ResponseHelper batchUpdateStatus(
            @ApiParam(value = "goodsId", required = true) @RequestParam List<String> ids,
            @ApiParam(value = "ON=有效 OFF=无效", required = true) @RequestParam StateEnum stateEnum
    ) throws Exception {
        goodsMaterialLogic.batchUpdateStatus(ids, stateEnum);
        return ResponseHelper.success();
    }



    @PostMapping("/item/count")
    @ApiOperation(value = "线下销售订单-选择物料明细")
    public ResponseHelper<IPage<GoodsMaterialItemVO>> itemCount(
            @ApiParam(value = "规格型号", required = false) @RequestParam(required = false) String specsName,
            @ApiParam(value = "商品名称", required = false) @RequestParam(required = false) String goodsMaterialName,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) throws Exception {
        IPage<GoodsMaterialItemVO> goodsMaterialItemVOIPage = goodsMaterialLogic.queryGoodsMaterialItem(specsName,goodsMaterialName, pageNum, pageSize);
        return ResponseHelper.success(goodsMaterialItemVOIPage);
    }


    @PostMapping("/item/list")
    @ApiOperation(value = "商品物料明细")
    public ResponseHelper<Page<GoodsMaterialItemSendVO>> itemList(
            @ApiParam(value = "规格型号", required = true) @RequestParam(required = true) String specsName,
            @ApiParam(value = "物料类型 INSIDE=内机 OUT=外机 PARTS=配件", required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "物料名称", required = false) @RequestParam(required = false) String name,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) throws Exception {
        Page<GoodsMaterialItemSendVO> goodsMaterialItemVOIPage = goodsMaterialLogic.itemList(specsName, type, name,pageNum,pageSize);
        return ResponseHelper.success(goodsMaterialItemVOIPage);
    }


}
