package com.fjhx.victoriatourist.service.abnormal.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.socket.service.WebSocketServer;
import com.fjhx.victoriatourist.entity.abnormal.dto.AbnormalDetailsDto;
import com.fjhx.victoriatourist.entity.abnormal.dto.AbnormalDetailsSelectDto;
import com.fjhx.victoriatourist.entity.abnormal.po.AbnormalDetails;
import com.fjhx.victoriatourist.entity.abnormal.po.AbnormalInfo;
import com.fjhx.victoriatourist.entity.abnormal.vo.AbnormalDetailsVo;
import com.fjhx.victoriatourist.mapper.abnormal.AbnormalDetailsMapper;
import com.fjhx.victoriatourist.service.abnormal.AbnormalDetailsService;
import com.fjhx.victoriatourist.service.abnormal.AbnormalInfoService;
import com.fjhx.victoriatourist.utils.CodeEnum;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 异常处理详情 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-11
 */
@DS(SourceConstant.VICTORIATOURIST)
@Service
public class AbnormalDetailsServiceImpl extends ServiceImpl<AbnormalDetailsMapper, AbnormalDetails> implements AbnormalDetailsService {
    @Autowired
    AbnormalInfoService abnormalInfoService;

    @Override
    public Page<AbnormalDetailsVo> getPage(AbnormalDetailsSelectDto dto) {
        IWrapper<AbnormalDetails> wrapper = getWrapper();
        wrapper.eq("ad", AbnormalDetails::getAbnormalInfoId, dto.getAbnormalInfoId());
        wrapper.orderByDesc("ad", AbnormalDetails::getId);
        Page<AbnormalDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        //赋值跟进用户名
        UserUtil.assignmentNickName(page.getRecords(), AbnormalDetails::getHandleUser, AbnormalDetailsVo::setHandleUserName);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(AbnormalDetailsDto abnormalDetailsDto) {
        Long abnormalInfoId = abnormalDetailsDto.getAbnormalInfoId();
        if (ObjectUtil.isEmpty(abnormalInfoId)) {
            throw new ServiceException("异常记录id不能为空");
        }
        Assert.notEmpty(abnormalDetailsDto.getExplain(), "异常说明不能为空");
        Integer status = abnormalDetailsDto.getStatus();
        if (ObjectUtil.isEmpty(status)) {
            throw new ServiceException("处理状态不能为空");
        }
//        AbnormalInfo abnormalInfo = new AbnormalInfo();
//        abnormalInfo.setId(abnormalInfoId);
        AbnormalInfo abnormalInfo = abnormalInfoService.getById(abnormalInfoId);
        if(ObjectUtil.isEmpty(abnormalInfo)){
            throw new ServiceException("无法查询到异常记录信息");
        }
//        abnormalInfo.setLinkCode(CodeEnum.ABNORMAL.getCode());
        abnormalInfo.setStatus(status == 1 ? 2 : 1);//如果跟进记录为已处理修改异常状态为已处理要不然跟进中
        abnormalInfo.setHandleUser(abnormalDetailsDto.getHandleUser());
        abnormalInfo.setHandleTime(abnormalDetailsDto.getHandleTime());
        abnormalInfoService.updateById(abnormalInfo);

        //如果下一跟进人不为空 就给下一跟进人发送消息
        if (ObjectUtil.isNotEmpty(abnormalDetailsDto.getNextHandleUser())) {
            //给下一个跟进人发消息通知
            JSONObject meassage = new JSONObject();
            meassage.put("msg", "有异常需要您跟进");
            meassage.put("abnormalInfoId", abnormalDetailsDto.getAbnormalInfoId().toString());
            WebSocketServer.sendInfo(abnormalDetailsDto.getNextHandleUser(), 1, meassage);
        }

        this.save(abnormalDetailsDto);
    }

}
