package com.zg.project.wisdom.controller.app;

import com.zg.common.utils.poi.ExcelUtil;
import com.zg.framework.aspectj.lang.annotation.Log;
import com.zg.framework.aspectj.lang.enums.BusinessType;
import com.zg.framework.web.controller.BaseController;
import com.zg.framework.web.domain.AjaxResult;
import com.zg.framework.web.page.TableDataInfo;
import com.zg.project.wisdom.domain.RkBill;
import com.zg.project.wisdom.domain.RkInfo;
import com.zg.project.wisdom.domain.dto.RkInfoBatchDTO;
import com.zg.project.wisdom.service.IRkBillService;
import com.zg.project.wisdom.service.IRkInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 库存单据主Controller
 *
 * @author zg
 * @date 2025-05-28
 */
@RestController
@RequestMapping("/app/wisdom/stock")
public class AppRkInfoController extends BaseController {

    @Autowired
    private IRkInfoService rkInfoService;

    @Autowired
    private IRkBillService rkBillService;

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

    /**
     * 查询库存单据主列表
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:list')")
//    @GetMapping("/details/list")
    @GetMapping("/list")
    public TableDataInfo list(RkInfo rkInfo)
    {
        startPage();
        List<RkInfo> list = rkInfoService.selectRkInfoList(rkInfo);
        return getDataTable(list);
    }


    /**
     * 根据单据号进行撤销
     * @param billNo
     * @return
     */
    @DeleteMapping("/deleteByBillNo/{billNo}")
    @PreAuthorize("@ss.hasPermi('wisdom:stock:deleteByBillNo')")
    public AjaxResult deleteByBillNo(@PathVariable String billNo) {
        rkInfoService.deleteByBillNo(billNo);
        return AjaxResult.success("撤销成功");
    }

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

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

    /**
     * 新增库存单据主
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:add')")
    @Log(title = "库存单据主", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult batchAdd(@RequestBody RkInfoBatchDTO dto) {
        return toAjax(rkInfoService.batchInsertApp(dto));
    }

    /**
     * 修改库存单据主
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:edit')")
    @Log(title = "库存单据主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RkInfo rkInfo)
    {
        return toAjax(rkInfoService.updateRkInfo(rkInfo));
    }

    /**
     * 删除库存单据主
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:remove')")
    @Log(title = "库存单据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rkInfoService.deleteRkInfoByIds(ids));
    }



}
