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.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.model.JsDisinfectManage;
import com.ruoyi.web.v2.v1.mapper.JsDisinfectManageMapper;
import com.ruoyi.web.v2.v1.service.IJsDisinfectManageService;
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 JsDisinfectManageServiceImpl extends ServiceImpl<JsDisinfectManageMapper, JsDisinfectManage> implements IJsDisinfectManageService {

    @Autowired
    private JsDisinfectManageMapper disinfectManageMapper;

    @Override
    public AjaxResult add(JsDisinfectManage jsDisinfectManage) {
        String disinfectMethod = jsDisinfectManage.getDisinfectMethod();
        Integer count = disinfectManageMapper.selectCount(new QueryWrapper<JsDisinfectManage>().eq("disinfect_method", disinfectMethod));
        if (count != 0) {
            return error("消毒方式已存在！");
        } else {
            //是否设置为默认吊钩
            if (jsDisinfectManage.getDefaultFlag()){
                //将其他吊钩设置为非默认
                disinfectManageMapper.updateHookDefault();
            }
            disinfectManageMapper.insert(jsDisinfectManage);
        }
        return success("添加成功");
    }

    @Override
    public AjaxResult edit(JsDisinfectManage jsDisinfectManage) {
        String disinfectMethod = jsDisinfectManage.getDisinfectMethod();
        Integer count = disinfectManageMapper.selectCount(new QueryWrapper<JsDisinfectManage>().eq("disinfect_method", disinfectMethod).ne("id",jsDisinfectManage.getId()));
        if (count != 0) {
            return error("消毒方式已存在！");
        } else {
            //是否设置为默认吊钩
            if (jsDisinfectManage.getDefaultFlag()){
                //将其他吊钩设置为非默认
                disinfectManageMapper.updateHookDefault();
            }
            disinfectManageMapper.updateById(jsDisinfectManage);
        }
        return success("修改成功");
    }

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

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize,String disinfectMethod) {
        QueryWrapper<JsDisinfectManage> queryWrapper = new QueryWrapper<>();
        Page<JsDisinfectManage> page = new Page<>(pageNum, pageSize);
        queryWrapper.like(StringUtils.isNotEmpty(disinfectMethod), "disinfect_method", disinfectMethod);
        return success(disinfectManageMapper.selectPage(page, queryWrapper));
    }

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