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.ProductionMaterialMapping;

import com.ruoyi.web.base.service.IProductionMaterialMappingService;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
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-16
 */
@RestController
@RequestMapping("/production-material-mapping")
public class ProductionMaterialMappingController {
    @Autowired
    private IProductionMaterialMappingService productionMaterialMappingService;
  
    @Autowired
    private TokenService tokenService;

    @ApiOperation("物料生产对应添加")
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody ProductionMaterialMapping productionMaterialMapping, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionMaterialMappingService.count(new QueryWrapper<ProductionMaterialMapping>().eq("org_id",loginOrgId).eq("goods_num", productionMaterialMapping.getGoodsNum())) >0 ){
            throw new Exception("该编号已存在");
        }
        String username = getUsername();
        productionMaterialMapping.setOrgId(loginOrgId);
        productionMaterialMapping.setCreateBy(username);
       
        return success(productionMaterialMappingService.save(productionMaterialMapping));
    }

    @ApiOperation("物料生产对应修改")
    @PostMapping("/edit")
    @Transactional
    public AjaxResult edit(@RequestBody ProductionMaterialMapping productionMaterialMapping, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionMaterialMappingService.count(new QueryWrapper<ProductionMaterialMapping>().ne("id",productionMaterialMapping.getId()).eq("org_id",loginOrgId).eq("production_plant_no", productionMaterialMapping.getProductionPlantNo())) >0 ){
            throw new Exception("该编号已存在");
        }
        String username = getUsername();
        productionMaterialMapping.setOrgId(loginOrgId);
        productionMaterialMapping.setCreateBy(username);
        productionMaterialMapping.setUpdateBy(username);
        return success(productionMaterialMappingService.updateById(productionMaterialMapping));
    }

    @ApiOperation("物料生产对应删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            productionMaterialMappingService.removeById(s);
        }
        return success();
    }

    @ApiOperation("物料生产对应列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request){
        List<ProductionMaterialMapping> org_id = productionMaterialMappingService.list(new QueryWrapper<ProductionMaterialMapping>().eq("org_id", tokenService.getLoginOrgId(request)));

        return success(org_id);
    }

    @ApiOperation("物料生产对应分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , HttpServletRequest request) {
        Page<ProductionMaterialMapping> org_id = productionMaterialMappingService.page(new Page<ProductionMaterialMapping>(pageNum, pageSize), new QueryWrapper<ProductionMaterialMapping>().eq("org_id", tokenService.getLoginOrgId(request)));

        return success(org_id);
    }

    @ApiOperation("物料生产对应详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        ProductionMaterialMapping byId = productionMaterialMappingService.getById(id);
        return success(byId);
    }
}
