package com.ruoyi.web.controller.buyerapi;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.util.IdUtil;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.buyerapi.domain.*;
import com.ruoyi.buyerapi.service.IBuyerUserService;
import com.ruoyi.buyerapi.service.IBuyerVoucherInfoService;
import com.ruoyi.buyerapi.service.IBuyerVoucherProjectService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.service.IBuyerAuxiliaryVoucherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 辅助帐凭证Controller
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/buyer/auxiliaryvoucher")
public class BuyerAuxiliaryVoucherController extends BaseController
{
    @Autowired
    private IBuyerAuxiliaryVoucherService buyerAuxiliaryVoucherService;

    @Autowired
    private IBuyerVoucherInfoService buyerVoucherInfoService;

    /**
     * 查询辅助帐凭证列表
     */
    @PreAuthorize("@ss.hasPermi('buyer:auxiliaryvoucher:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyerAuxiliaryVoucher buyerAuxiliaryVoucher)
    {
        startPage();
        buyerAuxiliaryVoucher.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerAuxiliaryVoucher> list = buyerAuxiliaryVoucherService.selectBuyerAuxiliaryVoucherList(buyerAuxiliaryVoucher);
        return getDataTable(list);
    }

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

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

    /**
     * 新增辅助帐凭证
     */
    @PreAuthorize("@ss.hasPermi('buyer:auxiliaryvoucher:add')")
    @Log(title = "辅助帐凭证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyerAuxiliaryVoucher buyerAuxiliaryVoucher)
    {
        buyerAuxiliaryVoucher.setId(IdUtil.simpleUUID());
        buyerAuxiliaryVoucher.setCreateBy(getUsername());
        return toAjax(buyerAuxiliaryVoucherService.insertBuyerAuxiliaryVoucher(buyerAuxiliaryVoucher));
    }

    /**
     * 修改辅助帐凭证
     */
    @PreAuthorize("@ss.hasPermi('buyer:auxiliaryvoucher:edit')")
    @Log(title = "辅助帐凭证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyerAuxiliaryVoucher buyerAuxiliaryVoucher)
    {
        buyerAuxiliaryVoucher.setUpdateBy(getUsername());
        return toAjax(buyerAuxiliaryVoucherService.updateBuyerAuxiliaryVoucher(buyerAuxiliaryVoucher));
    }

    /**
     * 删除辅助帐凭证
     */
    @PreAuthorize("@ss.hasPermi('buyer:auxiliaryvoucher:remove')")
    @Log(title = "辅助帐凭证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(buyerAuxiliaryVoucherService.deleteBuyerAuxiliaryVoucherByIds(ids));
    }

    /**
     * 刷新关联文件
     */
    @Log(title = "刷新关联文件", businessType = BusinessType.UPDATE)
    @PostMapping("/updateFileInfos")
    public AjaxResult updateFileInfos(@RequestBody BuyerAuxiliaryVoucher buyerAuxiliaryVoucher)
    {
        return toAjax(buyerAuxiliaryVoucherService.updateFileInfos(buyerAuxiliaryVoucher));
    }

    @ApiOperation(value = "下载模板文件")
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<BuyerAuxiliaryVoucherExcel> util = new ExcelUtil<BuyerAuxiliaryVoucherExcel>(BuyerAuxiliaryVoucherExcel.class);
        util.importTemplateExcel(response, "凭证导入模板");
    }

    @ApiOperation(value = "导入数据")
    @PostMapping("/importTable")
    public AjaxResult importTable(MultipartFile file) {
        try{
            return AjaxResult.success(buyerAuxiliaryVoucherService.importTable(file));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
