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.ProductionBatchProcessingType;
import com.ruoyi.web.base.service.IProductionBatchProcessingTypeService;
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-03-19
 */
@RestController
@RequestMapping("/production-batch-processing-type")
public class ProductionBatchProcessingTypeController {
    @Autowired
    private IProductionBatchProcessingTypeService productionBatchProcessingTypeService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("生产批次加工类型添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionBatchProcessingType productionBatchProcessingType, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        productionBatchProcessingType.setOrgId(loginOrgId);
        productionBatchProcessingType.setCreateBy(getUsername());
        return success(productionBatchProcessingTypeService.save(productionBatchProcessingType));
    }

    @ApiOperation("生产批次加工类型修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionBatchProcessingType productionBatchProcessingType, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        productionBatchProcessingType.setUpdateBy(getUsername());
        return success(productionBatchProcessingTypeService.updateById(productionBatchProcessingType));
    }

    @ApiOperation("生产批次加工类型删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionBatchProcessingTypeService.removeById(id);
        }
        return success();
    }

    @ApiOperation("生产批次加工类型列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionBatchProcessingTypeService.list(
                new QueryWrapper<ProductionBatchProcessingType>().eq("org_id", tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("生产批次加工类型分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize,
                           HttpServletRequest request) {
        return success(productionBatchProcessingTypeService.page(
                new Page<>(pageNum, pageSize),
                new QueryWrapper<ProductionBatchProcessingType>().eq("org_id", tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("生产批次加工类型详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionBatchProcessingTypeService.getById(id));
    }
}
