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.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.model.JsCheckInstrument;
import com.ruoyi.web.v2.v1.mapper.JsCheckInstrumentMapper;
import com.ruoyi.web.v2.v1.service.IJsCheckInstrumentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.v2.v1.utils.PhoneNumberValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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
 */
@Service
public class JsCheckInstrumentServiceImpl extends ServiceImpl<JsCheckInstrumentMapper, JsCheckInstrument> implements IJsCheckInstrumentService {

    @Autowired
    private JsCheckInstrumentMapper instrumentMapper;

    @Override
    public AjaxResult add(JsCheckInstrument checkDevice) {
        String deviceName = checkDevice.getDeviceName();
        if (ObjectUtil.isNotEmpty(checkDevice.getPhone())) {
            if (!PhoneNumberValidator.isValidContact(checkDevice.getPhone())) {

                return error("联系方式格式不正确！");
            }
        }
        Integer count = instrumentMapper.selectCount(new QueryWrapper<JsCheckInstrument>().eq("device_name", deviceName)
                .or().eq("device_num", checkDevice.getDeviceNum()));
        if (count != 0) {
            return error("检验仪器已存在！");
        } else {
            instrumentMapper.insert(checkDevice);
        }
        return success("添加成功");
    }

    @Override
    public AjaxResult edit(JsCheckInstrument checkDevice) {
        String deviceName = checkDevice.getDeviceName();
        if (ObjectUtil.isNotEmpty(checkDevice.getPhone())) {
            if (!PhoneNumberValidator.isValidContact(checkDevice.getPhone())) {
                return error("联系方式格式不正确！");
            }
        }
        Integer count = instrumentMapper.selectCount(new QueryWrapper<JsCheckInstrument>().ne("id", checkDevice.getId()).eq("device_name", deviceName));
        if (count != 0) {
            return error("检验仪器名称已存在！");
        }
        Integer count1 = instrumentMapper.selectCount(new QueryWrapper<JsCheckInstrument>().ne("id", checkDevice.getId()).eq("device_num", checkDevice.getDeviceNum()));
        if (count1 != 0) {
            return error("检验仪器编号已存在！");
        }
        instrumentMapper.updateById(checkDevice);

        return success("修改成功");
    }

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

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String deviceNum, String deviceName) {
        QueryWrapper<JsCheckInstrument> queryWrapper = new QueryWrapper<>();
        Page<JsCheckInstrument> page = new Page<>(pageNum, pageSize);
        queryWrapper.eq(StringUtils.isNotEmpty(deviceNum), "device_num", deviceNum).or()
                .like(StringUtils.isNotEmpty(deviceName), "device_name", deviceName);
        return success(instrumentMapper.selectPage(page, queryWrapper));
    }

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