package com.chinacreator.video.restful.controller;

import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.RedeemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yu.su
 * @date 2021.03.25
 * 挽回用户
 */
@RestController
@RequestMapping("/redeemUser")
public class RedeemUserController {

    @Autowired
    private RedeemUserService redeemUserService;

    @PostMapping("/insert")
    public Map<String, Object> insert(@RequestBody OrderInfo orderInfo) {
        System.out.println(orderInfo);
        Map<String, Object> resp = new HashMap<>();
        String resultCode = "0";
        String errorInfo = "";
        try {
            redeemUserService.check(orderInfo);
            redeemUserService.findUser(orderInfo);
            redeemUserService.insert(orderInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultCode = ((BussinessException) e).getCode();
                errorInfo = e.getMessage();
            } else {
                resultCode = "8000";
                errorInfo = e.getMessage();
            }
        } finally {
            resp.put("resultCode", resultCode);
            resp.put("errorInfo", errorInfo);
        }


        return resp;
    }
}
