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

import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.operationCenter.customer.OpCenterCustomerOperatorOrgParam;
import cn.superdesk.uniorder.dto.param.operationCenter.operators.OperationCenterPaymentCustomerParam;
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.payment.OperationCenterPaymentVo;
import cn.superdesk.uniorder.dubbo.consumer.operationCenter.payment.OperationCenterPaymentDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
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 extends BaseController {

   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("/linkCustomer")
    @ResponseBody
    public R<String> linkCustomer(@RequestBody @Validated OperationCenterPaymentCustomerParam 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.linkCustomer(param,userId);
            result.setData("保存成功！");
            return result;
        }

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


    }

    /**
     * 支付方式-关联客户-根据支付方式id查询
     */
    @PostMapping("/linkCustomer/{paymentId}")
    public R<OperationCenterPaymentCustomerParam> linkOperatorAndOrgByCustomerId(@PathVariable("paymentId") String paymentId) {
        return operationCenterPaymentDubboService.linkCustomerByPaymentId(paymentId);

    }


}
