package com.zt.plat.module.qms.business.other.service;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.other.controller.vo.*;
import com.zt.plat.module.qms.business.other.dal.dataobject.OtherSystemUserDO;
import com.zt.plat.module.qms.business.other.dal.mapper.OtherSystemUserMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 第三方系统用户信息对应 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class OtherSystemUserServiceImpl implements OtherSystemUserService {

    @Resource
    private OtherSystemUserMapper otherSystemUserMapper;

    @Override
    public OtherSystemUserRespVO createOtherSystemUser(OtherSystemUserSaveReqVO createReqVO) {
        // 插入
        OtherSystemUserDO otherSystemUser = BeanUtils.toBean(createReqVO, OtherSystemUserDO.class);
        otherSystemUserMapper.insert(otherSystemUser);
        // 返回
        return BeanUtils.toBean(otherSystemUser, OtherSystemUserRespVO.class);
    }

    @Override
    public void updateOtherSystemUser(OtherSystemUserSaveReqVO updateReqVO) {
        // 校验存在
        validateOtherSystemUserExists(updateReqVO.getId());
        // 更新
        OtherSystemUserDO updateObj = BeanUtils.toBean(updateReqVO, OtherSystemUserDO.class);
        otherSystemUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteOtherSystemUser(Long id) {
        // 校验存在
        validateOtherSystemUserExists(id);
        // 删除
        otherSystemUserMapper.deleteById(id);
    }

    @Override
        public void deleteOtherSystemUserListByIds(List<Long> ids) {
        // 校验存在
        validateOtherSystemUserExists(ids);
        // 删除
        otherSystemUserMapper.deleteByIds(ids);
        }

    private void validateOtherSystemUserExists(List<Long> ids) {
        List<OtherSystemUserDO> list = otherSystemUserMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OTHER_SYSTEM_USER_NOT_EXISTS);
        }
    }

    private void validateOtherSystemUserExists(Long id) {
        if (otherSystemUserMapper.selectById(id) == null) {
            throw exception(OTHER_SYSTEM_USER_NOT_EXISTS);
        }
    }

    @Override
    public OtherSystemUserDO getOtherSystemUser(Long id) {
        return otherSystemUserMapper.selectById(id);
    }

    @Override
    public PageResult<OtherSystemUserDO> getOtherSystemUserPage(OtherSystemUserPageReqVO pageReqVO) {
        return otherSystemUserMapper.selectPage(pageReqVO);
    }

}