package com.ruoyi.web.controller.buyerapi;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.poi.excel.ExcelWriter;
import com.ruoyi.buyerapi.domain.BuyerVoucherInfoExport;
import com.ruoyi.buyerapi.domain.BuyerVoucherInfoTotalExport;
import com.ruoyi.common.utils.SecurityUtils;
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.BuyerVoucherInfo;
import com.ruoyi.buyerapi.service.IBuyerVoucherInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 辅助帐凭证详细列Controller
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/buyer/voucherinfo")
public class BuyerVoucherInfoController extends BaseController
{
    @Autowired
    private IBuyerVoucherInfoService buyerVoucherInfoService;

    /**
     * 查询辅助帐凭证详细列列表
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyerVoucherInfo buyerVoucherInfo)
    {
        startPage();
        List<BuyerVoucherInfo> list = buyerVoucherInfoService.selectBuyerVoucherInfoList(buyerVoucherInfo);
        return getDataTable(list);
    }

    /**
     * 查询辅助帐凭证详细列列表（导出）
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:list')")
    @Log(title = "辅助帐凭证详细列导出", businessType = BusinessType.EXPORT)
    @PostMapping("/selectBuyerVoucherInfoListExport")
    public void selectBuyerVoucherInfoListExport(HttpServletResponse response, String projectId) throws IOException {
        List<BuyerVoucherInfoExport> list = buyerVoucherInfoService.selectBuyerVoucherInfoListExport(projectId);
        ExcelUtil<BuyerVoucherInfoExport> util = new ExcelUtil<BuyerVoucherInfoExport>(BuyerVoucherInfoExport.class);
        util.exportExcel(response, list, "辅助帐导出列表");
    }

    /**
     * 查询辅助帐凭证详细列列表
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:list')")
    @GetMapping("/voucherInfoViewList")
    public TableDataInfo voucherInfoViewList(String projectId) {
        List<BuyerVoucherInfoExport> list = buyerVoucherInfoService.selectBuyerVoucherInfoListExport(projectId);
        return getDataTable(list);
    }

    /**
     * 汇总辅助帐（导出）
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:list')")
    @Log(title = "辅助帐凭证详细列导出", businessType = BusinessType.EXPORT)
    @PostMapping("/totalVoucherInfoListExport")
    public void totalVoucherInfoListExport(HttpServletResponse response, String[] projectIds) throws IOException {
        List<BuyerVoucherInfoTotalExport> list = buyerVoucherInfoService.totalVoucherInfoList(projectIds);
        ExcelUtil<BuyerVoucherInfoTotalExport> util = new ExcelUtil<BuyerVoucherInfoTotalExport>(BuyerVoucherInfoTotalExport.class);
        util.exportExcel(response, list, "汇总辅助帐导出列表");
    }

    /**
     * 汇总辅助帐
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:list')")
    @PostMapping("/totalVoucherInfoList")
    public TableDataInfo totalVoucherInfoList(@RequestBody String[] projectIds) {
        List<BuyerVoucherInfoTotalExport> list = buyerVoucherInfoService.totalVoucherInfoList(projectIds);
        return getDataTable(list);
    }

    /**
     * 导出辅助帐凭证详细列列表
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:export')")
    @Log(title = "辅助帐凭证详细列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyerVoucherInfo buyerVoucherInfo)
    {
        List<BuyerVoucherInfo> list = buyerVoucherInfoService.selectBuyerVoucherInfoList(buyerVoucherInfo);
        ExcelUtil<BuyerVoucherInfo> util = new ExcelUtil<BuyerVoucherInfo>(BuyerVoucherInfo.class);
        util.exportExcel(response, list, "辅助帐凭证详细列数据");
    }

    /**
     * 获取辅助帐凭证详细列详细信息
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(buyerVoucherInfoService.selectBuyerVoucherInfoById(id));
    }

    /**
     * 新增辅助帐凭证详细列
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:add')")
    @Log(title = "辅助帐凭证详细列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyerVoucherInfo buyerVoucherInfo)
    {
        buyerVoucherInfo.setId(IdUtil.simpleUUID());
        buyerVoucherInfo.setCreateBy(getUsername());
        return toAjax(buyerVoucherInfoService.insertBuyerVoucherInfo(buyerVoucherInfo));
    }

    /**
     * 修改辅助帐凭证详细列
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:edit')")
    @Log(title = "辅助帐凭证详细列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyerVoucherInfo buyerVoucherInfo)
    {
        buyerVoucherInfo.setUpdateBy(getUsername());
        return toAjax(buyerVoucherInfoService.updateBuyerVoucherInfo(buyerVoucherInfo));
    }

    /**
     * 删除辅助帐凭证详细列
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherinfo:remove')")
    @Log(title = "辅助帐凭证详细列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(buyerVoucherInfoService.deleteBuyerVoucherInfoByIds(ids));
    }
}
