package com.cyksj.service.suggest.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.*;
import com.cyksj.model.entity.*;
import com.cyksj.service.suggest.UserSuggestService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目名: yhlxj2
 * 文件名: UserSuggestServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/10/11 17:10
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserSuggestServiceImpl implements UserSuggestService {
	private final UserFeedbackRecordMapper userFeedbackRecordMapper;

	private final UserFeedbackProgressRecordMapper userFeedbackProgressRecordMapper;

	private final UserRecommendGoodsRecordMapper userRecommendGoodsRecordMapper;

	private final GoodsDonMapper goodsDonMapper;

	private final UserMapper userMapper;

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void suggestGoodsUsage(UserFeedbackProgressRecord progressRecord) {
		Long goodsId = progressRecord.getGoodsId();
		Long userId = progressRecord.getUserId();
		String imgs = progressRecord.getImgs();
		String content = progressRecord.getContent();
		if (StrUtil.isAllEmpty(imgs, content)) {
			throw BusinessRuntimeException.getInstance("请填写问题内容或截图");
		}
		if (StrUtil.isNotBlank(imgs) && imgs.length() > 2024) {
			throw BusinessRuntimeException.getInstance("图片过多");
		}
		if (StrUtil.isNotEmpty(content) && content.length() > 1000) {
			throw BusinessRuntimeException.getInstance("问题内容描述过多");
		}
		String phone = progressRecord.getPhone();
		if (StrUtil.isEmpty(phone)) {
			throw BusinessRuntimeException.getInstance("请输入正确的手机号");
		}
		String goodsTitle = null;
		if (goodsId != null && goodsId != 0) {
			GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
			Assert.notNull(goodsDon, "反馈的平台不存在");
			goodsTitle = goodsDon.getTitle();
		}
		User user = userMapper.selectById(userId);
		UserFeedbackRecord userFeedbackRecord = new UserFeedbackRecord();
		userFeedbackRecord.setUserId(userId);
		userFeedbackRecord.setGoodsId(goodsId);
		userFeedbackRecord.setGoodsTitle(goodsTitle);
		userFeedbackRecord.setPhone(phone);
		userFeedbackRecord.setDescription(content);
		userFeedbackRecord.setNickname(user.getNickname());
		userFeedbackRecordMapper.insert(userFeedbackRecord);

		progressRecord.setFeedbackId(userFeedbackRecord.getId());
		progressRecord.setNickname(user.getNickname());
		userFeedbackProgressRecordMapper.insert(progressRecord);
	}

	@Override
	public void recommendGoods(UserRecommendGoodsRecord userRecommendGoodsRecord) {
		String url = userRecommendGoodsRecord.getUrl();
		if (StrUtil.isEmpty(url) || url.length() > 512) {
			throw BusinessRuntimeException.getInstance("请输入正确的参考链接地址");
		}
		String goodsTitle = userRecommendGoodsRecord.getGoodsTitle();
		if (StrUtil.isEmpty(goodsTitle) || goodsTitle.length() > 128) {
			throw BusinessRuntimeException.getInstance("请填写正确的新品名称");
		}
		userRecommendGoodsRecordMapper.insert(userRecommendGoodsRecord);
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void userFeedbackMsg(UserFeedbackProgressRecord progressRecord) {
		Long feedbackId = progressRecord.getFeedbackId();
		Long userId = progressRecord.getUserId();
		if (feedbackId == null) {
			throw BusinessRuntimeException.getInstance("反馈留言错误");
		}
		UserFeedbackRecord userFeedbackRecord = userFeedbackRecordMapper.selectOne(Wrappers.lambdaQuery(UserFeedbackRecord.class)
				.eq(UserFeedbackRecord::getUserId, userId)
				.eq(UserFeedbackRecord::getId, feedbackId));
		if (userFeedbackRecord == null) {
			throw BusinessRuntimeException.getInstance("反馈记录不存在");
		}
		String content = progressRecord.getContent();
		String imgs = progressRecord.getImgs();
		if (StrUtil.isAllEmpty(imgs, content)) {
			throw BusinessRuntimeException.getInstance("请填写留言内容或截图");
		}
		if (StrUtil.isNotBlank(imgs) && imgs.length() > 2024) {
			throw BusinessRuntimeException.getInstance("图片过多");
		}
		if (StrUtil.isNotEmpty(content) && content.length() > 1000) {
			throw BusinessRuntimeException.getInstance("留言内容过多");
		}
		User user = userMapper.selectById(userId);
		progressRecord.setNickname(user.getNickname());
		userFeedbackProgressRecordMapper.insert(progressRecord);
		userFeedbackRecord.setIsNewMessage(true);
		userFeedbackRecordMapper.updateById(userFeedbackRecord);
	}
}
