package com.flossom.miniProgram.service.impl;

import com.flossom.common.core.domain.entity.CtLeaveMessage;
import com.flossom.common.core.domain.entity.WxScriptMessage;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.domain.entity.WxUserScriptLog;
import com.flossom.common.core.enums.CtLeaveMessageEnum;
import com.flossom.common.core.enums.MessageTypeEnum;
import com.flossom.common.core.enums.WxUserIntegralMessageTypeEnum;
import com.flossom.common.core.mapper.CtLeaveMessageMapper;
import com.flossom.common.core.mapper.WxScriptMessageMapper;
import com.flossom.common.core.mapper.WxUserMemberMapper;
import com.flossom.common.core.mapper.WxUserScriptLogMapper;
import com.flossom.common.core.utils.DateUtils;
import com.flossom.common.security.utils.SecurityUtils;
import com.flossom.miniProgram.service.ICtleaveMeassageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class CtleaveMessageServiceImpl implements ICtleaveMeassageService {
    @Autowired
    private CtLeaveMessageMapper ctLeaveMessageMapper;

    @Autowired
    private WxScriptMessageMapper wxScriptMessageMapper;

    @Autowired
    private WxUserScriptLogMapper wxUserScriptLogMapper;

    @Autowired
    private WxUserMemberMapper wxUserMemberMapper;

    @Transactional
    @Override
    public int saveCtleaveMeassage(CtLeaveMessage ctLeaveMessage) {
        // 留言成功发送消息
        WxScriptMessage wxScriptMessage = wxScriptMessageMapper.selectOneByMessageType(MessageTypeEnum.SUBMIT_MESSAGE.getCode());
        if (wxScriptMessage != null) {
            WxUserScriptLog wxUserScriptLog = new WxUserScriptLog();
            BeanUtils.copyProperties(wxScriptMessage, wxUserScriptLog);
            wxUserScriptLog.setId(null);
            wxUserScriptLog.setWxUserId(SecurityUtils.getLoginUser().getWxUserMember().getId());
            wxUserScriptLog.setMessageType(WxUserIntegralMessageTypeEnum.LEAVE_MESSAGE.getCode());
            wxUserScriptLog.setIsCustom(0);
            wxUserScriptLog.setTitile(wxScriptMessage.getMessageTitle());
            wxUserScriptLog.setContent(wxScriptMessage.getMessageContent());
            wxUserScriptLog.setCreateTime(DateUtils.getNowDate());
            wxUserScriptLog.setCreateBy(null);
            wxUserScriptLog.setUpdateTime(null);
            wxUserScriptLog.setUpdateBy(null);
            wxUserScriptLogMapper.insertWxUserScriptLog(wxUserScriptLog);
        }


        // 保存消息
        CtLeaveMessageEnum ctLeaveMessageEnum = CtLeaveMessageEnum.fromString(ctLeaveMessage.getSource());
        ctLeaveMessage.setSourceName(ctLeaveMessageEnum.getRemark());
        ctLeaveMessage.setCreateTime(new Date());
        WxUserMember wxUserMember = wxUserMemberMapper.selectWxUserMemberById(SecurityUtils.getLoginUser().getWxUserMember().getId());
        if (wxUserMember != null) {
            ctLeaveMessage.setUserId(wxUserMember.getId());
            ctLeaveMessage.setUserNumber(wxUserMember.getId().intValue());
            ctLeaveMessage.setUserImg(wxUserMember.getHeadimg());
            ctLeaveMessage.setUserName(wxUserMember.getNickname());
            ctLeaveMessage.setUserPhone(wxUserMember.getMobile());
        }
        return ctLeaveMessageMapper.insertCtLeaveMessage(ctLeaveMessage);
    }
}
