package cn.superdesk.uniorder.controller.operationCenter.payment;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.data.extract.enums.opCenter.OpCenterBizPlatformEnum;
import cn.superdesk.uniorder.common.constants.BaseMessageConf;
import cn.superdesk.uniorder.common.constants.ResponseResultConstants;
import cn.superdesk.uniorder.common.rest.response.AnResponseStreamUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.mapper.operationCenter.OperationCenterPaymentBizMapper;
import cn.superdesk.uniorder.dto.param.operationCenter.operators.OperationCenterOperatorsParam;
import cn.superdesk.uniorder.dto.param.operationCenter.operators.OperationCenterPaymentOperatorParam;
import cn.superdesk.uniorder.dto.param.operationCenter.payment.OperationCenterPayParam;
import cn.superdesk.uniorder.dto.param.operationCenter.payment.OperationCenterPaymentParam;
import cn.superdesk.uniorder.dto.vo.operationCenter.operators.OperationCenterPaymentOperatorVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.order.OperationCenterOrderDetailsVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.payment.OperationCenterPaymentVo;
import cn.superdesk.uniorder.dubbo.consumer.operationCenter.payment.OperationCenterPaymentDubboService;
import cn.superdesk.uniorder.dubbo.facade.operationCenter.payment.DubboOperationCenterPaymentBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author : zx
 * @Create : 2023/3/28 17:20
 * Description : 运营中心-系统管理-支付方式配置
 */
@Api(value = "运营中心-系统管理", tags = {"系统管理-支付方式配置"})
@Slf4j
@RestController
@RequestMapping("/operationCenter/pay")
@RequiredArgsConstructor
public class OperationCenterPaymentController {

   private final OperationCenterPaymentDubboService operationCenterPaymentDubboService;

    @ApiOperation(value = "支付方式列表", notes = "")
    @PostMapping("/list")
    @ResponseBody
    public R<List<OperationCenterPaymentVo>> list(@RequestBody @Validated OperationCenterPayParam param) {
        R<List<OperationCenterPaymentVo>> result = new R<>();
        result = operationCenterPaymentDubboService.getOperationCenterPaymentList(param);

        return result;
    }




    @ApiOperation(value = "新增或编辑支付方式", notes = "")
    @PostMapping("/addOrUpdatePayment")
    @ResponseBody
    public R<String> addOrUpdatePayment(@RequestBody @Validated OperationCenterPaymentParam param, HttpServletRequest request) {
        String token      = request.getHeader("token");
        log.info("request::token -> {} \n", token);
        Long userId=null;
        R<String> result = new R<>();
        if (StrUtil.isNotBlank(token)) {
            LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
            Long sysUserId = loginSysUser.getSysUserId();
            userId = sysUserId;
            result = operationCenterPaymentDubboService.addOrUpdateOperationCenterPayment(param, userId);
            return result;
        }

        return new R<>("500","token is null,请刷新！",false);
    }




    @ApiOperation(value = "根据id查询", notes = "")
    @PostMapping("/{id}")
    @ResponseBody
    public R<OperationCenterPaymentParam> addOrUpdatePayment(@PathVariable Long id) {

        R<OperationCenterPaymentParam> result =new R<>();

        OperationCenterPaymentParam paymentVo = operationCenterPaymentDubboService.getPaymentById(id);

        result.setData(paymentVo);


        return result;
    }

    @ApiOperation(value = "关联运营商", notes = "")
    @PostMapping("/linkOperators")
    @ResponseBody
    public R<String> linkOperators(@RequestBody @Validated OperationCenterPaymentOperatorParam param, HttpServletRequest request) {

        String token      = request.getHeader("token");
        log.info("request::token -> {} \n", token);
        Long userId=null;

        R<String> result =new R<>();

        if (StrUtil.isNotBlank(token)) {
            LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
            Long sysUserId = loginSysUser.getSysUserId();
            userId = sysUserId;
            operationCenterPaymentDubboService.linkOperators(param,userId);
            result.setData("保存成功！");
            return result;
        }

        return new R<>("500","token is null,请刷新！",false);


    }

    @ApiOperation(value = "查询所有运营商列表", notes = "")
    @PostMapping("/getOperatorsAll")
    @ResponseBody
    public R<List<OperationCenterPaymentOperatorVo>> getOperatorsAll(@RequestBody OperationCenterOperatorsParam param) {

        R<List<OperationCenterPaymentOperatorVo>> result = new R<>();

        String operatorsName = "";
        if (StringUtils.isNotBlank(param.getOperatorsName())) {
            operatorsName = param.getOperatorsName();
        }

        List<OperationCenterPaymentOperatorVo> operatorsAll=operationCenterPaymentDubboService.getOperatorsAll(operatorsName);

        result.setData(operatorsAll);


        return result;
    }

}
