package com.ruoyi.web.controller.buyerapi;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
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.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.buyerapi.domain.BuyerBill;
import com.ruoyi.buyerapi.service.IBuyerBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统发票Controller
 * 
 * @author ruoyi
 * @date 2022-09-19
 */
@Api(tags = "系统发票")
@RestController
@RequestMapping("/buyer/bill")
public class BuyerBillController extends BaseController
{
    @Autowired
    private IBuyerBillService buyerBillService;

    /**
     * 查询系统发票列表
     */
    @ApiOperation(value = "查询系统发票列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "no", value = "订单号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "billStatus", value = "开票状态（0未开票1已开票）", required = true, paramType = "query"),
            @ApiImplicitParam(name = "billType", value = "发票类型(0电子普通发票1增值税专用发票)", required = true, paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('system:bill:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyerBill buyerBill)
    {
        startPage();
        buyerBill.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerBill> list = buyerBillService.selectBuyerBillList(buyerBill);
        return getDataTable(list);
    }

    /**
     * 导出系统发票列表
     */
    @ApiOperation(value = "导出系统发票列表")
    @PreAuthorize("@ss.hasPermi('system:bill:export')")
    @Log(title = "系统发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyerBill buyerBill)
    {
        List<BuyerBill> list = buyerBillService.selectBuyerBillList(buyerBill);
        ExcelUtil<BuyerBill> util = new ExcelUtil<BuyerBill>(BuyerBill.class);
        util.exportExcel(response, list, "系统发票数据");
    }

    /**
     * 获取系统发票详细信息
     */
    @ApiOperation(value = "获取系统发票详细信息")
    @PreAuthorize("@ss.hasPermi('system:bill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(buyerBillService.selectBuyerBillById(id));
    }

    /**
     * 新增系统发票
     */
    @ApiOperation(value = "新增系统发票")
    @PreAuthorize("@ss.hasPermi('system:bill:add')")
    @Log(title = "系统发票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyerBill buyerBill)
    {
        buyerBill.setId(IdUtil.simpleUUID());
        buyerBill.setCreateBy(getUsername());
        return toAjax(buyerBillService.insertBuyerBill(buyerBill));
    }

    /**
     * 修改系统发票
     */
    @ApiOperation(value = "修改系统发票")
    @PreAuthorize("@ss.hasPermi('system:bill:edit')")
    @Log(title = "系统发票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyerBill buyerBill)
    {
        buyerBill.setUpdateBy(getUsername());
        return toAjax(buyerBillService.updateBuyerBill(buyerBill));
    }

    /**
     * 删除系统发票
     */
    @ApiOperation(value = "删除系统发票")
    @PreAuthorize("@ss.hasPermi('system:bill:remove')")
    @Log(title = "系统发票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(buyerBillService.deleteBuyerBillByIds(ids));
    }
}
