package com.zt.plat.module.base.service.base;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.controller.admin.base.vo.ContactPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.ContactRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.ContactSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.ContactDO;
import com.zt.plat.module.base.dal.mysql.base.ContactMapper;
import jakarta.annotation.Resource;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.CONTACT_NOT_EXISTS;

/**
 * 联系人信息 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ContactServiceImpl implements ContactService {

    @Resource
    private ContactMapper contactMapper;

    @Override
    public ContactRespVO createContact(ContactSaveReqVO createReqVO) {
        // 插入
        ContactDO contact = BeanUtils.toBean(createReqVO, ContactDO.class);
        contactMapper.insert(contact);
        // 返回
        return BeanUtils.toBean(contact, ContactRespVO.class);
    }

    @Override
    public void updateContact(ContactSaveReqVO updateReqVO) {
        // 校验存在
        validateContactExists(updateReqVO.getId());
        // 更新
        ContactDO updateObj = BeanUtils.toBean(updateReqVO, ContactDO.class);
        contactMapper.updateById(updateObj);
    }

    @Override
    public void deleteContact(Long id) {
        // 校验存在
        validateContactExists(id);
        // 删除
        contactMapper.deleteById(id);
    }

    @Override
        public void deleteContactListByIds(List<Long> ids) {
        // 校验存在
        validateContactExists(ids);
        // 删除
        contactMapper.deleteByIds(ids);
        }

    private void validateContactExists(List<Long> ids) {
        List<ContactDO> list = contactMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONTACT_NOT_EXISTS);
        }
    }

    private void validateContactExists(Long id) {
        if (contactMapper.selectById(id) == null) {
            throw exception(CONTACT_NOT_EXISTS);
        }
    }

    @Override
    public ContactDO getContact(Long id) {
        return contactMapper.selectById(id);
    }

    @Override
    public PageResult<ContactDO> getContactPage(ContactPageReqVO pageReqVO) {
        return contactMapper.selectPage(pageReqVO);
    }

    @Override
    public void enableContactList(List<ContactSaveReqVO> saveReqVOS) {
        List<ContactDO> updateObj = BeanUtils.toBean(saveReqVOS, ContactDO.class);
        List<BatchResult> count =  contactMapper.updateById(updateObj);
        if (CollUtil.isEmpty(count)) {
            throw exception(CONTACT_NOT_EXISTS);
        }
    }

}