package com.younike.admin;

import com.alibaba.fastjson.JSON;
import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.domain.entity.SysUser;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.SecurityUtils;
import com.younike.framework.config.AnonymousAccess;
import com.younike.framework.web.service.SysPermissionService;
import com.younike.hlpay.dto.NotifyRefundResponseVo;
import com.younike.system.domain.OrderRefund;
import com.younike.system.domain.vo.OrderVo;
import com.younike.system.param.OrderQueryParam;
import com.younike.system.service.IOrderRefundService;
import com.younike.system.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 会员订单Controller
 *
 * @author zulu.chen
 * @date 2022-01-11
 */
@Api("会员订单接口")
@RestController
@RequestMapping("/admin/order")
@Log4j2
public class AdminOrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderRefundService orderRefundService;

    @Autowired
    private SysPermissionService permissionService;


    /**
     * 查询会员订单列表
     */
    @ApiOperation("查询会员订单列表")
    @PreAuthorize("@ss.hasRole('operation')")
    @GetMapping("/orderList")
    public TableDataInfo list(OrderQueryParam param) {
        startPage();
        List<OrderVo> list = orderService.orderList(param);
        return getDataTable(list);
    }

    @ApiOperation("查询会员订单退款信息")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @GetMapping("getRefundInfo")
    public AjaxResult getRefundInfo(@RequestParam("orderId") String orderId) {
        return AjaxResult.success(orderRefundService.selectOrderRefundByOrderId(orderId));
    }

    @ApiOperation("审核退款")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping("/doRefund")
    public AjaxResult doRefund(@RequestBody OrderRefund orderRefund) {
        // 获取当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();
//        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        Iterator it = roles.iterator();
//        String role = StringUtils.EMPTY;
//        while(it.hasNext()) {
//            role = it.next().toString();
//        }
//        if ("merchant".equals(role)) {
//            orderRefund.setStatus(OrderRefundStateEnum.MERCHANT_REFUNDS.getCode());
//        }
//        if ("operation".equals(role)) {
//            orderRefund.setStatus(OrderRefundStateEnum.PLATFORM_REFUNDS.getCode());
//        }
        orderRefund.setUpdateBy(String.valueOf(user.getUserId()));
        return orderService.doRefund(orderRefund);
    }

    @ApiOperation("合利宝订单退款回调")
    @PostMapping("/hlb/refund/cb")
    @AnonymousAccess
    public String refundCb(@ModelAttribute NotifyRefundResponseVo body){
        log.info("合利宝退款回调：" + JSON.toJSONString(body));
        return orderService.refundCb(body);
    }
}
