package com.zg.project.wisdom.controller;

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

import com.zg.project.wisdom.domain.dto.BorrowReturnDTO;
import com.zg.project.wisdom.domain.dto.RkBillCreateDTO;
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.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.zg.framework.aspectj.lang.annotation.Log;
import com.zg.framework.aspectj.lang.enums.BusinessType;
import com.zg.project.wisdom.domain.RkBill;
import com.zg.project.wisdom.service.IRkBillService;
import com.zg.framework.web.controller.BaseController;
import com.zg.framework.web.domain.AjaxResult;
import com.zg.common.utils.poi.ExcelUtil;
import com.zg.framework.web.page.TableDataInfo;

/**
 * 库存单据Controller
 * 
 * @author zg
 * @date 2026-01-20
 */
@RestController
@RequestMapping("/wisdom/bill")
public class RkBillController extends BaseController
{
    @Autowired
    private IRkBillService rkBillService;

    /**
     * 查询库存单据列表
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody RkBill rkBill) {
        startPage();
        List<RkBill> list = rkBillService.selectRkBillList(rkBill);
        return getDataTable(list);
    }

    /**
     * 导出库存单据列表
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:export')")
    @Log(title = "库存单据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RkBill rkBill)
    {
        List<RkBill> list = rkBillService.selectRkBillList(rkBill);
        ExcelUtil<RkBill> util = new ExcelUtil<RkBill>(RkBill.class);
        util.exportExcel(response, list, "库存单据数据");
    }

    /**
     * 获取库存单据详细信息
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rkBillService.selectRkBillById(id));
    }

    /**
     * 新增库存单据
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:add')")
    @Log(title = "库存单据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody RkBillCreateDTO dto) {
        return toAjax(rkBillService.insertRkBillAndDetail(dto));
    }

    /**
     * 向已有单据追加物料
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:add')")
    @Log(title = "库存单据", businessType = BusinessType.INSERT)
    @PostMapping("/append")
    public AjaxResult append(@RequestBody RkBillCreateDTO dto) {
        return toAjax(rkBillService.appendRkBillDetail(dto));
    }

    /**
     * 修改库存单据
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:edit')")
    @Log(title = "库存单据", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody RkBill rkBill)
    {
        return toAjax(rkBillService.updateRkBill(rkBill));
    }

    /**
     * 删除库存单据
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:remove')")
    @Log(title = "库存单据", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(@RequestBody Long[] ids)
    {
        return toAjax(rkBillService.deleteRkBillByIds(ids));
    }

    /**
     * 新增出库单据
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:out:add')")
    @Log(title = "出库单据", businessType = BusinessType.INSERT)
    @PostMapping("/out/add")
    public AjaxResult addOut(@RequestBody RkBillCreateDTO dto) {
        return toAjax(rkBillService.insertOutBillAndDetail(dto));
    }

    /**
     * 还料入库
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:return:add')")
    @Log(title = "还料", businessType = BusinessType.UPDATE)
    @PostMapping("/borrow/return")
    public AjaxResult returnBorrow(@RequestBody BorrowReturnDTO dto) {
        return toAjax(rkBillService.returnBorrow(dto));
    }

}
