package com.zg.project.wisdom.controller;

import com.zg.common.utils.DateUtils;
import com.zg.common.utils.SecurityUtils;
import com.zg.framework.web.controller.BaseController;
import com.zg.framework.web.domain.AjaxResult;
import com.zg.project.wisdom.domain.RkBill;
import com.zg.framework.web.page.TableDataInfo;
import com.zg.project.wisdom.domain.RkInfo;
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 java.util.List;

@RestController
@RequestMapping("/wisdom/bill")
public class RkBillController extends BaseController {

    @Autowired
    private IRkBillService rkBillService;

    @Autowired
    private IRkInfoService rkInfoService;

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

    /**
     * 根据ID获取详情
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(rkBillService.selectRkBillById(id));
    }

    /**
     * 新增单据主（如未通过入库明细新增）
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:add')")
    @PostMapping
    public AjaxResult add(@RequestBody RkBill rkBill) {
        rkBill.setCreateBy(SecurityUtils.getUsername());
        rkBill.setCreateTime(DateUtils.getNowDate());
        return toAjax(rkBillService.insertRkBill(rkBill));
    }

    /**
     * 修改单据主
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody RkBill rkBill) {
        rkBill.setUpdateBy(SecurityUtils.getUsername());
        rkBill.setUpdateTime(DateUtils.getNowDate());
        return toAjax(rkBillService.updateRkBill(rkBill));
    }

    /**
     * 删除单据主（逻辑删除）
     */
    @PreAuthorize("@ss.hasPermi('wisdom:stock:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rkBillService.logicDeleteRkBillByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('wisdom:stock:query')")
    @GetMapping("/detailList")
    public TableDataInfo detailList(@RequestParam String billNo) {
        RkInfo rkInfo = new RkInfo();
        rkInfo.setBillNo(billNo);
        startPage();
        List<RkInfo> list = rkInfoService.selectRkInfoList(rkInfo);
        return getDataTable(list);
    }
}
