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

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.listV2.CustomerWalletCreditRecordVO;
import com.zfire.mall.manager.bean.listV2.SaleTypeVO;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.wallet.CustomerWalletCreditLogic;
import com.zfire.mall.manager.plus.entity.CustomerWalletCreditRecord;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

@Slf4j
@RestController
@Api(value = "信用额度API", tags ={"信用额度API"} )
@RequestMapping(value = "/credit", produces = "application/json; charset=utf-8")
public class CustomerWalletCreditController {

    @Autowired
    CustomerWalletCreditLogic customerWalletCreditLogic;


    @ZfireList
    @PostMapping("/record/list/v2")
    @ApiOperation(value = "信用额度记录-V2")
    public ResponseHelper<IPage<CustomerWalletCreditRecordVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws Exception {
        IPage<CustomerWalletCreditRecordVO> page = customerWalletCreditLogic.recordListV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<CustomerWalletCreditRecordVO>() {});
    }

    @PostMapping("/list/v2/export")
    @ApiOperation("信用额度记录V2-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerWalletCreditRecordVO> list = customerWalletCreditLogic.recordListV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @GetMapping("/record/list")
    @ApiOperation(value = "信用额度记录")
    public ResponseHelper<IPage<CustomerWalletCreditRecord>> list(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String customerWalletId,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "页号",required = false) @RequestParam(required = false) Integer pageNum,
            @ApiParam(value = "页大小",required = false) @RequestParam(required = false) Integer pageSize
    ) throws Exception {
        IPage<CustomerWalletCreditRecord> list = customerWalletCreditLogic.recordList(customerWalletId,startTime, endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/record/export")
    @ApiOperation(value = "信用额度记录-导出")
    public void export(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String customerWalletCreditId,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<CustomerWalletCreditRecord> list = customerWalletCreditLogic.recordList(customerWalletCreditId,startTime, endTime, 1, -1);
        ExcelData excelData = customerWalletCreditLogic.recordExportData(list.getRecords());
        ExcelUtils.exportExcel(request,response,"额度记录.xlsx",excelData);
    }


    @PostMapping("/record/add")
    @ApiOperation(value = "设置信用额度")
    public ResponseHelper add(
            @ApiParam(value = "经销商id",required = true) @RequestParam(required = true) String customerId,
            @ApiParam(value = "金额",required = true) @RequestParam(required = true) BigDecimal amount,
            @ApiParam(value = "经销商钱包id",required = true) @RequestParam(required = true) String customerWalletId,
            @ApiParam(value = "开始时间",required = true) @RequestParam(required = true) String startTime,
            @ApiParam(value = "结束时间",required = true) @RequestParam(required = true) String endTime
    ) throws Exception {
        customerWalletCreditLogic.addRecord(customerId, amount, customerWalletId,startTime,endTime);
        return ResponseHelper.success();
    }




}
