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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.PayDetail;
import com.gree.mall.manager.bean.workorder.IncreOrderVO;
import com.gree.mall.manager.bean.workorder.PgIncreOrderBean;
import com.gree.mall.manager.bean.workorder.WebsitOrderSettleCountVO;
import com.gree.mall.manager.bean.workorder.WorkerOrderSettleCountVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.workorder.IncreOrderSettleLogic;
import com.gree.mall.manager.plus.entity.*;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "增值服务订单结算管理", tags = {"增值服务订单结算管理"})
@RequestMapping(value = "/incre/order/settle", produces = "application/json; charset=utf-8")
public class IncreOrderSettleController {

    @Resource
    IncreOrderSettleLogic increOrderSettleLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "订单结算管理-列表")
    public ResponseHelper<Page<IncreOrderVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<IncreOrderVO> orderSmallTypeVO = increOrderSettleLogic.list(zfireParamBean, false);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<IncreOrderVO>() {});
    }

    @ZfireList
    @PostMapping("/list2")
    @ApiOperation(value = "订单结算管理-列表2")
    public ResponseHelper<Page<IncreOrderVO>> list2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<IncreOrderVO> orderSmallTypeVO = increOrderSettleLogic.list(zfireParamBean, true);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<IncreOrderVO>() {});
    }

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

    @PostMapping("/list/export2")
    @ApiOperation(value = "订单结算管理-导出2")
    public void listExport2(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<IncreOrderVO> page = increOrderSettleLogic.list(zfireParamBean, true);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "订单结算管理-详情")
    public ResponseHelper<PgIncreOrderBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        PgIncreOrderBean bean = increOrderSettleLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/add")
    @ApiOperation(value = "订单结算管理-提交")
    public ResponseHelper<PgIncreOrder> add(
            @RequestBody PgIncreOrder order
    ) throws Exception {

        increOrderSettleLogic.add(order);

        return ResponseHelper.success(order);
    }


    @PostMapping("/getIncreItem")
    @ApiOperation(value = "获得服务内容得金额和限值")
    public ResponseHelper<List<PgIncreItem>> getIncreItem(
            @ApiParam(value = "品牌",required = false) @RequestParam(required = false) String brand,
            @ApiParam(value = "大类",required = false) @RequestParam(required = false) String mainName
    ) throws RemoteServiceException {
        List<PgIncreItem> pgIncreItem = increOrderSettleLogic.getIncreItem(brand,mainName);
        return ResponseHelper.success(pgIncreItem);
    }


    @PostMapping("/getWebsit")
    @ApiOperation(value = "获得服务网点")
    public ResponseHelper<List<PgIncreWebsit>> getWebsit(
            @ApiParam(value = "服务配置id",required = false) @RequestParam(required = false) String increId

    ) throws RemoteServiceException {
        List<PgIncreWebsit> pgIncreItem = increOrderSettleLogic.getWebsit(increId);
        return ResponseHelper.success(pgIncreItem);
    }


    @PostMapping("/getCategoryList")
    @ApiOperation(value = "获得服务大类")
    public ResponseHelper<List<GoodsCategory>> getCategoryList(
            @ApiParam(value = "品牌",required = false) @RequestParam(required = false) String brand
    ) throws Exception {
        List<GoodsCategory> sysDictCompanyList = increOrderSettleLogic.getCategoryList(brand);
        return ResponseHelper.success(sysDictCompanyList);
    }


    @PostMapping("/getBrand")
    @ApiOperation(value = "获得品牌")
    public ResponseHelper<List<Brand>> getBrand(

    ) throws Exception {
        List<Brand> brand = increOrderSettleLogic.getBrand();
        return ResponseHelper.success(brand);
    }

    @PostMapping("/generate/pay/code")
    @ApiOperation(value = "订单结算管理-生成支付二维码")
    public ResponseHelper<PayDetail> pay(
            @ApiParam(value = "单号", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws Exception {
        PayDetail detail = increOrderSettleLogic.pay(id, request);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/examine")
    @ApiOperation(value = "订单结算管理-审批")
    public ResponseHelper examine(
            @RequestBody PgIncreOrder order
    ) throws Exception {

        increOrderSettleLogic.examine(order);

        return ResponseHelper.success();
    }

    @PostMapping("/refund")
    @ApiOperation(value = "订单结算管理-退款")
    public ResponseHelper refund(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws Exception {

        increOrderSettleLogic.refund(id);

        return ResponseHelper.success();
    }

    @PostMapping("/confirm")
    @ApiOperation(value = "订单结算管理-确认")
    public ResponseHelper confirm(
             @RequestBody List<String> ids
    ) throws Exception {

        increOrderSettleLogic.confirm(ids);

        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/count/list")
    @ApiOperation(value = "订单网点分账统计-列表")
    public ResponseHelper<Page<WebsitOrderSettleCountVO>> countList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitOrderSettleCountVO> orderSmallTypeVO = increOrderSettleLogic.countList(zfireParamBean);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<WebsitOrderSettleCountVO>() {});
    }

    @PostMapping("/count/list/export")
    @ApiOperation(value = "订单网点分账统计-导出")
    public void countListExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WebsitOrderSettleCountVO> page = increOrderSettleLogic.countList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/count/worker/list")
    @ApiOperation(value = "订单师傅分账统计-列表")
    public ResponseHelper<Page<WorkerOrderSettleCountVO>> countWorkerList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WorkerOrderSettleCountVO> orderSmallTypeVO = increOrderSettleLogic.countWorkerList(zfireParamBean);
        return ResponseHelper.success(orderSmallTypeVO, new TypeReference<WorkerOrderSettleCountVO>() {});
    }

    @PostMapping("/count/worker/list/export")
    @ApiOperation(value = "订单师傅分账统计-导出")
    public void countWorkerListExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WorkerOrderSettleCountVO> page = increOrderSettleLogic.countWorkerList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }



}
