package shop.alien.second.service.impl;

import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import shop.alien.entity.second.SecondGoods;
import shop.alien.entity.store.LifeNotice;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.vo.WebSocketVo;
import shop.alien.mapper.LifeNoticeMapper;
import shop.alien.mapper.LifeUserMapper;
import shop.alien.second.feign.AlienStoreFeign;
import shop.alien.second.service.SecondGoodsNotificationService;
import shop.alien.util.common.Constants;

/**
 * 二手商品消息通知服务实现类
 * 负责商品相关的消息通知业务逻辑
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SecondGoodsNotificationServiceImpl implements SecondGoodsNotificationService {

    /**
     * 用户信息Mapper
     */
    private final LifeUserMapper lifeUserMapper;

    /**
     * 公告Mapper
     */
    private final LifeNoticeMapper lifeNoticeMapper;

    /**
     * 店铺服务Feign接口
     */
    private final AlienStoreFeign alienStoreFeign;

    /**
     * 发送审核成功消息
     * @param goods 商品信息
     */
    @Override
    public void sendMessage(SecondGoods goods) {
        // 根据 goods.getUserId() 获取用户信息
        LifeUser lifeUser = lifeUserMapper.selectById(goods.getUserId());
        String phone = lifeUser.getUserPhone();
        
        // 调取feign接口 发送消息 life_notice表
        LifeNotice lifeNotice = new LifeNotice();
        lifeNotice.setSenderId("system");
        lifeNotice.setReceiverId("user_" + phone);
        lifeNotice.setBusinessId(goods.getAuditRecordId());
        lifeNotice.setTitle("商品审核通知");
        
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("goodsId", goods.getId());
        jsonObject.put("status", "true");
        jsonObject.put("message", "恭喜您的商品已发布成功。");
        lifeNotice.setContext(jsonObject.toJSONString());
        lifeNotice.setNoticeType(Constants.Notice.SYSTEM_NOTICE); // 系统通知
        lifeNotice.setIsRead(0);
        lifeNoticeMapper.insert(lifeNotice);
        
        sendNotice("user_" + phone, lifeNotice);
    }

    /**
     * 发送审核失败消息
     * @param goods 商品信息
     */
    @Override
    public void sendFailedMsg(SecondGoods goods) {
        // 根据 goods.getUserId() 获取用户信息
        LifeUser lifeUser = lifeUserMapper.selectById(goods.getUserId());
        String phone = lifeUser.getUserPhone();
        
        // 调取feign接口 发送消息 life_notice表
        LifeNotice lifeNotice = new LifeNotice();
        lifeNotice.setSenderId("system");
        lifeNotice.setReceiverId("user_" + phone);
        lifeNotice.setBusinessId(goods.getAuditRecordId());
        lifeNotice.setTitle("商品审核通知");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("goodsId", goods.getId());
        jsonObject.put("status", "false");
        jsonObject.put("message", "抱歉您的商品发布失败，图片或文字存在违规行为，请您修改后重新发布。");
        lifeNotice.setContext(jsonObject.toJSONString());
        lifeNotice.setNoticeType(Constants.Notice.SYSTEM_NOTICE); // 系统通知
        lifeNotice.setIsRead(0);
        lifeNoticeMapper.insert(lifeNotice);
        
        sendNotice("user_" + phone, lifeNotice);
    }

    /**
     * 发送商品下架消息
     * @param goods 商品信息
     */
    @Override
    public void sendShelveMessage(SecondGoods goods) {
        try {
            // 根据 goods.getUserId() 获取用户信息
            LifeUser lifeUser = lifeUserMapper.selectById(goods.getUserId());
            String phone = lifeUser.getUserPhone();
            
            // 调取feign接口 发送消息 life_notice表
            LifeNotice lifeNotice = new LifeNotice();
            lifeNotice.setSenderId("system");
            lifeNotice.setReceiverId("user_" + phone);
            lifeNotice.setBusinessId(goods.getId());
            lifeNotice.setTitle("商品下架通知");
            
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goodsId", goods.getId());
            jsonObject.put("message", "您的商品：" + goods.getTitle() + " 已下架");
            lifeNotice.setContext(jsonObject.toJSONString());
            lifeNotice.setNoticeType(Constants.Notice.SYSTEM_NOTICE); // 系统通知
            lifeNotice.setIsRead(0);
            lifeNoticeMapper.insert(lifeNotice);
            
            sendNotice("user_" + phone, lifeNotice);
        } catch (Exception e) {
            log.error("发送消息通知失败，goods: {}", goods, e);
        }
    }

    /**
     * 发送通知消息
     * @param receiverId 接收者ID
     * @param lifeNotice 通知内容
     */
    private void sendNotice(String receiverId, LifeNotice lifeNotice) {
        try {
            WebSocketVo webSocketVo = new WebSocketVo();
            webSocketVo.setSenderId("system");
            webSocketVo.setReceiverId(receiverId);
            webSocketVo.setCategory("notice");
            webSocketVo.setNoticeType("1");
            webSocketVo.setIsRead(0);
            webSocketVo.setText(JSONObject.from(lifeNotice).toJSONString());
            alienStoreFeign.sendMsgToClientByPhoneId(receiverId, JSONObject.from(webSocketVo).toJSONString());
        } catch (Exception e) {
            log.error("发送消息通知失败，receiverId: {}", receiverId, e);
        }
    }
}

