package com.younike.member;

import com.alibaba.fastjson.JSON;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.utils.StringUtils;
import com.younike.framework.config.AnonymousAccess;
import com.younike.hlpay.dto.PaymentParam;
import com.younike.system.domain.Order;
import com.younike.system.domain.vo.MemberOrderVo;
import com.younike.hlpay.dto.OrderParam;
import com.younike.system.param.OredrRefundParam;
import com.younike.system.service.IOrderRecordService;
import com.younike.system.service.IOrderService;
import com.younike.system.service.IServerTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.List;


/**
 * 会员订单Controller
 *
 * @author dail
 * @date 2022-01-10
 */
@Api("会员订单接口")
@RestController
@RequestMapping("/member/order")
public class MemberOrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderRecordService orderRecordService;

    @Autowired
    private IServerTimeService serverTimeService;


    /**
     * 查询会员订单列表
     */
    @ApiOperation("查询会员订单列表")
    @AnonymousAccess
//    @PreAuthorize("@ss.hasPermi('member:order:list')")
    @GetMapping("/list")
    public Object list(Order order) {
        if (order == null || order.getMemberId() == null) {
            return AjaxResult.error("参数错误");
        }
        startPage();
        List<MemberOrderVo> list = orderService.listMemberOrderByOrderState(order);
        return getDataTable(list);
    }

    /**
     * 查询会员订单详情
     */
    @ApiOperation("查询会员订单详情")
    @AnonymousAccess
