package com.ruoyi.web.v2.v1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.v2.v1.entity.BaseDisLocation;
import com.ruoyi.web.v2.v1.service.IBaseDisLocationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-12-01
 */
@RestController
@RequestMapping("/base-dis-location")
public class BaseDisLocationController {
    @Autowired
    private IBaseDisLocationService baseDisLocationService;

    @ApiOperation("检验项目管理添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BaseDisLocation checkProject) throws JsonProcessingException {
        if (baseDisLocationService.count(new QueryWrapper<BaseDisLocation>().eq(
                "location_name",checkProject.getLocationName()))>0){
            error("该名字已存在");
        }
        return success(baseDisLocationService.save(checkProject));
    }

    @ApiOperation("检验项目管理修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BaseDisLocation checkProject) throws JsonProcessingException {
        if (baseDisLocationService.count(new QueryWrapper<BaseDisLocation>().eq(
                "location_name",checkProject.getLocationName()).ne("id",checkProject.getId()))>0){
            error("该名字已存在");
        }
        return success(baseDisLocationService.updateById(checkProject));
    }

    @ApiOperation("检验项目管理删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            BaseDisLocation byId = baseDisLocationService.getById(s);
            if (byId.getLocationDel() == 1){
                error("系统地点，不能删除");
            }
            baseDisLocationService.removeById(s);
        }
      return success();
    }

    @ApiOperation("检验项目管理列表")
    @PostMapping("/list")
    public AjaxResult listAll(){
        return success(baseDisLocationService.list());
    }

    @ApiOperation("检验项目管理分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize ) {
        return success(baseDisLocationService.page(new Page<BaseDisLocation>(pageNum, pageSize)));
    }

    @ApiOperation("检验项目管理详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(baseDisLocationService.getById(id));
    }

}
