package cn.iocoder.yudao.module.tkdata.service.customserviceinfo;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.tkdata.controller.admin.customserviceinfo.vo.CustomServiceInfoPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.customserviceinfo.vo.CustomServiceInfoSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.customserviceinfo.CustomServiceInfoDO;
import cn.iocoder.yudao.module.tkdata.dal.mysql.customserviceinfo.CustomServiceInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;import static cn.iocoder.yudao.module.tkdata.enums.ErrorCodeConstants.CUSTOM_SERVICE_INFO_NOT_EXISTS;

/**
 * 客服信息 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class CustomServiceInfoServiceImpl implements CustomServiceInfoService {

    @Resource
    private CustomServiceInfoMapper customServiceInfoMapper;

    @Override
    public Long createCustomServiceInfo(CustomServiceInfoSaveReqVO createReqVO) {
        // 插入
        CustomServiceInfoDO customServiceInfo = BeanUtils.toBean(createReqVO, CustomServiceInfoDO.class);
        customServiceInfoMapper.insert(customServiceInfo);
        // 返回
        return customServiceInfo.getId();
    }

    @Override
    public void updateCustomServiceInfo(CustomServiceInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomServiceInfoExists(updateReqVO.getId());
        // 更新
        CustomServiceInfoDO updateObj = BeanUtils.toBean(updateReqVO, CustomServiceInfoDO.class);
        customServiceInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomServiceInfo(Long id) {
        // 校验存在
        validateCustomServiceInfoExists(id);
        // 删除
        customServiceInfoMapper.deleteById(id);
    }

    @Override
        public void deleteCustomServiceInfoListByIds(List<Long> ids) {
        // 校验存在
        validateCustomServiceInfoExists(ids);
        // 删除
        customServiceInfoMapper.deleteByIds(ids);
        }

    private void validateCustomServiceInfoExists(List<Long> ids) {
        List<CustomServiceInfoDO> list = customServiceInfoMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CUSTOM_SERVICE_INFO_NOT_EXISTS);
        }
    }

    private void validateCustomServiceInfoExists(Long id) {
        if (customServiceInfoMapper.selectById(id) == null) {
            throw exception(CUSTOM_SERVICE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public CustomServiceInfoDO getCustomServiceInfo(Long id) {
        return customServiceInfoMapper.selectById(id);
    }

    @Override
    public PageResult<CustomServiceInfoDO> getCustomServiceInfoPage(CustomServiceInfoPageReqVO pageReqVO) {
        return customServiceInfoMapper.selectPage(pageReqVO);
    }

}