package com.ruoyi.web.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProcureSupplier;
import com.ruoyi.web.base.service.IProcureSupplierService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 供应商管理 前端控制器
 * </p>
 *
 * @author author
 * @since 2026-02-05
 */
@RestController
@RequestMapping("/procure-supplier")
public class ProcureSupplierController {
    @Autowired
    private IProcureSupplierService procureSupplierService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("自动转级别管理添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProcureSupplier procureSupplier, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (procureSupplierService.count(new QueryWrapper<ProcureSupplier>().eq("org_id",loginOrgId).eq("supplier_num", procureSupplier.getSupplierNum())) >0 ){
            throw new Exception("该编号已存在");
        }
        procureSupplier.setOrgId(loginOrgId);
        procureSupplier.setCreateBy(getUsername());

        return success(procureSupplierService.save(procureSupplier));
    }

    @ApiOperation("自动转级别管理修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProcureSupplier procureSupplier, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (procureSupplierService.count(new QueryWrapper<ProcureSupplier>().ne("id",procureSupplier.getId()).eq("org_id",loginOrgId).eq("supplier_num", procureSupplier.getSupplierNum())) >0 ){
            throw new Exception("该编号已存在");
        }
        procureSupplier.setUpdateBy(getUsername());
        return success(procureSupplierService.updateById(procureSupplier));
    }

    @ApiOperation("自动转级别管理删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            procureSupplierService.removeById(s);
        }
        return success();
    }

    @ApiOperation("自动转级别管理列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request){

        return success(procureSupplierService.list(new QueryWrapper<ProcureSupplier>().eq("org_id", tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("自动转级别管理分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , HttpServletRequest request) {
        return success(procureSupplierService.page(new Page<ProcureSupplier>(pageNum,pageSize),new QueryWrapper<ProcureSupplier>().eq("org_id", tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("自动转级别管理详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(procureSupplierService.getById(id));
    }


}
