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.ProductionPlant;
import com.ruoyi.web.base.domain.ProductionPlan;
import com.ruoyi.web.base.service.IProductionPlantService;
import com.ruoyi.web.base.service.IProductionPlanService;
import com.ruoyi.web.base.service.IProductionPlantService;
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-plant")
public class ProductionPlantController {
    @Autowired
    private IProductionPlantService productionPlantService;

    @Autowired
    private TokenService tokenService;

    @ApiOperation("车间信息表添加")
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody ProductionPlant productionPlant, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionPlantService.count(new QueryWrapper<ProductionPlant>().eq("org_id",loginOrgId).eq("production_plant_no", productionPlant.getProductionPlantNo())) >0 ){
            throw new Exception("该编号已存在");
        }
        String username = getUsername();
        productionPlant.setOrgId(loginOrgId);
        productionPlant.setCreateBy(username);

        return success(productionPlantService.save(productionPlant));
    }

    @ApiOperation("车间信息表修改")
    @PostMapping("/edit")
    @Transactional
    public AjaxResult edit(@RequestBody ProductionPlant productionPlant, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionPlantService.count(new QueryWrapper<ProductionPlant>().ne("id",productionPlant.getId()).eq("org_id",loginOrgId).eq("production_plant_no", productionPlant.getProductionPlantNo())) >0 ){
            throw new Exception("该编号已存在");
        }
        String username = getUsername();
        productionPlant.setOrgId(loginOrgId);
        productionPlant.setCreateBy(username);
        productionPlant.setUpdateBy(username);
        return success(productionPlantService.updateById(productionPlant));
    }

    @ApiOperation("车间信息表删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            productionPlantService.removeById(s);
        }
        return success();
    }

    @ApiOperation("车间信息表列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request){
        List<ProductionPlant> org_id = productionPlantService.list(new QueryWrapper<ProductionPlant>().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<ProductionPlant> org_id = productionPlantService.page(new Page<ProductionPlant>(pageNum, pageSize), new QueryWrapper<ProductionPlant>().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");
        ProductionPlant byId = productionPlantService.getById(id);
        return success(byId);
    }

}
