package com.gree.mall.manager.controller.engin;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.engin.EnginPayManageBean;
import com.gree.mall.manager.bean.engin.EnginPayManageGatherVO;
import com.gree.mall.manager.bean.engin.EnginPayManageVO;
import com.gree.mall.manager.bean.material.base.WebsitGoodsAloneBean;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.engin.EnginPayManageLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

@Slf4j
@RestController
@Api(value = "工程开单收款管理", tags ={"工程开单收款管理API"} )
@RequestMapping(value = "/engin/pay", produces = "application/json; charset=utf-8")
public class EnginPayManageController {

    @Resource
    EnginPayManageLogic enginPayManageLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "工程开单收款管理-列表")
    public ResponseHelper<IPage<EnginPayManageVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<EnginPayManageVO> page = enginPayManageLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<EnginPayManageVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("工程开单收款管理-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<EnginPayManageVO> baseVOIPage = enginPayManageLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "工程开单收款管理-详情")
    public ResponseHelper<EnginPayManageBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        EnginPayManageBean bean = enginPayManageLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/save")
    @ApiOperation(value = "工程开单收款管理-提交")
    public ResponseHelper<EnginPayManageBean> save(
            HttpServletRequest request,
            @RequestBody EnginPayManageBean bean
    ) throws Exception {
        enginPayManageLogic.save(request, bean);
        return ResponseHelper.success();
    }

    @PostMapping("/confirm")
    @ApiOperation(value = "工程开单收款管理-审核")
    public ResponseHelper confirm(
            HttpServletRequest request,
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws Exception {
        enginPayManageLogic.confirm(request, id);
        return ResponseHelper.success();
    }

    @PostMapping("/pay")
    @ApiOperation(value = "工程开单收款管理-收款")
    public ResponseHelper<WebsitGoodsAloneBean> pay(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "收款金额", required = true) @RequestParam BigDecimal amount,
            @ApiParam(value = "收款凭证") @RequestParam(required = false) String fileUrl,
            @ApiParam(value = "备注") @RequestParam(required = false) String payRemark
    ) throws Exception {
        enginPayManageLogic.pay(id, amount, fileUrl, payRemark);
        return ResponseHelper.success();
    }

    @PostMapping("/import")
    @ApiOperation(value = "工程开单收款管理-导入(下载模板:工程收款管理模板.xlsx)")
    public ResponseHelper<String> importData (
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        return ResponseHelper.success(enginPayManageLogic.importData(file));
    }

    @ZfireList
    @PostMapping("/gather/list")
    @ApiOperation(value = "工程开单收款管理汇总-列表")
    public ResponseHelper<IPage<EnginPayManageGatherVO>> gatherList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<EnginPayManageGatherVO> page = enginPayManageLogic.gatherList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<EnginPayManageGatherVO>() {});
    }

    @PostMapping("/gather/list/export")
    @ApiOperation("工程开单收款管理汇总-导出")
    public void gatherListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<EnginPayManageGatherVO> baseVOIPage = enginPayManageLogic.gatherList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }
}
