package cn.iocoder.yudao.module.jx.service.impl;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.utils.DateUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolPhone;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolInfoMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolPhoneMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolPhoneService;
import cn.iocoder.yudao.module.jx.vo.DriveSchoolInfoVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 客服Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-10
 */
@Service
public class DriveSchoolPhoneServiceImpl implements IDriveSchoolPhoneService
{
    @Autowired
    private DriveSchoolPhoneMapper driveSchoolPhoneMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询客服
     *
     * @param id 客服主键
     * @return 客服
     */
    @Override
    public DriveSchoolPhone selectDriveSchoolPhoneById(Long id)
    {
        return driveSchoolPhoneMapper.selectDriveSchoolPhoneById(id);
    }

    /**
     * 查询客服列表
     *
     * @param driveSchoolPhone 客服
     * @return 客服
     */
    @Override
    public IPage<DriveSchoolPhone> selectDriveSchoolPhoneList(DriveSchoolPhone driveSchoolPhone,Page<DriveSchoolPhone> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolPhone.setDeptId(deptId);
        if (deptId==100){
            driveSchoolPhone.setDeptId(null);
        }
        return driveSchoolPhoneMapper.selectDriveSchoolPhoneList(driveSchoolPhone,page);
    }

    /**
     * 新增客服
     *
     * @param driveSchoolPhone 客服
     * @return 结果
     */
    @Override
    public int insertDriveSchoolPhone(DriveSchoolPhone driveSchoolPhone)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolPhone.setDeptId(deptId);
        return driveSchoolPhoneMapper.insertDriveSchoolPhone(driveSchoolPhone);
    }

    /**
     * 修改客服
     *
     * @param driveSchoolPhone 客服
     * @return 结果
     */
    @Override
    public int updateDriveSchoolPhone(DriveSchoolPhone driveSchoolPhone)
    {
        //driveSchoolPhone.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolPhoneMapper.updateDriveSchoolPhone(driveSchoolPhone);
    }

    /**
     * 批量删除客服
     *
     * @param ids 需要删除的客服主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolPhoneByIds(Long[] ids)
    {
        return driveSchoolPhoneMapper.deleteDriveSchoolPhoneByIds(ids);
    }

    @Resource
    private DriveSchoolInfoMapper driveSchoolInfoMapper;

    /**
     * 删除客服信息
     *
     * @param id 客服主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolPhoneById(Long id)
    {
        return driveSchoolPhoneMapper.deleteDriveSchoolPhoneById(id);
    }

    @Override
    public DriveSchoolPhone getPhone(DriveSchoolPhone driveSchoolPhone) {
        if (driveSchoolPhone.getDeptId()!=100){
            DriveSchoolInfoVO schoolInfoByDeptId = driveSchoolInfoMapper.findInfoByJxId(driveSchoolPhone.getDeptId());
            driveSchoolPhone.setDeptId(schoolInfoByDeptId.getDeptId());
        }
        return driveSchoolPhoneMapper.getPhone(driveSchoolPhone);
    }
}
