package com.ruoyi.web.arap.controller;

import com.ruoyi.web.arap.domain.ArapRefund;
import com.ruoyi.web.arap.service.IArapRefundService;
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.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.stock.service.IRoleOrgService;
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("/erp/arap/refund")
public class ArapRefundController extends BaseController {

    @Autowired
    private IArapRefundService arapRefundService;

    @Autowired
    private IRoleOrgService roleOrgService;

    @PreAuthorize("@ss.hasPermi('erp:arap:refund:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArapRefund query) {
        query.setOrgId(currentOrgId());
        startPage();
        List<ArapRefund> list = arapRefundService.selectList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:refund:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ArapRefund entity = arapRefundService.selectById(id);
        if (entity == null || !currentOrgId().equals(entity.getOrgId())) {
            return error("无权访问该数据");
        }
        return success(entity);
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:refund:add')")
    @Log(title = "客户退款登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArapRefund entity) {
        entity.setOrgId(currentOrgId());
        return toAjax(arapRefundService.add(entity, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:refund:edit')")
    @Log(title = "客户退款登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArapRefund entity) {
        ArapRefund old = arapRefundService.selectById(entity.getId());
        if (old == null || !currentOrgId().equals(old.getOrgId())) {
            return error("无权修改该数据");
        }
        entity.setOrgId(currentOrgId());
        return toAjax(arapRefundService.edit(entity, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:refund:remove')")
    @Log(title = "客户退款登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") String ids) {
        int rows = 0;
        for (Long id : Convert.toLongArray(ids)) {
            ArapRefund old = arapRefundService.selectById(id);
            if (old == null || !currentOrgId().equals(old.getOrgId())) {
                continue;
            }
            rows += arapRefundService.removeById(id, old.getOrgId(), getUsername());
        }
        return toAjax(rows);
    }

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

