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.ExcelData;
import com.zfire.mall.manager.bean.customer.CustomerProductTypeRelaBean;
import com.zfire.mall.manager.bean.listV2.CustomerProductTypeRelaVO;
import com.zfire.mall.manager.bean.listV2.K3CustomerVO;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.customer.CustomerEnginDepositLogic;
import com.zfire.mall.manager.logic.customer.CustomerProductTypeLogic;
import com.zfire.mall.manager.plus.entity.CustomerEnginDeposit;
import com.zfire.mall.manager.plus.entity.CustomerProductTypeRela;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
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.util.List;

@Slf4j
@RestController
@Api(value = "经销商客户存货分类关系", tags ={"经销商客户存货分类关系"} )
@RequestMapping(value = "/customer/pt", produces = "application/json; charset=utf-8")
public class CustomerProductTypeController {

    @Autowired
    CustomerProductTypeLogic customerProductTypeLogic;


    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation(value = "经销商客户存货分类列表V2")
    public ResponseHelper<CustomerProductTypeRelaVO> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ){
        IPage<CustomerProductTypeRelaVO> customerProductTypeRelaVOIPage = customerProductTypeLogic.listV2(zfireParamBean);
        return ResponseHelper.success(customerProductTypeRelaVOIPage, new TypeReference<CustomerProductTypeRelaVO>() {
        });
    }

    @PostMapping("/user/export/v2")
    @ApiOperation("经销商客户存货分类列表V2-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerProductTypeRelaVO> stockBeanIPage = customerProductTypeLogic.listV2(zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @GetMapping("/list")
    @ApiOperation(value = "经销商客户存货分类列表")
    public ResponseHelper<IPage<CustomerProductTypeRelaBean>> list(
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "钱包id") @RequestParam(required = false) String customerWalletId,
            @ApiParam(required = false, value = "钱包名称") @RequestParam(required = false) String customerWalletName,
            @ApiParam(required = true, value = "页号") @RequestParam(required = true) Integer pageNum,
            @ApiParam(required = true, value = "页大小") @RequestParam(required = true) Integer pageSize
    ){
        IPage<CustomerProductTypeRelaBean> list = customerProductTypeLogic.list(customerName, customerWalletId, customerWalletName, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/export")
    @ApiOperation(value = "经销商客户存货分类-导出")
    public void export(
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "钱包id") @RequestParam(required = false) String customerWalletId,
            @ApiParam(required = false, value = "钱包名称") @RequestParam(required = false) String customerWalletName,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<CustomerProductTypeRelaBean> list = customerProductTypeLogic.list(customerName, customerWalletId, customerWalletName, 1, -1);
        ExcelData excelData = customerProductTypeLogic.export(list.getRecords());
        ExcelUtils.exportExcel(request,response,"经销商钱包对应关系.xlsx",excelData);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "经销商客户存货分类-详情")
    public ResponseHelper<CustomerProductTypeRelaBean> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ){
        CustomerProductTypeRelaBean detail = customerProductTypeLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增经销商客户存货分类")
    public ResponseHelper add(
            @ApiParam(required = true, value = "object") @RequestBody(required = true) CustomerProductTypeRela customerProductTypeRela
    ){
        customerProductTypeLogic.add(customerProductTypeRela);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改经销商客户存货分类")
    public ResponseHelper update(
            @ApiParam(required = true, value = "object") @RequestBody(required = true) CustomerProductTypeRela customerProductTypeRela
    ){
        customerProductTypeLogic.update(customerProductTypeRela);
        return ResponseHelper.success();
    }



    @PostMapping("/delete")
    @ApiOperation(value = "删除经销商客户存货分类")
    public ResponseHelper delete(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) List<String> ids
    ){
        customerProductTypeLogic.delete(ids);
        return ResponseHelper.success();
    }


    @GetMapping("/download")
    @ApiOperation("下载模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("钱包业务员关系模板.xlsx",response);
    }


    @PostMapping("/importData")
    @ApiOperation("导入")
    public ResponseHelper importData(
            MultipartFile file
    ) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        customerProductTypeLogic.improtData(objects);
        return ResponseHelper.success();
    }



}
