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.villageCommitteeRole.VillageCommitteeRoleAddRequest;
import com.ruoyi.web.domain.dto.villageCommitteeRole.VillageCommitteeRoleEditRequest;
import com.ruoyi.web.domain.dto.villageCommitteeRole.VillageCommitteeRoleQueryRequest;
import com.ruoyi.web.domain.entity.VillageCommitteeRole;
import com.ruoyi.web.domain.entity.VillageCommitteeRoleUser;
import com.ruoyi.web.mapper.VillageCommitteeRoleMapper;
import com.ruoyi.web.service.VillageCommitteeRoleService;
import com.ruoyi.web.service.VillageCommitteeRoleUserService;
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 VillageCommitteeRoleServiceImpl extends ServiceImpl<VillageCommitteeRoleMapper, VillageCommitteeRole>
        implements VillageCommitteeRoleService {

    @Resource
    private VillageCommitteeRoleUserService villageCommitteeRoleUserService;


    @Override
    public Integer addVillageCommitteeRole(VillageCommitteeRoleAddRequest villageCommitteeRoleAddRequest) {
        if (villageCommitteeRoleAddRequest == null) {
            throw new ServiceException("请求参数为空");
        }
        VillageCommitteeRole villageCommitteeRole = new VillageCommitteeRole();
        villageCommitteeRole.setType(villageCommitteeRole.getType());
        this.save(villageCommitteeRole);
        return villageCommitteeRole.getId();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteVillageCommitteeRole(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<VillageCommitteeRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", idList);

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

    @Override
    public void editVillageCommitteeRole(VillageCommitteeRoleEditRequest villageCommitteeRoleEditRequest) {
        // 判断是否存在
        Integer id = villageCommitteeRoleEditRequest.getId();
        VillageCommitteeRole oldVillageCommitteeRole = this.getById(id);
        if (oldVillageCommitteeRole == null) {
            throw new ServiceException("未找到村委角色");
        }
        VillageCommitteeRole villageCommitteeRole = new VillageCommitteeRole();
        BeanUtil.copyProperties(villageCommitteeRoleEditRequest, villageCommitteeRole);
        String type = villageCommitteeRole.getType();
        if (StrUtil.isNotBlank(type)) {
            throw new ServiceException("村委角色类型不能为空");
        }
        villageCommitteeRole.setUpdateTime(new Date());
        // 操作数据库
        boolean result = this.updateById(villageCommitteeRole);
        if (!result) {
            throw new ServiceException("修改村委角色操作失败");
        }
    }


    @Override
    public VillageCommitteeRole getVillageCommitteeRoleById(int id) {
        if (id <= 0 || ObjectUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空或id异常");
        }
        VillageCommitteeRole villageCommitteeRole = this.getById(id);
        if (villageCommitteeRole == null) {
            throw new ServiceException("未找到村委角色");
        }
        return villageCommitteeRole;
    }


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





