package com.cy.guoyan.admin.module.guoyan.service.expertinfo;

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

import java.util.*;
import com.cy.guoyan.admin.module.guoyan.controller.admin.expertinfo.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.expertinfo.ExpertInfoDO;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.pojo.PageParam;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;

import com.cy.guoyan.admin.module.guoyan.dal.mysql.expertinfo.ExpertInfoMapper;

import static com.cy.guoyan.admin.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cy.guoyan.admin.framework.common.util.collection.CollectionUtils.convertList;
import static com.cy.guoyan.admin.framework.common.util.collection.CollectionUtils.diffList;
import static com.cy.guoyan.admin.module.system.enums.ErrorCodeConstants.*;

/**
 * 专家信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ExpertInfoServiceImpl implements ExpertInfoService {

    @Resource
    private ExpertInfoMapper expertInfoMapper;

    @Override
    public Long createExpertInfo(ExpertInfoSaveReqVO createReqVO) {
        // 插入
        ExpertInfoDO expertInfo = BeanUtils.toBean(createReqVO, ExpertInfoDO.class);
        expertInfoMapper.insert(expertInfo);
        // 返回
        return expertInfo.getId();
    }

    @Override
    public void updateExpertInfo(ExpertInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateExpertInfoExists(updateReqVO.getId());
        // 更新
        ExpertInfoDO updateObj = BeanUtils.toBean(updateReqVO, ExpertInfoDO.class);
        expertInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteExpertInfo(Long id) {
        // 校验存在
        validateExpertInfoExists(id);
        // 删除
        expertInfoMapper.deleteById(id);
    }

    @Override
        public void deleteExpertInfoListByIds(List<Long> ids) {
        // 校验存在
        validateExpertInfoExists(ids);
        // 删除
        expertInfoMapper.deleteByIds(ids);
        }

    private void validateExpertInfoExists(List<Long> ids) {
        List<ExpertInfoDO> list = expertInfoMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(EXPERT_INFO_NOT_EXISTS);
        }
    }

    private void validateExpertInfoExists(Long id) {
        if (expertInfoMapper.selectById(id) == null) {
            throw exception(EXPERT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ExpertInfoDO getExpertInfo(Long id) {
        return expertInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ExpertInfoDO> getExpertInfoPage(ExpertInfoPageReqVO pageReqVO) {
        return expertInfoMapper.selectPage(pageReqVO);
    }

}