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

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.JsCheckProject;
import com.ruoyi.web.v2.v1.mapper.JsCheckProjectMapper;
import com.ruoyi.web.v2.v1.service.IJsCheckProjectService;
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-05-20
 */
@Service
public class JsCheckProjectServiceImpl extends ServiceImpl<JsCheckProjectMapper, JsCheckProject> implements IJsCheckProjectService {

    @Autowired
    private JsCheckProjectMapper projectMapper;

    @Override
    public AjaxResult add(JsCheckProject 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<JsCheckProject>().eq("project_name", projectName));
        if (count != 0) {
            return error("检验项目已存在！");
        } else {
            //是否设置为默认吊钩
            if (checkProject.getDefaultFlag()){
                //将其他吊钩设置为非默认
                projectMapper.updateHookDefault();
            }
            projectMapper.insert(checkProject);
        }
        return success("添加成功");
    }

    @Override
    public AjaxResult edit(JsCheckProject 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<JsCheckProject>().eq("project_name", projectName).ne("id", checkProject.getId()));
        if (count != 0) {
            return error("检验项目已存在！");
        } else {
            //是否设置为默认吊钩
            if (checkProject.getDefaultFlag()){
                //将其他吊钩设置为非默认
                projectMapper.updateHookDefault();
            }
            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<JsCheckProject> queryWrapper = new QueryWrapper<>();
        Page<JsCheckProject> 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));
    }
}
