package com.zg.project.wisdom.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.RkRecord;
import com.zg.project.wisdom.service.IRkRecordService;
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/record")
public class RkRecordController extends BaseController
{
    @Autowired
    private IRkRecordService rkRecordService;

    /**
     * 查询出入库记录列表
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(RkRecord rkRecord)
    {
        startPage();
        List<RkRecord> list = rkRecordService.selectRkRecordList(rkRecord);
        return getDataTable(list);
    }

    /**
     * 根据单据号查询出入库记录明细
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:list')")
    @GetMapping("/listByBillNo/{billNo}")
    public AjaxResult listByBillNo(@PathVariable String billNo)
    {
        return success(rkRecordService.selectRkRecordListByBillNo(billNo));
    }

    /**
     * 导出出入库记录列表
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:export')")
    @Log(title = "出入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RkRecord rkRecord)
    {
        List<RkRecord> list = rkRecordService.selectRkRecordList(rkRecord);
        ExcelUtil<RkRecord> util = new ExcelUtil<RkRecord>(RkRecord.class);
        util.exportExcel(response, list, "出入库记录数据");
    }

    /**
     * 获取出入库记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rkRecordService.selectRkRecordById(id));
    }

    /**
     * 新增出入库记录
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:add')")
    @Log(title = "出入库记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RkRecord rkRecord)
    {
        return toAjax(rkRecordService.insertRkRecord(rkRecord));
    }

    /**
     * 修改出入库记录
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:edit')")
    @Log(title = "出入库记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody RkRecord rkRecord)
    {
        return toAjax(rkRecordService.updateRkRecord(rkRecord));
    }

    /**
     * 删除入库记录
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:remove')")
    @Log(title = "入库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult remove(@RequestBody Long[] ids)
    {
        return toAjax(rkRecordService.deleteRkRecordByIds(ids));
    }

    /**
     * 撤销入库（回退为预入库）
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:rollback')")
    @Log(title = "入库撤销", businessType = BusinessType.UPDATE)
    @PostMapping("/rollback")
    public AjaxResult rollback(@RequestBody RkRecord record) {
        return toAjax(rkRecordService.rollbackToPreIn(record.getIds()));
    }

    /**
     * 一键入库（批量完成入库）
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:finish')")
    @Log(title = "一键入库", businessType = BusinessType.UPDATE)
    @PostMapping("/finish")
    public AjaxResult finish(@RequestBody RkRecord record) {
        return toAjax(rkRecordService.finishIn(record.getIds()));
    }

    /**
     * 撤销出库
     */
    @PreAuthorize("@ss.hasPermi('wisdom:bill:out:rollback')")
    @Log(title = "撤销出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out/rollback")
    public AjaxResult rollbackOut(@RequestBody RkRecord record) {
        return toAjax(rkRecordService.rollbackOut(record.getIds()));
    }

    /**
     * 一键出库（批量完成出库）
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:out:finish')")
    @Log(title = "一键出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out/finish")
    public AjaxResult finishOut(@RequestBody RkRecord record) {
        return toAjax(rkRecordService.finishOut(record.getIds()));
    }

    /**
     * 删除预出库记录
     */
    @PreAuthorize("@ss.hasPermi('wisdom:record:out:remove')")
    @Log(title = "预出库记录删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/out/delete")
    public AjaxResult deletePreOut(@RequestBody Long[] ids) {
        return toAjax(rkRecordService.deletePreOutRecords(ids));
    }

    /**
     * 出入库统计（同时返回）
     */
    @PostMapping("/statistic")
    public AjaxResult statistic(@RequestBody RkRecord query) {
        return AjaxResult.success(rkRecordService.getRecordStatistic(query));
    }

}
