package com.reptileclient.jlifeapp.web;


import com.reptileclient.jlifeapp.dto.HmCreateRightRequestDTO;
import com.reptileclient.jlifeapp.dto.HmrightDetailRespDTO;
import com.reptileclient.jlifeapp.dto.HmrightInfoRespDTO;
import com.reptileclient.jlifeapp.result.ResultBody;
import com.reptileclient.jlifeapp.service.HmRightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(value = "盒马权益单处理")
@Slf4j
public class HmRightController {

    @Autowired
    private HmRightService hmRightService;

    @ApiOperation(value = "创建权益单")
    @PostMapping("createHmRightOrder")
    public ResultBody createHmRightOrder(HmCreateRightRequestDTO dto) {
        log.info("创建盒马权益单，入参：" + dto);
        String rightOrderNo = hmRightService.createHmRightOrder(dto);
        if(StringUtils.isBlank(rightOrderNo)) {
        	return new ResultBody(400, "创建失败");
        }
        //获取盒马权益卡信息
        List<HmrightInfoRespDTO> hmrightInfoDTOList = hmRightService.queryHmRightOrder(rightOrderNo);
        return new ResultBody(hmrightInfoDTOList.get(0));
    }

    @ApiOperation(value = "查询权益单信息")
    @GetMapping("queryHmRightOrder")
    public ResultBody queryHmRightOrder(@RequestParam("rightOrderNo") String rightOrderNo) {
        log.info("查询盒马权益单信息，入参：" + rightOrderNo);
        List<HmrightInfoRespDTO> hmrightInfoDTOList = hmRightService.queryHmRightOrder(rightOrderNo);
        log.info("查询盒马权益单信息,出参:" + hmrightInfoDTOList);
        return new ResultBody(hmrightInfoDTOList.get(0));
    }

    @ApiOperation(value = "查询权益单预约详情")
    @GetMapping("queryHmRightOrderDetail")
    public ResultBody queryHmRightOrderDetail(@RequestParam("rightOrderNo") String rightOrderNo) {
        log.info("查询权益单预约详情，入参：" + rightOrderNo);
        List<HmrightDetailRespDTO> detail = hmRightService.queryHmRightOrderDetail(rightOrderNo);
        log.info("查询权益单预约详情,出参:" + detail);
        return new ResultBody(detail);
    }

    @ApiOperation(value = "取消权益")
    @GetMapping("cancelHmRight")
    public ResultBody cancelHmRight(@RequestParam("rightOrderNo") String rightOrderNo) {
        log.info("取消权益，入参：" + rightOrderNo);
        hmRightService.cancelHmRight(rightOrderNo);
        //获取盒马权益卡信息
        List<HmrightInfoRespDTO> hmrightInfoDTOList = hmRightService.queryHmRightOrder(rightOrderNo);
        return new ResultBody(hmrightInfoDTOList.get(0));
    }

}

