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.CommonCountBean;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.k3.K3ReceiptBean;
import com.zfire.mall.manager.bean.k3.KingDeeMaterialBean;
import com.zfire.mall.manager.bean.listV2.CustomerWalletCreditRecordVO;
import com.zfire.mall.manager.bean.listV2.K3ReceiptVO;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.k3.K3MaterialLogic;
import com.zfire.mall.manager.logic.k3.K3ReceiptLogic;
import com.zfire.mall.manager.plus.entity.CustomerWalletCreditRecord;
import com.zfire.mall.manager.plus.entity.KingDeeMaterial;
import com.zfire.mall.manager.plus.entity.KingDeeReceipt;
import com.zfire.mall.manager.plus.entity.KingDeeWallet;
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;

@Slf4j
@RestController
@Api(value = "金蝶收款单KPI", tags ={"金蝶收款单KPI"} )
@RequestMapping(value = "/k3/receipt", produces = "application/json; charset=utf-8")
public class K3ReceiptController {

    @Autowired
    K3ReceiptLogic k3ReceiptLogic;


    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation(value = "收款单列表-V2")
    public ResponseHelper<IPage<K3ReceiptVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws Exception {
        IPage<K3ReceiptVO> page = k3ReceiptLogic.listV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<K3ReceiptVO>() {});
    }

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



    @ApiOperation(value = "收款单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<K3ReceiptBean>> list(
            @ApiParam(required = false, value = "单据类型") @RequestParam(required = false) String billType,
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @ApiParam(required = false, value = "往来单位") @RequestParam(required = false) String wlCompanyType,
            @ApiParam(required = false, value = "付款单位") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "结算方式") @RequestParam(required = false) String settleType,
            @ApiParam(required = false, value = "业务日期开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "业务日期结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "广州=GZ  佛山=FS") @RequestParam(required = false) String websitNumber,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<K3ReceiptBean> list = k3ReceiptLogic.list(billType, billNo, wlCompanyType, customerName, settleType, startTime, endTime,websitNumber, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @ApiOperation(value = "收款单列表-统计")
    @GetMapping("/listCount")
    public ResponseHelper<CommonCountBean> listCount(
            @ApiParam(required = false, value = "单据类型") @RequestParam(required = false) String billType,
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @ApiParam(required = false, value = "往来单位") @RequestParam(required = false) String wlCompanyType,
            @ApiParam(required = false, value = "付款单位") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "结算方式") @RequestParam(required = false) String settleType,
            @ApiParam(required = false, value = "业务日期开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "业务日期结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "广州=GZ  佛山=FS") @RequestParam(required = false) String websitNumber
    ) throws Exception {
        CommonCountBean bean = k3ReceiptLogic.listCount(billType, billNo, wlCompanyType, customerName, settleType, startTime, endTime,websitNumber);
        return ResponseHelper.success(bean);
    }



    @ApiOperation(value = "收款单详情")
    @PostMapping("/detail")
    public ResponseHelper<K3ReceiptBean> detail(
            @ApiParam(required = true) @RequestParam String id
    ) throws Exception {
        K3ReceiptBean detail = k3ReceiptLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/abandon")
    @ApiOperation("弃审")
    public ResponseHelper abandon(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws InterruptedException {
        k3ReceiptLogic.abandon(id);
        return ResponseHelper.success();
    }



    @ApiOperation(value = "导出收款单列表")
    @GetMapping("/export")
    public void list(
            @ApiParam(required = false, value = "单据类型") @RequestParam(required = false) String billType,
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @ApiParam(required = false, value = "往来单位") @RequestParam(required = false) String wlCompanyType,
            @ApiParam(required = false, value = "付款单位") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "结算方式") @RequestParam(required = false) String settleType,
            @ApiParam(required = false, value = "业务日期开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "业务日期结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "广州=GZ  佛山=FS") @RequestParam(required = false) String websitNumber,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<K3ReceiptBean> list = k3ReceiptLogic.list(billType, billNo, wlCompanyType, customerName, settleType, startTime, endTime,websitNumber, 1, -1);
        ExcelData export = k3ReceiptLogic.export(list.getRecords());
        ExcelUtils.exportExcel(request,response,"收款单数据.xlsx",export);
    }



    @ApiOperation(value = "新增收款单")
    @PostMapping("/add")
    public ResponseHelper add(
            @RequestBody KingDeeReceipt kingDeeReceipt
    ) throws Exception {
        k3ReceiptLogic.add(kingDeeReceipt);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "修改收款单")
    @PostMapping("/update")
    public ResponseHelper updateCustomer(
            @RequestBody KingDeeReceipt kingDeeReceipt
    ) throws Exception {
        k3ReceiptLogic.update(kingDeeReceipt);
        return ResponseHelper.success();
    }



    @ApiOperation(value = "审批收款单")
    @PostMapping("/examine")
    public ResponseHelper examine(
            @ApiParam(required = false, value = "单据编号") @RequestParam(required = false) String billNo,
            @ApiParam(required = false, value = " 状态  OK通过  WAIT待审 FAIL 不通过") @RequestParam(required = false) String status
    ) throws Exception {
        k3ReceiptLogic.examine(billNo,status);
        return ResponseHelper.success();
    }



}
