package com.njcn.cssystem.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.cssystem.mapper.CsFeedbackChatMapper;
import com.njcn.cssystem.pojo.param.CsFeedbackAuditParm;
import com.njcn.cssystem.pojo.param.CsFeedbackChatAddParm;
import com.njcn.cssystem.pojo.param.CsFeedbackChatCheckParm;
import com.njcn.cssystem.pojo.po.CsFeedbackChatPO;
import com.njcn.cssystem.pojo.po.CsFeedbackPO;
import com.njcn.cssystem.service.CsFeedbackChatService;
import com.njcn.cssystem.service.CsFeedbackService;
import com.njcn.user.enums.AppRoleEnum;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/4/6  15:28【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsFeedbackChatServiceImpl extends ServiceImpl<CsFeedbackChatMapper, CsFeedbackChatPO> implements CsFeedbackChatService {


    private final CsFeedbackService csFeedbackService;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean AddFeedbackChat(CsFeedbackChatAddParm csFeedbackAddParm) {
        CsFeedbackChatPO csFeedbackChatPO = new CsFeedbackChatPO ();
        BeanUtils.copyProperties (csFeedbackAddParm, csFeedbackChatPO);
        csFeedbackChatPO.setUserId(RequestUtil.getUserIndex());
        csFeedbackChatPO.setChatStatus ("0");
        csFeedbackChatPO.setStatus ("1");
        boolean save = this.save (csFeedbackChatPO);
        return save;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean updateChatStatus(CsFeedbackChatCheckParm csFeedbackChatCheckParm) {
        UpdateWrapper<CsFeedbackChatPO> updateWrapper = new UpdateWrapper<> ();
        String userIndex = RequestUtil.getUserIndex();
        String userRole = RequestUtil.getUserRole();
        List<String> strings = JSONArray.parseArray(userRole, String.class);
        if(CollectionUtils.isEmpty(strings)){
            throw new BusinessException(AlgorithmResponseEnum.UNKNOW_ROLE);

        }
        userRole=strings.get(0);
        CsFeedbackPO one = csFeedbackService.lambdaQuery().eq(CsFeedbackPO::getId, csFeedbackChatCheckParm.getId()).one();
            /*如何非创建人添加一条聊天记录“反馈已收到，处理中”*/
        if ((!Objects.equals(one.getCreateBy(),userIndex))&&Objects.equals(one.getStatus(),"1")
                &&(Objects.equals(userRole, AppRoleEnum.ENGINEERING_USER.getCode())||Objects.equals(userRole, AppRoleEnum.OPERATION_MANAGER.getCode()))) {

            CsFeedbackChatPO csFeedbackChatPO = new CsFeedbackChatPO ();
            csFeedbackChatPO.setUserId(RequestUtil.getUserIndex());
            csFeedbackChatPO.setId(csFeedbackChatCheckParm.getId());
            csFeedbackChatPO.setChatContent("反馈已收到，处理中");
            csFeedbackChatPO.setChatStatus ("0");
            csFeedbackChatPO.setStatus ("1");
            this.save (csFeedbackChatPO);
            CsFeedbackAuditParm csFeedbackAuditParm = new CsFeedbackAuditParm();
            csFeedbackAuditParm.setId(csFeedbackChatCheckParm.getId());
            csFeedbackAuditParm.setStatus("2");
            csFeedbackService.auditFeedBack(csFeedbackAuditParm);

        }

        return true;
    }
}
