package com.xinelu.manage.service.scriptInfo.impl;

import com.xinelu.common.config.SystemBusinessConfig;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.file.FileUploadUtils;
import com.xinelu.common.utils.file.MimeTypeUtils;
import com.xinelu.common.utils.uuid.IdUtils;
import com.xinelu.manage.domain.scriptInfo.ScriptInfo;
import com.xinelu.manage.mapper.scriptInfo.ScriptInfoMapper;
import com.xinelu.manage.service.scriptInfo.IScriptInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 话术信息Service业务层处理
 *
 * @author xinelu
 * @date 2024-02-26
 */
@Service
public class ScriptInfoServiceImpl implements IScriptInfoService {
    @Resource
    private ScriptInfoMapper scriptInfoMapper;

    @Resource
    private SystemBusinessConfig systemBusinessConfig;


    /**
     * 查询话术信息
     *
     * @param id 话术信息主键
     * @return 话术信息
     */
    @Override
    public ScriptInfo selectScriptInfoById(Long id) {
        return scriptInfoMapper.selectScriptInfoById(id);
    }

    /**
     * 查询话术信息列表
     *
     * @param scriptInfo 话术信息
     * @return 话术信息
     */
    @Override
    public List<ScriptInfo> selectScriptInfoList(ScriptInfo scriptInfo) {
        return scriptInfoMapper.selectScriptInfoList(scriptInfo);
    }

    /**
     * 新增话术信息
     *
     * @param scriptInfo 话术信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertScriptInfo(ScriptInfo scriptInfo) {
        // 判断通用话术名称是否存在
        if (scriptInfoMapper.countByScriptInfo(scriptInfo) > 0) {
            throw new ServiceException("通用话术名称已存在");
        }
        // 设置创建人与创建时间
        scriptInfo.setCreateBy(SecurityUtils.getUsername());
        scriptInfo.setCreateTime(LocalDateTime.now());
        scriptInfo.setScriptId(IdUtils.fastUUID());
        return scriptInfoMapper.insertScriptInfo(scriptInfo);
    }

    /**
     * 修改话术信息
     *
     * @param scriptInfo 话术信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateScriptInfo(ScriptInfo scriptInfo) {
        // 检查除当前记录之外是否存在同名的话术名称
        if (scriptInfoMapper.countByScriptNameExcludingId(scriptInfo.getScriptName(), scriptInfo.getDepartmentId(), scriptInfo.getId(), scriptInfo.getCommonScriptName()) > 0) {
            // 存在同名的通用话术名称，不能进行更新
            throw new ServiceException("通用话术名称已存在，请使用其他名称。");
        }
        // 不存在同名的话术名称或者名称未改变，设置修改人和修改时间
        scriptInfo.setUpdateBy(SecurityUtils.getUsername());
        scriptInfo.setUpdateTime(LocalDateTime.now());
        // 进行更新操作
        return scriptInfoMapper.updateScriptInfo(scriptInfo);
    }

    /**
     * 批量删除话术信息
     *
     * @param ids 需要删除的话术信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteScriptInfoByIds(Long[] ids) {
        return scriptInfoMapper.deleteScriptInfoByIds(ids);
    }

    /**
     * 删除话术信息信息
     *
     * @param id 话术信息主键
     * @return 结果
     */
    @Override
    public int deleteScriptInfoById(Long id) {
        return scriptInfoMapper.deleteScriptInfoById(id);
    }

    /**
     * 话术图片上传
     */
    @Override
    public AjaxResult uploadScriptInfo(MultipartFile multipartFile) throws Exception {
        // 获取文件路径
        String uploadPathUrl = SystemBusinessConfig.getProfile() + systemBusinessConfig.getScriptFileUrl();
        if (StringUtils.isBlank(uploadPathUrl)) {
            return AjaxResult.success();
        }
        // 上传
        String scriptFilePath = FileUploadUtils.upload(uploadPathUrl, multipartFile, MimeTypeUtils.IMAGE_EXTENSION);
        if (StringUtils.isBlank(scriptFilePath)) {
            throw new ServiceException("图片上传失败，请联系管理员！");
        }
        // 上传成功，返回话术图片url
        AjaxResult ajax = AjaxResult.success("上传成功！");
        ajax.put("imgUrl", scriptFilePath);
        return ajax;
    }
}
