package com.zcloud.basic.info.command;

import cn.hutool.core.bean.BeanUtil;
import com.zcloud.basic.info.domain.gateway.PostGateway;
import com.alibaba.cola.exception.BizException;
import com.zcloud.basic.info.domain.model.UserE;
import com.zcloud.basic.info.persistence.dataobject.UserDO;
import com.zcloud.basic.info.persistence.repository.UserRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * web-app
 *
 * @Author SondonYong
 * @Date 2025-10-31 17:24:04
 */
@Component
@AllArgsConstructor
public class PostRemoveExe {
    private final PostGateway postGateway;
    private final UserRepository userRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        // 校验是否有人员使用该岗位
        List<UserDO> userDOS = userRepository.listByPostId(id);
        UserE userE = new UserE();
        userE.checkNull(BeanUtil.copyToList(userDOS, UserE.class));

        boolean res = postGateway.deletedPostById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        UserE userE = new UserE();

        // 校验是否有人员使用该岗位
        List<UserDO> userDOS = userRepository.listByPostIds(ids);
        userE.checkNull(BeanUtil.copyToList(userDOS, UserE.class));

        boolean res = postGateway.deletedPostByIds(ids);
        if (!res) {
            throw new BizException("删除失败");
        }

        return true;
    }
}

