package com.ruoyi.web.arap.controller;

import com.ruoyi.web.arap.domain.vo.ArapDeductItemVo;
import com.ruoyi.web.arap.service.IArapDeductService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.stock.service.IRoleOrgService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/erp/arap/deduct")
public class ArapDeductController extends BaseController {

    @Autowired
    private IArapDeductService arapDeductService;

    @Autowired
    private IRoleOrgService roleOrgService;

    @PreAuthorize("@ss.hasPermi('erp:arap:deduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "bizDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date bizDate) {
        startPage();
        List<ArapDeductItemVo> list = arapDeductService.listByDate(currentOrgId(), bizDate);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:deduct:run')")
    @Log(title = "应收单扣款作业", businessType = BusinessType.UPDATE)
    @PostMapping("/batchDeduct")
    public AjaxResult batchDeduct(@RequestBody List<ArapDeductItemVo> rows) {
        return toAjax(arapDeductService.batchDeduct(currentOrgId(), getUsername(), rows));
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:deduct:reverse')")
    @Log(title = "应收单扣款作业", businessType = BusinessType.UPDATE)
    @PostMapping("/batchUndeduct")
    public AjaxResult batchUndeduct(@RequestBody List<ArapDeductItemVo> rows) {
        return toAjax(arapDeductService.batchUndeduct(currentOrgId(), getUsername(), rows));
    }

    private String currentOrgId() {
        return roleOrgService.resolveOrgId(getUserId(), getDeptId());
    }
}

