package com.zfire.mall.manager.controller.k3;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.k3.KingDeeMaterialBean;
import com.zfire.mall.manager.bean.listV2.K3CustomerVO;
import com.zfire.mall.manager.bean.listV2.K3MaterialVO;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3MaterialLogic;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.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;

@Slf4j
@RestController
@Api(value = "金蝶物料KPI", tags ={"金蝶物料KPI"} )
@RequestMapping(value = "/k3/material", produces = "application/json; charset=utf-8")
public class K3MaterialController {

    @Autowired
    K3MaterialLogic k3MaterialLogic;




    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation(value = "物料列表V2")
    public ResponseHelper<K3MaterialVO> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<K3MaterialVO> page = k3MaterialLogic.listV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<K3MaterialVO>() {});
    }

    @PostMapping("/export/v2")
    @ApiOperation("物料列表V2-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<K3MaterialVO> stockBeanIPage = k3MaterialLogic.listV2(zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(),zfireParam.getExportFields(),request,response);
    }





    @ApiOperation(value = "物料列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<KingDeeMaterialBean>> list(
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String number,
            @ApiParam(required = false, value = "厂产品编码") @RequestParam(required = false) String oldNumber,
            @ApiParam(required = false, value = "搜索内容") @RequestParam(required = false) String keyword,
            @ApiParam(required = false, value = "机型型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "绑定供应链品类的物料 true/false") @RequestParam(required = false) Boolean bindMain,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<KingDeeMaterialBean> list = k3MaterialLogic.list(number, oldNumber, keyword,specification,bindMain, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @ApiOperation(value = "物料详情")
    @GetMapping("/detail")
    public ResponseHelper<KingDeeMaterialBean> detail(
            @ApiParam(required = true) @RequestParam String id
    ) throws Exception {
        KingDeeMaterialBean list = k3MaterialLogic.detail(id);
        return ResponseHelper.success(list);
    }

    @ApiOperation(value = "编辑物料")
    @PostMapping("/edit")
    public ResponseHelper edit(
            @ApiParam(required = true, value = "物料") @RequestBody KingDeeMaterialBean kingDeeMaterialBean
    ) throws Exception {
        k3MaterialLogic.edit(kingDeeMaterialBean);
        return ResponseHelper.success();
    }
}
