package com.fdkankan.ucenter.controller.app;

import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.service.IInvoiceService;
import com.fdkankan.ucenter.vo.request.InvoiceParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ucenter/user/invoice")
public class InvoiceController extends BaseController {

    @Autowired
    IInvoiceService invoiceService;


    /**
     * 获得可开发票的最高额度
     * @param param
     * @return
     */
    @RequestMapping(value = "/max", method = RequestMethod.POST)
    public Result getMaxInvoice(@RequestBody InvoiceParam param){
        return Result.success(invoiceService.getMaxInvoice(param));
    }

    /**
     * 获取用户发票记录
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result getInvoices(@RequestBody InvoiceParam param) throws Exception {
        return Result.success(invoiceService.getInvoices(getToken(), param));

    }

    /**
     * 获取用户发票记录
     */
    @RequestMapping(value = "/mine", method = RequestMethod.POST)
    public Result mine(@RequestBody InvoiceParam param) throws Exception {
        return Result.success(invoiceService.getMyInvoiceInfo(getToken(), param));
    }

    /**
     * 保存用户发票信息
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody InvoiceParam param) throws Exception {
        invoiceService.saveInvoice(getToken(), param);
        return Result.success();
    }

    /**
     * 获取发票详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Result getInvoiceDetail(@RequestBody InvoiceParam param) throws Exception {
        return Result.success(invoiceService.getById(param.getInvoiceId()));
    }

    /**
     * 申请开票
     * @param param
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addInvoice(@RequestBody InvoiceParam param) throws Exception {
        invoiceService.addInvoice(getToken(), param);
        return Result.success();
    }

    /**
     * 修改开票信息
     * @param param
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateInvoice(@RequestBody InvoiceParam param) throws Exception {
        invoiceService.updateInvoice(getToken(), param);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result deleteInvoice(@RequestBody InvoiceParam param) throws Exception {
        invoiceService.removeById(param.getInvoiceId());
        return Result.success();
    }

    /**
     * 申请开票
     * @param param
     * @return
     */
    @RequestMapping(value = "/open", method = RequestMethod.POST)
    public Result open(@RequestBody InvoiceParam param) throws Exception {
        return Result.success(invoiceService.openInvoice( param,getToken()));
    }

}
