package com.ruoyi.web.controller.buyerapi;

import java.util.List;
import cn.hutool.core.util.IdUtil;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.buyerapi.domain.BuyerVoucherProjectGroupBySubject;
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.BuyerVoucherProject;
import com.ruoyi.buyerapi.service.IBuyerVoucherProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 辅助帐凭证项目关系Controller
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/buyer/voucherProject")
public class BuyerVoucherProjectController extends BaseController
{
    @Autowired
    private IBuyerVoucherProjectService buyerVoucherProjectService;

    /**
     * 查询辅助帐凭证项目关系列表
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherProject:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyerVoucherProject buyerVoucherProject)
    {
        startPage();
        List<BuyerVoucherProject> list = buyerVoucherProjectService.selectBuyerVoucherProjectList(buyerVoucherProject);
        return getDataTable(list);
    }

    /**
     * 导出辅助帐凭证项目关系列表
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherProject:export')")
    @Log(title = "辅助帐凭证项目关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyerVoucherProject buyerVoucherProject)
    {
        List<BuyerVoucherProject> list = buyerVoucherProjectService.selectBuyerVoucherProjectList(buyerVoucherProject);
        ExcelUtil<BuyerVoucherProject> util = new ExcelUtil<BuyerVoucherProject>(BuyerVoucherProject.class);
        util.exportExcel(response, list, "辅助帐凭证项目关系数据");
    }

    /**
     * 获取辅助帐凭证项目关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherProject:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(buyerVoucherProjectService.selectBuyerVoucherProjectById(id));
    }

    /**
     * 新增辅助帐凭证项目关系
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherProject:add')")
    @Log(title = "辅助帐凭证项目关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyerVoucherProject buyerVoucherProject)
    {
        buyerVoucherProject.setId(IdUtil.simpleUUID());
        buyerVoucherProject.setCreateBy(getUsername());
        return toAjax(buyerVoucherProjectService.insertBuyerVoucherProject(buyerVoucherProject));
    }

    /**
     * 批量新增辅助帐凭证项目关系
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherProject:add')")
    @Log(title = "批量新增辅助帐凭证项目关系", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addBatch")
    public AjaxResult addBatch(@RequestBody List<BuyerVoucherProjectGroupBySubject> buyerVoucherProjects)
    {
        for(BuyerVoucherProjectGroupBySubject subject : buyerVoucherProjects){
            for(int i = 0; i < subject.getBuyerVoucherProjects().size(); i++){
                BuyerVoucherProject buyerVoucherProject = subject.getBuyerVoucherProjects().get(i);
                buyerVoucherProject.setId(IdUtil.simpleUUID());
                buyerVoucherProject.setSubject(subject.getSubject());
                buyerVoucherProject.setCreateBy(getUsername());
                buyerVoucherProject.setCompanyId(SecurityUtils.getCompanyId());
                buyerVoucherProject.setOrderIndex(i);
                buyerVoucherProjectService.insertBuyerVoucherProject(buyerVoucherProject);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改辅助帐凭证项目关系
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherProject:edit')")
    @Log(title = "辅助帐凭证项目关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyerVoucherProject buyerVoucherProject)
    {
        buyerVoucherProject.setUpdateBy(getUsername());
        return toAjax(buyerVoucherProjectService.updateBuyerVoucherProject(buyerVoucherProject));
    }

    /**
     * 删除辅助帐凭证项目关系
     */
    @PreAuthorize("@ss.hasPermi('buyer:voucherProject:remove')")
    @Log(title = "辅助帐凭证项目关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(buyerVoucherProjectService.deleteBuyerVoucherProjectByIds(ids));
    }
}
