package com.ruoyi.web.arap.controller;

import com.ruoyi.web.arap.domain.ArapOffset;
import com.ruoyi.web.arap.domain.vo.ArapOpenPayVo;
import com.ruoyi.web.arap.service.IArapOffsetService;
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.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
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/offset")
public class ArapOffsetController extends BaseController {

    @Autowired
    private IArapOffsetService arapOffsetService;
    @Autowired
    private IRoleOrgService roleOrgService;

    @PreAuthorize("@ss.hasPermi('erp:arap:offset:list')")
    @GetMapping("/openPay/list")
    public TableDataInfo openPayList(ArapOffset query) {
        query.setOrgId(currentOrgId());
        startPage();
        List<ArapOpenPayVo> list = arapOffsetService.listOpenPay(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:offset:list')")
    @GetMapping("/order/list")
    public AjaxResult orderList(@RequestParam("custNo") String custNo,
                                @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                @RequestParam(value = "remark", required = false) String remark) {
        return success(arapOffsetService.listOrderOpen(currentOrgId(), custNo, startDate, endDate, remark));
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:offset:save')")
    @Log(title = "冲销作业", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody ArapOffset form) {
        return toAjax(arapOffsetService.saveOffset(form, currentOrgId(), getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:offset:list')")
    @GetMapping("/record/list")
    public TableDataInfo recordList(ArapOffset query) {
        query.setOrgId(currentOrgId());
        startPage();
        return getDataTable(arapOffsetService.listOffset(query));
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:offset:list')")
    @GetMapping("/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        ArapOffset data = arapOffsetService.getOffset(id, currentOrgId());
        if (data == null) return error("记录不存在");
        return success(data);
    }

    @PreAuthorize("@ss.hasPermi('erp:arap:offset:reverse')")
    @Log(title = "反冲销作业", businessType = BusinessType.UPDATE)
    @PostMapping("/reverse")
    public AjaxResult reverse(@RequestBody ArapOffset form) {
        return toAjax(arapOffsetService.reverseOffset(form, currentOrgId(), getUsername()));
    }

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

