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.goods.GoodsLibraryBean;
import com.gree.mall.manager.bean.goods.GoodsMaterialVO;
import com.gree.mall.manager.bean.listvo.goods.GoodsLibraryVO;
import com.gree.mall.manager.enums.StatusEnum;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsLibraryLogic;
import com.gree.mall.manager.plus.entity.GoodsLease;
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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 ={"商品库"} )
@RequiredArgsConstructor
@RequestMapping(value = "/goods/library", produces = "application/json; charset=utf-8")
public class GoodsLibraryController {

    private final GoodsLibraryLogic goodsLibraryLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<GoodsLibraryVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws Exception {
        IPage<GoodsLibraryVO> page = goodsLibraryLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<GoodsLibraryVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<GoodsLibraryVO> baseVOIPage = goodsLibraryLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<GoodsLibraryBean> detail(
            @ApiParam(value = "商品id") @RequestParam String goodsLibraryId
    ) throws Exception {
        GoodsLibraryBean detail = goodsLibraryLogic.detail(goodsLibraryId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增/编辑")
    public ResponseHelper save(
            @RequestBody GoodsLibraryBean goodsLibraryBean
    ) throws Exception {
        goodsLibraryLogic.save(goodsLibraryBean);
        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ResponseHelper del(
            @ApiParam(value = "商品id") @RequestParam String goodsLibraryId
    ) throws Exception {
        goodsLibraryLogic.delete(goodsLibraryId);
        return ResponseHelper.success();
    }

    @PostMapping("/status/batch")
    @ApiOperation(value = "批量改状态")
    public ResponseHelper del(
            @ApiParam(value = "商品id") @RequestParam List<String> goodsLibraryIds,
            @ApiParam(value = "状态 true/false") @RequestParam Boolean statusEnum
    ) throws Exception {
        goodsLibraryLogic.batchStatus(goodsLibraryIds,statusEnum);
        return ResponseHelper.success();
    }



    @PostMapping("/sync")
    @ApiOperation(value = "同步商品")
    public ResponseHelper sync() throws Exception {
        goodsLibraryLogic.sync();
        return ResponseHelper.success();
    }


}
