package com.ruoyi.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.domain.dto.environmentWarningThresholdRole.EnvironmentWarningThresholdRoleAddRequest;
import com.ruoyi.web.domain.dto.environmentWarningThresholdRole.EnvironmentWarningThresholdRoleEditRequest;
import com.ruoyi.web.domain.dto.environmentWarningThresholdRole.EnvironmentWarningThresholdRoleQueryRequest;
import com.ruoyi.web.domain.entity.EnvironmentWarningThresholdRole;
import com.ruoyi.web.domain.entity.EnvironmentWarningThresholdUser;
import com.ruoyi.web.mapper.EnvironmentWarningThresholdRoleMapper;
import com.ruoyi.web.service.EnvironmentWarningThresholdRoleService;
import com.ruoyi.web.service.EnvironmentWarningThresholdUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class EnvironmentWarningThresholdRoleServiceImpl extends ServiceImpl<EnvironmentWarningThresholdRoleMapper, EnvironmentWarningThresholdRole>
        implements EnvironmentWarningThresholdRoleService {

    @Resource
    private EnvironmentWarningThresholdUserService environmentWarningThresholdUserService;


    @Override
    public Integer addEnvironmentWarningThresholdRole(EnvironmentWarningThresholdRoleAddRequest environmentWarningThresholdRoleAddRequest) {
        if (environmentWarningThresholdRoleAddRequest == null) {
            throw new ServiceException("请求参数为空");
        }
        EnvironmentWarningThresholdRole environmentWarningThresholdRole = new EnvironmentWarningThresholdRole();
        environmentWarningThresholdRole.setType(environmentWarningThresholdRole.getType());
        this.save(environmentWarningThresholdRole);
        return environmentWarningThresholdRole.getId();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEnvironmentWarningThresholdRole(String ids) {
        if (StrUtil.isBlank(ids)) {
            throw new ServiceException("id不能为空或id异常");
        }

        // 2. 分割ID字符串为List<Integer>
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim)
                .filter(StrUtil::isNotBlank)
                .map(Long::parseLong)
                .collect(Collectors.toList());

        if (idList.isEmpty()) {
            throw new ServiceException("ID格式异常");
        }

        // 3. 构建删除条件
        QueryWrapper<EnvironmentWarningThresholdRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", idList);

        // 4. 执行删除（返回是否删除成功）
        boolean remove = remove(queryWrapper);
        if (remove) {
            QueryWrapper<EnvironmentWarningThresholdUser> wrapper = new QueryWrapper<>();
            wrapper.in("role_id", idList);
            return environmentWarningThresholdUserService.remove(wrapper);
        } else {
            return false;
        }
    }

    @Override
    public void editEnvironmentWarningThresholdRole(EnvironmentWarningThresholdRoleEditRequest environmentWarningThresholdRoleEditRequest) {
        // 判断是否存在
        Integer id = environmentWarningThresholdRoleEditRequest.getId();
        EnvironmentWarningThresholdRole oldEnvironmentWarningThresholdRole = this.getById(id);
        if (oldEnvironmentWarningThresholdRole == null) {
            throw new ServiceException("未找到人居环境负责人类型数据");
        }
        EnvironmentWarningThresholdRole environmentWarningThresholdRole = new EnvironmentWarningThresholdRole();
        BeanUtil.copyProperties(environmentWarningThresholdRoleEditRequest, environmentWarningThresholdRole);
        String type = environmentWarningThresholdRole.getType();
        if (StrUtil.isNotBlank(type)) {
            throw new ServiceException("人居环境负责人类型不能为空");
        }
        environmentWarningThresholdRole.setUpdateTime(new Date());
        // 操作数据库
        boolean result = this.updateById(environmentWarningThresholdRole);
        if (!result) {
            throw new ServiceException("修改人居环境负责人类型操作失败");
        }
    }


    @Override
    public EnvironmentWarningThresholdRole getEnvironmentWarningThresholdRoleById(int id) {
        if (id <= 0 || ObjectUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空或id异常");
        }
        EnvironmentWarningThresholdRole environmentWarningThresholdRole = this.getById(id);
        if (environmentWarningThresholdRole == null) {
            throw new ServiceException("未找到人居环境负责人类型数据");
        }
        return environmentWarningThresholdRole;
    }


    @Override
    public List<EnvironmentWarningThresholdRole> getListEnvironmentWarningThresholdRole(EnvironmentWarningThresholdRoleQueryRequest environmentWarningThresholdRoleQueryRequest) {
        if (ObjectUtil.isEmpty(environmentWarningThresholdRoleQueryRequest)) {
            throw new ServiceException("请求参数为空");
        }
        Integer id = environmentWarningThresholdRoleQueryRequest.getId();
        String type = environmentWarningThresholdRoleQueryRequest.getType();
        QueryWrapper<EnvironmentWarningThresholdRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(type), "type", type)
                .eq(ObjectUtil.isNotEmpty(id), "id", id);
        return this.list(queryWrapper);
    }
}




