package com.ruoyi.web.v2.v1.service.impl;

import cn.hutool.core.util.ObjectUtil;
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.entity.JsEnvCheckProject;
import com.ruoyi.web.v2.v1.mapper.JsEnvCheckProjectMapper;
import com.ruoyi.web.v2.v1.service.IJsEnvCheckProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 2.0检验项目管理 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-11-14
 */
@Service
public class JsEnvCheckProjectServiceImpl extends ServiceImpl<JsEnvCheckProjectMapper, JsEnvCheckProject> implements IJsEnvCheckProjectService {
    @Autowired
    private JsEnvCheckProjectMapper projectMapper;

    @Override
    public AjaxResult add(JsEnvCheckProject checkProject) throws JsonProcessingException {
        String projectName = checkProject.getProjectName();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonArray = mapper.readTree(checkProject.getResult());
        String fieldToCheck = "name";
        if (jsonUtil.checkForDuplicateFields(jsonArray, fieldToCheck)) {
            return error("新增检查项目'" + checkProject.getProjectName() + "'失败，本次添加中存在重复的检查结果");
        }
        Integer count = projectMapper.selectCount(new QueryWrapper<JsEnvCheckProject>().eq("project_name", projectName));
        if (count != 0) {
            return error("检验项目已存在！");
        }
        JsEnvCheckProject jsEnvCheckProject = projectMapper.selectOne(new QueryWrapper<JsEnvCheckProject>().orderByDesc("id").last("limit 1"));
        Integer num = 1;
        if (ObjectUtil.isNotEmpty(jsEnvCheckProject)){
            num =jsEnvCheckProject.getCheckMethod()+1;
        }
        jsEnvCheckProject.setCheckMethod(num);
        jsEnvCheckProject.setCheckContent("projcet"+num);
        projectMapper.insert(checkProject);
        return success("添加成功");
    }

    @Override
    public AjaxResult edit(JsEnvCheckProject checkProject) throws JsonProcessingException {
        String projectName = checkProject.getProjectName();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonArray = mapper.readTree(checkProject.getResult());
        String fieldToCheck = "name";
        if (jsonUtil.checkForDuplicateFields(jsonArray, fieldToCheck)) {
            return error("修改检查项目'" + checkProject.getProjectName() + "'失败，本次添加中存在重复的检查结果");
        }
        Integer count = projectMapper.selectCount(new QueryWrapper<JsEnvCheckProject>().eq("project_name", projectName).ne("id", checkProject.getId()));
        if (count != 0) {
            return error("检验项目已存在！");
        }
        projectMapper.updateById(checkProject);
        return success("修改成功");
    }

    @Override
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            projectMapper.deleteById(s);
        }
        return success("删除成功");
    }

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String projectName) {
        QueryWrapper<JsEnvCheckProject> queryWrapper = new QueryWrapper<>();
        Page<JsEnvCheckProject> page = new Page<>(pageNum, pageSize);
        queryWrapper.like(StringUtils.isNotEmpty(projectName), "project_name", projectName);
        return success(projectMapper.selectPage(page, queryWrapper));
    }

    @Override
    public AjaxResult listAll() {
        return success(projectMapper.selectList(null));
    }
}
