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

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.front.CustomerCodeCustomerCheckVO;
import com.zfire.mall.manager.bean.front.CustomerCodeCustomerVO;
import com.zfire.mall.manager.bean.front.CustomerCodeVO;
import com.zfire.mall.manager.bean.listV2.StageK3CustomerVO;
import com.zfire.mall.manager.bean.manage.customer.CustomerApplyBean;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.customer.CustomerApplyLogic;
import com.zfire.mall.manager.logic.front.CustomerCodeLogic;
import com.zfire.mall.manager.plus.entity.CustomerApply;
import com.zfire.mall.manager.plus.entity.CustomerCode;
import com.zfire.mall.manager.utils.CommonUtils;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;

@Slf4j
@RestController
@Api(value = "嘉贤出库条码库API", tags ={"嘉贤出库条码库API"} )
@RequestMapping(value = "/customer/code", produces = "application/json; charset=utf-8")
public class CustomerCodeController {

    @Autowired
    CustomerCodeLogic customerCodeLogic;

    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation(value = "出库条码库")
    public ResponseHelper<CustomerCodeVO> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<CustomerCodeVO> k3CustomerVOIPage = customerCodeLogic.listV2(zfireParamBean);
        return ResponseHelper.success(k3CustomerVOIPage, new TypeReference<CustomerCodeVO>() {});
    }

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



    @ZfireList
    @PostMapping("/listCustomer/v2")
    @ApiOperation(value = "商家仓条码列表")
    public ResponseHelper<CustomerCodeCustomerVO> listCustomer(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<CustomerCodeCustomerVO> customerCodeCustomerVOIPage = customerCodeLogic.listCustomer(zfireParamBean);
        return ResponseHelper.success(customerCodeCustomerVOIPage, new TypeReference<CustomerCodeVO>() {});
    }

    @PostMapping("/listCustomer/export/v2")
    @ApiOperation("商家仓条码列表-导出")
    public void listCustomerExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerCodeCustomerVO> customerCodeCustomerVOIPage = customerCodeLogic.listCustomer(zfireParam);
        //3.导出
        FieldUtils.exportData(customerCodeCustomerVOIPage.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @GetMapping("/ShangJiaDownload")
    @ApiOperation("商家仓条码-下载模板")
    public void luBaoDownload(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        CommonUtils.downloadFile("商家仓导入.xlsx",response);
    }


    @PostMapping("/import")
    @ApiOperation("导入商家仓条码")
    public ResponseHelper importData(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request
    ) throws RemoteServiceException, IOException, ParseException {
        InputStream inputStream = file.getInputStream();
        customerCodeLogic.importData(inputStream,request);
        return ResponseHelper.success();
    }



    @ZfireList
    @PostMapping("/listCustomerCheck/v2")
    @ApiOperation(value = "条码校验列表")
    public ResponseHelper<CustomerCodeCustomerCheckVO> listCustomerCheck(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<CustomerCodeCustomerCheckVO> customerCodeCustomerVOIPage = customerCodeLogic.listCustomerCheck(zfireParamBean);
        return ResponseHelper.success(customerCodeCustomerVOIPage, new TypeReference<CustomerCodeCustomerCheckVO>() {});
    }

    @PostMapping("/listCustomerCheck/export/v2")
    @ApiOperation("条码校验列表-导出")
    public void listCustomerCheckExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerCodeCustomerCheckVO> customerCodeCustomerVOIPage = customerCodeLogic.listCustomerCheck(zfireParam);
        //3.导出
        FieldUtils.exportData(customerCodeCustomerVOIPage.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @GetMapping("/download")
    @ApiOperation("条码校验-下载模板")
    public void download(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        CommonUtils.downloadFile("条码核验.xlsx",response);
    }


    @PostMapping("/importCheck")
    @ApiOperation("导入条码校验")
    public ResponseHelper importCheck(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request
    ) throws RemoteServiceException, IOException, ParseException {
        InputStream inputStream = file.getInputStream();
        customerCodeLogic.importCheck(inputStream,request);
        return ResponseHelper.success();
    }


    @ZfireList
    @PostMapping("/listDaCang/v2")
    @ApiOperation(value = "大仓出库条码库")
    public ResponseHelper<CustomerCodeVO> listDaCang(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<CustomerCodeVO> k3CustomerVOIPage = customerCodeLogic.listDaCang(zfireParamBean);
        return ResponseHelper.success(k3CustomerVOIPage, new TypeReference<CustomerCodeVO>() {});
    }

    @PostMapping("/listDaCang/export/v2")
    @ApiOperation("大仓出库条码库-导出")
    public void listDaCangExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerCodeVO> stockBeanIPage = customerCodeLogic.listDaCang(zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(),zfireParam.getExportFields(),request,response);
    }


}
