package com.liquidnet.service.goblin.controller;

import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.dto.manage.GoblinOrderParam;
import com.liquidnet.service.goblin.dto.vo.*;
import com.liquidnet.service.goblin.param.SyncOrderParam;
import com.liquidnet.service.goblin.service.IGoblinOrderAppService;
import com.liquidnet.service.goblin.service.IGoblinOrderService;
import com.liquidnet.service.goblin.service.IGoblinStoreZhengzaiService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "正在下单相关-商铺")
@RestController
@RequestMapping("/zhengzai/store")
public class GoblinStoreZhengzaiController {

    @Autowired
    IGoblinStoreZhengzaiService goblinStoreZhengzaiService;
    @Autowired
    IGoblinOrderService goblinOrderService;
    @Autowired
    IGoblinOrderAppService goblinOrderAppService;

    @GetMapping("market/list")
    @ApiOperation("正在下单-活动列表")
    public ResponseDto<List<GoblinSelfMarketingVo>> getZhengzaiMarketList() {
        return goblinStoreZhengzaiService.getZhengzaiMarketList();
    }

    @GetMapping("spu/list")
    @ApiOperation("正在下单-参与活动的spu")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "marketId", value = "活动id", example = "1"),
    })
    public ResponseDto<List<GoblinZhengzaiGoodVo>> getStoreList(@RequestParam("marketId") @Valid String marketId) {
        return goblinStoreZhengzaiService.getSpuList(marketId);
    }

    @PostMapping("fc7bce6d6c2213b866f76493f92224b7")
    @ApiOperation("正在下单-下单")
    public ResponseDto<GoblinPayInnerResultVo> checkOrder(@RequestBody GoblinOrderParam param) {
        if (param.getUid() == null) {
            param.setUid("zhengzai");
        }
        return goblinOrderService.checkOrder(param, param.getUid());
    }

    @PostMapping("listByCode")
    @ApiOperation("列表[根据masterCode]")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderMasterCode", value = "主订单号"),
    })
    public ResponseDto<List<GoblinAppOrderListVo>> orderListByMaster(@RequestParam("orderMasterCode") @Valid String orderMasterCode) {
        return goblinOrderAppService.orderListByMaster(orderMasterCode);
    }

    @PostMapping("details")
    @ApiOperation("订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id", example = "1"),
    })
    public ResponseDto<GoblinAppOrderDetailsVo> orderDetails(@RequestParam("orderId") @Valid String orderId) {
        return goblinOrderAppService.orderDetails(orderId, null);
    }

    @PostMapping("paySuccess")
    @ApiOperation("支付成功")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<Boolean> syncOrder(@ModelAttribute @Valid SyncOrderParam syncOrderParam) {
        if (!syncOrderParam.getPaymentType().equals("huifu") || !syncOrderParam.getCode().equals("code")) {
            return ResponseDto.failure();
        }
        String result = goblinOrderService.syncOrder(syncOrderParam);
        if (result.equals("success")) {
            return ResponseDto.success();
        }
        return ResponseDto.failure();
    }
}
