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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.websit.SettlementOrderDetail;
import com.gree.mall.manager.bean.websit.SettlementOrderLogicVO;
import com.gree.mall.manager.bean.websit.WorkerSettlementLineVO;
import com.gree.mall.manager.bean.websit.WorkerSettlementVO;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.websit.SettlementOrderLogic;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "结算单新列表", tags = {"结算单新列表-电子支付"})
@RequestMapping(value = "/settlementOrderNew", produces = "application/json; charset=utf-8")
public class SettlementOrderNewController {

    @Autowired
    private SettlementOrderLogic settlementOrderLogic;



    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "结算单列表")
    public ResponseHelper<IPage<SettlementOrderLogicVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<SettlementOrderLogicVO> page = settlementOrderLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<SettlementOrderLogicVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "结算单导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<SettlementOrderLogicVO> page = settlementOrderLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/detail")
    @ApiOperation("结算详情")
    public ResponseHelper<SettlementOrderDetail> detail(
            @ApiParam(value = "结算id" , required = true) @RequestParam(required = true) String id,

            HttpServletRequest request
    ) throws ParseException {
        SettlementOrderDetail  settlementOrderDetail = settlementOrderLogic.detail(id);
        return ResponseHelper.success(settlementOrderDetail);
    }


    @PostMapping("/settlement")
    @ApiOperation("结算")
    public ResponseHelper settlement(
            @ApiParam(value = "结算id" , required = true) @RequestParam(required = true) List<String> id,
            HttpServletRequest request
    ) throws ParseException, WxPayException {
        settlementOrderLogic.settlement(id);
        return ResponseHelper.success();
    }



    @PostMapping("/lineSettlement")
    @ApiOperation("线下结算")
    public ResponseHelper lineSettlement(
            @ApiParam(value = "结算id" , required = true) @RequestParam(required = true) List<String> id,
            HttpServletRequest request
    ) throws ParseException {
        settlementOrderLogic.lineSettlementOrder(id);
        return ResponseHelper.success();
    }




}