//    @PreAuthorize("@ss.hasPermi('member:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getMemberOrderByOrderId(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(orderService.getMemberOrderByOrderId(orderId));
    }

    @ApiOperation("订单接口：会员下单")
    @AnonymousAccess
    @PostMapping
    public AjaxResult addOrder(@RequestBody OrderParam param) {
        //数据校验
        if(ObjectUtils.isEmpty(param)){
            return AjaxResult.error("参数错误");
        }
        if(param.getMemberId()==null){
            return AjaxResult.error("参数错误(会员id不能为空)");
        }
        if(param.getGoodsId()==null){
            return AjaxResult.error("参数错误(商品id不能为空)");
        }
        if(param.getMerchantId()==null){
            return AjaxResult.error("参数错误(商户id不能为空)");
        }

        if(param.getServerId()==null){
            return AjaxResult.error("参数错误(服务人员id不能为空)");
        }
        if(StringUtils.isNull(param.getAppointmentStartTime())){
            return AjaxResult.error("参数错误(预约起始,结束时间不能为空)");
        }
        if(param.getOrderAmount()==null){
            return AjaxResult.error("参数错误(订单金额不能为空)");
        }
        if(param.getGoodsCounts()==null){
            return AjaxResult.error("参数错误(订单商品数量不能为空)");
        }
        if(StringUtils.isNull(param.getOrderType())){
            return AjaxResult.error("参数错误(订单类型不能为空)");
        }
        if(param.getTotalAmount()==null){
            return AjaxResult.error("参数错误(实付总价金额)");
        }
        if(param.getAddressId()==null){
            return AjaxResult.error("参数错误(下单地址不能为空)");
        }
        if(param.getPrice()==null){
            return AjaxResult.error("参数错误（商品单价不能为空）");
        }

        if(param.getSingleTime()==null){
            return AjaxResult.error("参数错误（单次服务标准时长不能为空）");
        }

        return  orderService.addOrder(param);
    }

    @PutMapping("/payment")
    @AnonymousAccess
    @ApiOperation("订单支付")
    public AjaxResult edit(@RequestBody PaymentParam param) {

        //参数验证
        if(ObjectUtils.isEmpty(param)){
            return AjaxResult.error("参数错误");
        }
        //验证会员d是否为空
        if(StringUtils.isNull(param.getOrderId())){
            return AjaxResult.error("参数错误(订单id不能为空)");
        }
        //实付总价，支付方式不能为空
        if(StringUtils.isNull(param.getPayWay()) || param.getTotalAmount()==null){
            return AjaxResult.error("参数错误(实付总价，支付方式不能为空)");
        }
        return orderService.editOrder(param);
    }

    /**
     * 申请平台介入（申请退款）
     * @param param
     * @return
     */
    @PutMapping("/applyForRefund")
    @AnonymousAccess
    @ApiOperation("申请平台介入（申请退款）")
    public AjaxResult applyForRefund(@RequestBody OredrRefundParam param) {

        //参数验证
        if(ObjectUtils.isEmpty(param) || param.getOrderId()==null){
            return AjaxResult.error("参数错误");
        }
        return orderService.applyForRefund(param);
    }

    /**
     * 取消申请平台介入
     * @param param
     * @return
     */
    @PutMapping("/cancelRefund")
    @AnonymousAccess
    @ApiOperation("取消申请平台介入")
    public AjaxResult cancelRefund(@RequestBody OrderParam param) {

        //参数验证
        if(ObjectUtils.isEmpty(param) || param.getOrderId()==null){
            return AjaxResult.error("参数错误");
        }
        return orderService.cancelRefund(param);
    }

    /**
     * 申请取消订单
     * @param param
     * @return
     */
    @PutMapping("/cancal")
    @AnonymousAccess
    @ApiOperation("申请取消订单")
    public AjaxResult cancal(@RequestBody OredrRefundParam param) {

        //参数验证
        if(ObjectUtils.isEmpty(param)|| param.getOrderId()==null){
            return AjaxResult.error("参数错误");
        }
        return orderService.cancal(param);
    }

    /**
     * 确认完成
     * @param param
     * @return
     */
    @PutMapping("/affirm")
    @AnonymousAccess
    @ApiOperation("确认完成")
    public AjaxResult affirm(@RequestBody OredrRefundParam param) {

        //参数验证
        if(ObjectUtils.isEmpty(param)|| param.getOrderId()==null){
            return AjaxResult.error("参数错误");
        }
        return orderService.affirm(param);
    }

    /**
     * 追加服务
     * @param param
     * @return
     */
    @PutMapping("/addServer")
    @AnonymousAccess
    @ApiOperation("追加服务")
    public AjaxResult addServer(@RequestBody OrderParam param) {

        //参数验证
        if(ObjectUtils.isEmpty(param)){
            return AjaxResult.error("参数错误");
        }
        //验证会员d是否为空
        if(StringUtils.isNull(param.getOrderId())){
            return AjaxResult.error("参数错误(订单id不能为空)");
        }
        //验证服务人员id是否为空
        if(StringUtils.isNull(param.getOrderId())){
            return AjaxResult.error("参数错误(服务人员id不能为空)");
        }
        //实付总价，支付方式不能为空
        if(param.getAddAmount()==null || StringUtils.isNull(param.getAddPayWay()) || param.getAddCounts()==null || param.getSingleTime()==null){
            return AjaxResult.error("参数错误(追加服务金额，支付方式,追加服务数量，单次服务标准时长不能为空)");
        }
        return orderService.addServer(param);
    }

    /**
     * 查询会员订单追踪信息
     */
    @ApiOperation("查询会员订单追踪信息")
    @AnonymousAccess
//    @PreAuthorize("@ss.hasPermi('member:orderRecord:query')")
    @GetMapping(value = "/getMemberOrderRecord")
    public AjaxResult getMemberOrderRecord(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            return AjaxResult.error("参数错误(订单id不能为空)");
        }
        return AjaxResult.success(orderRecordService.getMemberOrderRecord(orderId));
    }

    /**
     * 查询会员订单追踪信息
     */
    @ApiOperation("查询服务人员服务预约列表")
    @GetMapping(value = "/server-time-section")
//    @PermitAll
    public AjaxResult getServerTimeSections(@RequestParam("serverId") Long serverId) {
        if (serverId == null) {
            return AjaxResult.error("参数错误(服务人员id不能为空)");
        }
        return AjaxResult.success(serverTimeService.getServerTimeSections(serverId));
    }


}
