package com.zcloud.basic.info.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.basic.info.domain.gateway.DepartmentSuperviseGateway;
import com.zcloud.basic.info.domain.model.DepartmentSuperviseE;
import com.zcloud.basic.info.persistence.dataobject.DepartmentSuperviseDO;
import com.zcloud.basic.info.persistence.repository.DepartmentSuperviseRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-11-10 14:13:46
 */
@Service
@AllArgsConstructor
public class DepartmentSuperviseGatewayImpl implements DepartmentSuperviseGateway {
    private final DepartmentSuperviseRepository departmentSuperviseRepository;

    @Override
    public Boolean add(DepartmentSuperviseE departmentSuperviseE) {
        DepartmentSuperviseDO d = new DepartmentSuperviseDO();
        BeanUtils.copyProperties(departmentSuperviseE, d);
        departmentSuperviseRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(DepartmentSuperviseE departmentSuperviseE) {
        DepartmentSuperviseDO d = new DepartmentSuperviseDO();
        BeanUtils.copyProperties(departmentSuperviseE, d);
        departmentSuperviseRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedDepartmentSuperviseById(Long id) {
        return departmentSuperviseRepository.removeById(id);
    }

    @Override
    public Boolean deletedDepartmentSuperviseByIds(Long[] ids) {
        return departmentSuperviseRepository.removeByIds(Collections.singletonList(ids));
    }

    @Override
    public void addbatch(List<DepartmentSuperviseE> departmentSuperviseEList) {
        List<DepartmentSuperviseDO> departmentSuperviseDOs = departmentSuperviseEList.stream()
                .map(this::convertToDO)
                .collect(Collectors.toList());
        departmentSuperviseRepository.saveBatch(departmentSuperviseDOs);
    }
    private DepartmentSuperviseDO convertToDO(DepartmentSuperviseE entity) {
        DepartmentSuperviseDO departmentSuperviseDO = new DepartmentSuperviseDO();
        BeanUtils.copyProperties(entity, departmentSuperviseDO);
        return departmentSuperviseDO;
    }

    @Override
    public void deletedDepartmentBySuperviseId(Long id) {
        QueryWrapper<DepartmentSuperviseDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supervise_department_id", id);
        departmentSuperviseRepository.remove(queryWrapper);
    }

    @Override
    public void deletedDepartmentBySuperviseIdList(List<Long> idList) {
        QueryWrapper<DepartmentSuperviseDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("supervise_department_id", idList);
        departmentSuperviseRepository.remove(queryWrapper);
    }
}

