package com.zg.project.wisdom.controller;

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

import com.zg.project.wisdom.domain.dto.PcRkInfoBatchDTO;
import com.zg.project.wisdom.domain.dto.RkInfoBatchDTO;
import com.zg.project.wisdom.domain.dto.StockOutDTO;
import com.zg.project.wisdom.service.IGysJhService;
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.zg.framework.aspectj.lang.annotation.Log;
import com.zg.framework.aspectj.lang.enums.BusinessType;
import com.zg.project.wisdom.domain.RkInfo;
import com.zg.project.wisdom.service.IRkInfoService;
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 2025-05-28
 */
@RestController
@RequestMapping("/wisdom/stock")
public class RkInfoController extends BaseController
{
    @Autowired
    private IRkInfoService rkInfoService;

    /**
     * 查询库存单据主列表
     */
    @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);
    }

    /**
     * 导出库存单据主列表
     */
    @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));
    }

    /**
     * 新增库存单据主
     */
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('wisdom:stock:add')")
    @Log(title = "库存单据主", businessType = BusinessType.INSERT)
    public AjaxResult batchAdd(@RequestBody PcRkInfoBatchDTO dto) {
        return toAjax(rkInfoService.batchInsert(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));
    }

    /**
     * 库龄超过20天统计
     * @return
     */
    @GetMapping("/warning/stock/over20")
    @PreAuthorize("@ss.hasPermi('wisdom:stock:warning')")
    public AjaxResult getOverdueStockTopList() {
        int total = rkInfoService.countOverdueStock();
        List<RkInfo> list = rkInfoService.selectTopOverdueStock(20);
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("list", list);
        return AjaxResult.success(result);
    }

    /**
     * 出库操作
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wisdom:outbound:add')")
    @Log(title = "库存单据主", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOutStock")
    public AjaxResult updateOutStock(@RequestBody StockOutDTO dto) {
        return toAjax(rkInfoService.updateOutStock(dto));
    }
}
