package cn.iocoder.yudao.module.cus.service.management;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.cus.controller.admin.management.vo.*;
import cn.iocoder.yudao.module.cus.dal.dataobject.management.ManagementDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.cus.dal.mysql.management.ManagementMapper;

import static cn.iocoder.yudao.module.cus.enums.ErrorCodeConstants.MANAGEMENT_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 客户管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ManagementServiceImpl implements ManagementService {

    @Resource
    private ManagementMapper managementMapper;

    @Override
    public Long createManagement(ManagementSaveReqVO createReqVO) {
        // 插入
        ManagementDO management = BeanUtils.toBean(createReqVO, ManagementDO.class);
        managementMapper.insert(management);

        // 返回
        return management.getId();
    }

    @Override
    public void updateManagement(ManagementSaveReqVO updateReqVO) {
        // 校验存在
        validateManagementExists(updateReqVO.getId());
        // 更新
        ManagementDO updateObj = BeanUtils.toBean(updateReqVO, ManagementDO.class);
        managementMapper.updateById(updateObj);
    }

    @Override
    public void deleteManagement(Long id) {
        // 校验存在
        validateManagementExists(id);
        // 删除
        managementMapper.deleteById(id);
    }

    @Override
        public void deleteManagementListByIds(List<Long> ids) {
        // 删除
        managementMapper.deleteByIds(ids);
        }


    private void validateManagementExists(Long id) {
        if (managementMapper.selectById(id) == null) {
            throw exception(MANAGEMENT_NOT_EXISTS);
        }
    }

    @Override
    public ManagementDO getManagement(Long id) {
        return managementMapper.selectById(id);
    }

    @Override
    public PageResult<ManagementDO> getManagementPage(ManagementPageReqVO pageReqVO) {
        return managementMapper.selectPage(pageReqVO);
    }

}