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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.jsonUtil;
import com.ruoyi.web.v2.v1.model.JsDrug;
import com.ruoyi.web.v2.v1.service.IJsDrugService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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>
 * v2.0违禁药物名称 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-21
 */
@RestController
@RequestMapping("/js-drug")
@Api("2.0违禁药物名称")
public class JsDrugController {

    @Autowired
    private IJsDrugService drugService;

    @ApiOperation("违禁药物管理添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody JsDrug jsDrug) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonArray = mapper.readTree(jsDrug.getResult());
        String fieldToCheck = "name";
        if (jsonUtil.checkForDuplicateFields(jsonArray, fieldToCheck)) {
            return error("新增药物'" + jsDrug.getDrugName() + "'失败，本次添加中存在重复的检查结果");
        }
        int i = drugService.count(new QueryWrapper<JsDrug>().eq("drug_name", jsDrug.getDrugName()));
        if (i != 0) {
            return error("该药物已存在");
        }
        drugService.save(jsDrug);
        return success();
    }

    @ApiOperation("违禁药物管理修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody JsDrug jsDrug) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonArray = mapper.readTree(jsDrug.getResult());
        String fieldToCheck = "name";
        if (jsonUtil.checkForDuplicateFields(jsonArray, fieldToCheck)) {
            return error("修改药物'" + jsDrug.getDrugName() + "'失败，本次添加中存在重复的检查结果");
        }
        int i = drugService.count(new QueryWrapper<JsDrug>().eq("drug_name", jsDrug.getDrugName()).ne("id",jsDrug.getId()));
        if (i != 0) {
            return error("该药物已存在");
        }
        drugService.updateById(jsDrug);
        return success();
    }

    @ApiOperation("违禁药物管理删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        for (String s : split) {
            drugService.removeById(s);
        }
        return success();
    }

    @ApiOperation("违禁药物管理列表")
    @PostMapping("/list")
    public AjaxResult listAll(@RequestBody Map<String, String> param){
        if(!ObjectUtils.isEmpty(param) && !ObjectUtils.isEmpty(param.getOrDefault("type", null))) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("type", param.get("type"));
            return success(drugService.list(wrapper));
        }
        return success(drugService.list());
    }

    @ApiOperation("违禁药物管理分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "drugName", required = false) String drugName,
                           @RequestParam(value = "type", required = false) String type) {
        Page<JsDrug> page = new Page<>(pageNum, pageSize);
        QueryWrapper<JsDrug> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(drugName), "drug_name", drugName);
        queryWrapper.eq("type", type);
        return success(drugService.page(page, queryWrapper));
    }

    @ApiOperation("违禁药物管理详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(drugService.getById(id));
    }
}
