package com.dsk.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.dsk.common.core.domain.entity.BusinessContacts;
import com.dsk.common.core.domain.model.LoginUser;
import com.dsk.common.exception.base.BaseException;
import com.dsk.common.utils.CheckUtils;
import com.dsk.common.utils.DateUtils;
import com.dsk.common.utils.SecurityUtils;
import com.dsk.system.mapper.BusinessContactsMapper;
import com.dsk.system.service.IBusinessContactsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目联系人Service业务层处理
 *
 * @author lxl
 * @date 2023-05-17
 */
@Service
public class BusinessContactsServiceImpl implements IBusinessContactsService
{
    @Autowired
    private BusinessContactsMapper businessContactsMapper;

    /**
     * 查询项目联系人
     *
     * @param id 项目联系人主键
     * @return 项目联系人
     */
    @Override
    public BusinessContacts selectBusinessContactsById(Long id)
    {
        return businessContactsMapper.selectBusinessContactsById(id);
    }

    /**
     * 查询项目联系人列表
     *
     * @param businessContacts 项目联系人
     * @return 项目联系人
     */
    @Override
    public List<BusinessContacts> selectBusinessContactsList(BusinessContacts businessContacts)
    {
        return businessContactsMapper.selectBusinessContactsList(businessContacts);
    }

    /**
     * 新增项目联系人
     *
     * @param businessContacts 项目联系人
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusinessContacts(BusinessContacts businessContacts)
    {
        if(!CheckUtils.isPhone(businessContacts.getPhone())) throw new BaseException("500","请输入正确的电话号码");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isEmpty(loginUser)) throw new BaseException("请登录");
        //维护人员为当前登录用户
        businessContacts.setAccendant(loginUser.getUser().getNickName());
        return businessContactsMapper.insertBusinessContacts(businessContacts);
    }

    /**
     * 修改项目联系人
     *
     * @param businessContacts 项目联系人
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusinessContacts(BusinessContacts businessContacts)
    {
        if(!CheckUtils.isPhone(businessContacts.getPhone())) throw new BaseException("500","请输入正确的电话号码");
        businessContacts.setUpdateTime(DateUtils.getNowDate());
        return businessContactsMapper.updateBusinessContacts(businessContacts);
    }

    /**
     * 批量删除项目联系人
     *
     * @param ids 需要删除的项目联系人主键
     * @return 结果
     */
    @Override
    public int deleteBusinessContactsByIds(Long[] ids)
    {
        return businessContactsMapper.deleteBusinessContactsByIds(ids);
    }

    /**
     * 删除项目联系人信息
     *
     * @param id 项目联系人主键
     * @return 结果
     */
    @Override
    public int deleteBusinessContactsById(Long id)
    {
        return businessContactsMapper.deleteBusinessContactsById(id);
    }
}
