package com.xxfc.platform.im.biz;

import com.github.wxiaoqi.security.admin.feign.dto.AppUserDTO;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.Query;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.im.dto.MsgTypeEnum;
import com.xxfc.platform.im.dto.QuestionParamDto;
import com.xxfc.platform.im.dto.UpdateTypeEnum;
import com.xxfc.platform.im.entity.ImQuestion;
import com.xxfc.platform.im.mapper.ImQuestionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class ImQuestionBiz extends BaseBiz<ImQuestionMapper, ImQuestion> {

    @Autowired
    UserBiz userBiz;

    /**
     * 获取列表
     *
     * @param questionParamDto
     * @return
     */
    public ObjectRestResponse getList(QuestionParamDto questionParamDto) {
        AppUserDTO appUserDTO = userBiz.getUserInfo();
        if (appUserDTO == null) {
            return ObjectRestResponse.createFailedResult(508, "token is null or invalid");
        }
        Query query = new Query(questionParamDto);
        PageDataVO pageDataVO = PageDataVO.pageInfo(query, () -> mapper.getQuestionList(query.getSuper()));
        return new ObjectRestResponse<>().data(pageDataVO);
    }

    /**
     * 添加问题
     *
     * @param imQuestion
     * @return
     */
    public ObjectRestResponse<ImQuestion> add(ImQuestion imQuestion) {
        AppUserDTO appUserDTO = userBiz.getUserInfo();
        if (appUserDTO == null) {
            return ObjectRestResponse.createFailedResult(508, "token is null or invalid");
        }
        imQuestion.setUserId(Long.parseLong(appUserDTO.getImUserid() + ""));
        if (imQuestion == null) {
            return ObjectRestResponse.paramIsEmpty();
        }
        mapper.insertSelective(imQuestion);
        return ObjectRestResponse.succ();
    }

    /**
     * 删除消息，修改isDel为true
     *
     * @param id
     * @return
     */
    public ObjectRestResponse delete(Long id) {
        AppUserDTO appUserDTO = userBiz.getUserInfo();
        if (appUserDTO == null) {
            return ObjectRestResponse.createFailedResult(508, "token is null or invalid");
        }
        if (id == null) {
            return ObjectRestResponse.paramIsEmpty();
        }
        ImQuestion imQuestion = mapper.selectByPrimaryKey(id);
        if (imQuestion == null) {
            return ObjectRestResponse.createDefaultFail();
        }
        imQuestion.setIsDel(true);
        mapper.updateByPrimaryKeySelective(imQuestion);
        return ObjectRestResponse.succ();
    }


    public ObjectRestResponse update(Long id, MsgTypeEnum type, UpdateTypeEnum updateType) {
        if (id == null || type == null) {
            return ObjectRestResponse.paramIsEmpty();
        }
        ImQuestion imQuestion = mapper.selectByPrimaryKey(id);
        if (imQuestion == null) {
            return ObjectRestResponse.createDefaultFail();
        }

        if (type == MsgTypeEnum.comment) { //评论
            synchronized (imQuestion) {
                if (updateType == UpdateTypeEnum.add) { //添加
                    imQuestion.setCommentCount(imQuestion.getCommentCount() + 1);
                } else {
                    imQuestion.setCommentCount((imQuestion.getCommentCount() > 0 ? imQuestion.getCommentCount() - 1 : 0));
                }
            }
        }
        if (type == MsgTypeEnum.praise) { //点赞
            synchronized (imQuestion) {
                if (updateType == UpdateTypeEnum.add) { //添加
                    imQuestion.setPraiseCount(imQuestion.getPraiseCount() + 1);
                } else {
                    imQuestion.setPraiseCount(imQuestion.getPraiseCount() > 0 ? imQuestion.getPraiseCount() - 1 : 0);
                }
            }
        }
        mapper.updateByPrimaryKeySelective(imQuestion);
        return ObjectRestResponse.succ();
    }

}
