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

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.ApiNotAuth;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.PayDetail;
import com.gree.mall.manager.bean.serivce.BuyServiceRecordVO;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.service.BuyServiceLogic;
import com.gree.mall.manager.plus.entity.BuyCompanyInfo;
import com.gree.mall.manager.plus.entity.BuyNormConfig;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
@RestController
@Api(value = "购买服务API", tags = {"购买服务API"})
@RequestMapping(value = "/buy/service", produces = "application/json; charset=utf-8")
public class BuyServiceController {

    private final BuyServiceLogic buyServiceLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "购买列表")
    public ResponseHelper<Page<BuyServiceRecordVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<BuyServiceRecordVO> page = buyServiceLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<BuyServiceRecordVO>() {});
    }

    @ApiNotAuth
    @PostMapping("/config/list")
    @ApiOperation(value = "收费配置列表")
    public ResponseHelper<List<BuyNormConfig>> configList() {
        List<BuyNormConfig> page = buyServiceLogic.configList();
        return ResponseHelper.success(page);
    }

    @ApiNotAuth
    @PostMapping("/add")
    @ApiOperation(value = "添加购买方信息")
    public ResponseHelper<String> add(
            @RequestBody BuyCompanyInfo info
    ) throws Exception {
        String id = buyServiceLogic.add(info);
        return ResponseHelper.success(id);
    }

    @ApiNotAuth
    @PostMapping("/detail")
    @ApiOperation(value = "购买方信息")
    public ResponseHelper<BuyCompanyInfo> detail(
            @ApiParam(value = "购买方名称", required = true) @RequestParam String companyName
    ) throws Exception {
        BuyCompanyInfo info = buyServiceLogic.detail(companyName);
        return ResponseHelper.success(info);
    }

    @ApiNotAuth
    @PostMapping("/generate/pay/code")
    @ApiOperation(value = "生成支付二维码")
    public ResponseHelper<PayDetail> pay(
            @ApiParam(value = "购买方信息id", required = true) @RequestParam String id,
            @ApiParam(value = "收费编号", required = true) @RequestParam String normId,
            HttpServletRequest request
    ) throws Exception {
        PayDetail detail = buyServiceLogic.pay(id, normId, request);
        return ResponseHelper.success(detail);
    }

    @ApiNotAuth
    @PostMapping("/query/pay/code")
    @ApiOperation(value = "查询支付结果")
    public ResponseHelper<Boolean> queryPayCode(
            @ApiParam(value = "购买方名称", required = true) @RequestParam String companyName,
            @ApiParam(value = "支付id", required = true) @RequestParam String payId
    ) {
        Boolean result = buyServiceLogic.queryPayCode(companyName, payId);
        return ResponseHelper.success(result);
    }
}
