package com.jxy.order.controller;

import com.github.pagehelper.PageHelper;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.core.web.controller.BaseController;
import com.jxy.common.core.web.page.TableDataInfo;
import com.jxy.common.security.annotation.RequiresLogin;
import com.jxy.common.security.annotation.RequiresRoles;
import com.jxy.order.service.OrderService;
import com.jxy.order.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


/**
 * @Description 交易相关controller
 * @Date 2023/5/4 14:17
 * @Author 杜懿
 */
@RestController
@RequestMapping("/transfer")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;

    //获取地址
    //@PostMapping("/getUserAddress")
    //@RequiresLogin
    //public AjaxResult getUserAddCreditAddress(){
    //
    //    return  orderService.getUserAddress();
    //
    //}

    //生成充值支付单  入参:支付金额、币种、支付网络、支付地址、支付的txid、购买的业务和类型
    @PostMapping("/createOrder")
    @RequiresLogin
    public AjaxResult createAddCreditOrder(@RequestBody AddCreditOrderVo vo){
        return  orderService.createAddCreditOrder(vo);

    }

    //用户手动确认支付后立即处理订单
    @PostMapping("/endOrder")
    @RequiresLogin
    public AjaxResult handleOrder(@RequestBody OrderChangeByTxidVo vo){
        return  orderService.handleOrder(vo);

    }

    @PostMapping("/cancelOrder")
    @RequiresLogin
    public AjaxResult cancelOrder(@RequestBody OrderIdVo vo){
        return  orderService.cancelOrder(vo);

    }

    //获取用户当前余额
    @PostMapping("/getUserAccount")
    @RequiresLogin
    public AjaxResult getUserAccount(){

        return  orderService.getUserAccount();

    }

    //通过txid查询并处理用户订单
    @PostMapping("/checkOrderByTxid")
    @RequiresLogin
    public AjaxResult createOrderByTxid(@RequestBody OrderChangeByTxidVo vo){
        return  orderService.handleOrderByTxid(vo);
    }

    @PostMapping("/lastUseTxid")
    @RequiresLogin
    public AjaxResult lastUseTxid(){
        return  orderService.checkLastUseTxid();
    }

    //@PostMapping("/getEmailCode")
    //@RequiresLogin
    //public AjaxResult getEmailCode(){
    //
    //    return  orderService.sendAddCreditEmail();
    //
    //}

    //用户获取历史购买记录
    @PostMapping("/privateOrder")
    @RequiresLogin
    public TableDataInfo privateOrder(@RequestBody(required = false) StatusPageVo vo){
        if(StringUtils.isNull(vo)){
            vo = new StatusPageVo();
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        return getDataTable(orderService.privateOrder(vo));
    }

    //后台获取支付历史记录ALTER TABLE chat_message_history
    //CHANGE COLUMN `read` is_read tinyint(1) NOT NULL DEFAULT '0' COMMENT '0已读  1未读';
    //
    //ALTER TABLE chat_message
    //CHANGE COLUMN `read` is_read tinyint(1) NOT NULL DEFAULT '0' COMMENT '0已读  1未读';
    @PostMapping("/allAddCreditOrder")
    @RequiresLogin
    public TableDataInfo allAddCreditOrder(@RequestBody(required = false) OrderStatusVo vo){
        if(StringUtils.isNull(vo)){
            vo = new OrderStatusVo();
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        return getDataTable(orderService.getAddCreditOrderList(vo));
    }

    //查看订单详情
    @PostMapping("/details/{id}")
    @RequiresRoles("accounting")
    public AjaxResult getAddCreditDetails(@PathVariable("id") long id){
        return orderService.getAddCreditOrderById(id);
    }

    //新增地址
    @PostMapping("/addNewAddress")
    @RequiresRoles({"admin","support"})
    public AjaxResult addNewAddress(@RequestBody TotalAddressVo vo){
        return orderService.AddTotalAddresses(vo);
    }



    //获取地址使用详情
    @PostMapping("/allAddress")
    @RequiresLogin
    @RequiresRoles({"admin","support"})
    public TableDataInfo allAddress(@RequestBody(required = false) StatusPageVo vo){
        if(StringUtils.isNull(vo)){
            vo = new StatusPageVo();
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        return getDataTable(orderService.getAllAddress(vo));
    }

    //后台人员确认支付信息并更改订单状态
    //@PostMapping("/handleOrder")
    //@RequiresRoles("accounting")
    //public AjaxResult handleAddCreditOrder(@RequestBody HandleAddCreditOrderVo vo){
    //    return orderService.handleAddCreditOrder(vo);
    //}

    //@PostMapping("/test")
    //@RequiresLogin
    //public TableDataInfo test(@RequestBody(required = false) StatusPageVo vo){
    //
    //    startPage();
    //    return getDataTable(orderService.getAllAddress(vo));
    //}


    //后台获取订单操作记录

    //后台获取每日历史图表

    //通知用户网站通知栏， Mobile App,  email

}
