package com.fdkankan.platform.order.controller.manager;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.controller.BaseController;
import com.fdkankan.platform.user.request.RequestCamera;
import com.fdkankan.platform.user.request.RequestInvoice;
import com.fdkankan.platform.user.request.RequestTrade;
import com.fdkankan.platform.user.service.IManagerService;
import com.fdkankan.platform.user.vo.ResponseInvoice;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台管理-开票
 */
@Log4j2
@RestController
@RequestMapping("/service/platform/manager/invoice")
public class ManagerInvoiceController extends BaseController {

    @Autowired
    private IManagerService managerService;

    /**
     * 查看增值记录-开票记录  汇总
     * startDate        开始时间
     * endDate          结束时间
     * searchKey        关键词
     * pageNum          分页
     * pageSize         每页大小
     */
    @RequestMapping(value = {"/all"}, method = RequestMethod.POST)
    public Page<JSONObject> all(@RequestBody RequestCamera param) throws Exception {
        return managerService.findAllInvoiceByCondition(param);
    }

    /**
     * 设备查询列表-开票记录
     * startDate        开始时间
     * endDate          结束时间
     * searchKey        关键词
     * pageNum          分页
     * pageSize         每页大小
     */
    @RequestMapping(value = {"/list"}, method = RequestMethod.POST)
    public Page<ResponseInvoice> list(@RequestBody RequestTrade param) throws Exception {
        return managerService.findInvoiceByCondition(param);
    }

    /**
     * 更新发票记录
     * invoiceId        发票id
     * type             0-发票未开，1-发票已开
     */
    @RequestMapping(value = {"/updateStatus"}, method = RequestMethod.POST)
    public String updateStatus(@RequestBody RequestInvoice param) throws Exception {
        return managerService.updateInvoiceStatus(param.getInvoiceId(), param.getType());
    }

    /**
     * 发票管理，查询发票列表
     * finish       0：发票未开，1：发票已开,2:已发出
     * type         发票类型，2表示增值税普通发票，3表示增值税专用发票
     * searchKey    关键词
     * pageNum      分页
     * pageSize     每页大小
     */
    @RequestMapping(value = {"/invoiceList"}, method = RequestMethod.POST)
    public Page<ResponseInvoice> invoiceList(@RequestBody RequestTrade param) throws Exception {
        return managerService.findInvoiceList(param);
    }

    /**
     * 快递发送发票
     * invoiceId            发票id
     * expressCompany       快递公司
     * expressNumber        快递单号
     * remarks              备注
     */
    @RequestMapping(value = {"/sendExpress"}, method = RequestMethod.POST)
    public String sendExpress(@RequestBody RequestInvoice param) throws Exception {
        return managerService.sendExpress(param);
    }

    /**
     * 上传电子发票
     * file
     */
    @RequestMapping(value = {"/uploadEInvoice"}, method = RequestMethod.POST)
    public String uploadEInvoice(@RequestParam(value = "file",required = false) MultipartFile file, String invoiceId) throws Exception{
        return managerService.uploadEInvoice(file, invoiceId);
    }

    /**
     * 发送电子发票
     * @return
     */
    @RequestMapping(value = {"/sendEInvoice"}, method = RequestMethod.POST)
    public String sendEInvoice(@RequestBody RequestInvoice param) throws Exception{
        return managerService.sendEInvoice(param.getInvoiceId(), param.getRemarks(), param.getFilePath());
    }

    /**
     * 发票管理，导出发票列表
     * finish           0：发票未开，1：发票已开,2:已发出
     * type             发票类型，2表示增值税普通发票，3表示增值税专用发票
     * searchKey        关键词
     */
    @RequestMapping(value = {"/exportInvoiceList"}, method = RequestMethod.POST)
    public void exportInvoiceList(@RequestBody RequestTrade param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        managerService.exportInvoiceList(param, request, response);
    }

}
