package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.microsvc.toolkit.common.webportal.exception.BizException;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.SysSuggestionType;
import com.yonge.cooleshow.biz.dal.entity.SysSuggestionV2;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.MessageTypeEnum;
import com.yonge.cooleshow.biz.dal.mapper.SysSuggestionTypeMapper;
import com.yonge.cooleshow.biz.dal.mapper.SysSuggestionV2Mapper;
import com.yonge.cooleshow.biz.dal.service.SysMessageService;
import com.yonge.cooleshow.biz.dal.service.SysSuggestionTypeService;
import com.yonge.cooleshow.biz.dal.service.SysSuggestionV2Service;
import com.yonge.cooleshow.biz.dal.wrapper.SysSuggestionWrapper;
import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 平台建议表
 * 2022-11-15 17:59:45
 */
@Slf4j
@Service
public class SysSuggestionV2ServiceImpl extends ServiceImpl<SysSuggestionV2Mapper, SysSuggestionV2> implements SysSuggestionV2Service {

    @Autowired
    private SysMessageService sysMessageService;

    @Autowired
    private SysSuggestionTypeMapper sysSuggestionTypeMapper;

	/**
     * 查询详情
     * @param id 详情ID
     * @return SysSuggestionWrapper.SysSuggestion
     */
	@Override
    public SysSuggestionV2 detail(Long id) {

        return baseMapper.selectById(id);
    }

    /**
     * 分页查询
     * @param page IPage<SysSuggestionWrapper.SysSuggestionList>
     * @param query SysSuggestionWrapper.SysSuggestionQuery
     * @return IPage<SysSuggestionWrapper.SysSuggestion>
     */
    @Override
    public IPage<SysSuggestionWrapper.SysSuggestion> selectPage(IPage<SysSuggestionWrapper.SysSuggestion> page,
                                                                SysSuggestionWrapper.SysSuggestionQuery query){

        return page.setRecords(baseMapper.selectPage(page, query));
    }

    /**
     * 添加
     * @param SysSuggestion SysSuggestionWrapper.SysSuggestion
     * @return Boolean
     */
    @Override
    public Boolean add(SysSuggestionV2 SysSuggestion) {

        return this.save(SysSuggestion);
    }

    /**
     * 更新
     * @param SysSuggestion SysSuggestionWrapper.SysSuggestion
     * @return Boolean
     */
    @Override
    public Boolean update(SysSuggestionV2 SysSuggestion){

        return this.updateById(SysSuggestion);
    }

    /**
     * 处理反馈意见
     * @param handleSuggestion handleSuggestion
     * @return true/false
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean handleSuggestion(SysSuggestionWrapper.HandleSuggestion handleSuggestion) {
        Long id = handleSuggestion.getId();
        SysSuggestionV2 suggestion = this.getById(id);
        if (suggestion == null) {
            throw new BizException("反馈意见不存在");
        }
        Boolean handleStatus = suggestion.getHandleStatus();
        if (Boolean.TRUE.equals(handleStatus)) {
            throw new BizException("反馈意见已经处理");
        }

        Date handTime = new Date();
        this.lambdaUpdate()
            .set(SysSuggestionV2::getHandleStatus, true)
            .set(SysSuggestionV2::getHandleTime, handTime)
            .set(SysSuggestionV2::getFeedback, handleSuggestion.getFeedback())
            .set(SysSuggestionV2::getFeedbackContent, handleSuggestion.getFeedbackContent())
            .set(SysSuggestionV2::getHandleBy,handleSuggestion.getHandleBy())
            .set(SysSuggestionV2::getHandleAttitude, handleSuggestion.getHandleAttitude())
            .eq(SysSuggestionV2::getId, handleSuggestion.getId())
            .eq(SysSuggestionV2::getHandleStatus, false)
            .update();

        Boolean feedback = handleSuggestion.getFeedback();
        if (feedback && StringUtils.isNotEmpty(handleSuggestion.getFeedbackContent())) {
            // 推送消息
            try {
                Map<Long, String> receivers = new HashMap<>();
                receivers.put(suggestion.getUserId(), suggestion.getMobileNo());
                MessageTypeEnum messageType = MessageTypeEnum.SYS_SUGGEST_FEEDBACK_STUDENT;
                if (ClientEnum.TEACHER.equals(suggestion.getClientType())) {
                    messageType = MessageTypeEnum.SYS_SUGGEST_FEEDBACK_TEACHER;
                }
                sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, messageType,
                        receivers, null, 0, null, null, handleSuggestion.getFeedbackContent());
            } catch (Exception e) {
                log.warn("意见反馈推送发送失败，{}", e.getMessage());
            }
        }
        return true;
    }
}
