/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.business.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springblade.business.entity.UserOpinionComments;
import org.springblade.business.vo.UserOpinionCommentsVO;
import org.springblade.business.mapper.UserOpinionCommentsMapper;
import org.springblade.business.service.IUserOpinionCommentsService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户意见评论表 服务实现类
 *
 * @author BladeX
 * @since 2022-05-25
 */
@Service
@AllArgsConstructor
public class UserOpinionCommentsServiceImpl extends BaseServiceImpl<UserOpinionCommentsMapper, UserOpinionComments> implements IUserOpinionCommentsService {

	private final IUserClient userClient;

	@Override
	public List<UserOpinionCommentsVO> queryUserOpinionCommentsByUserOpinionId(String userOpinionId) {
		List<UserOpinionComments> result = this.baseMapper.queryUserOpinionCommentsByUserOpinionId(userOpinionId);
		if(result != null && result.size() > 0){
			List<UserOpinionCommentsVO> voResult = JSONArray.parseArray(JSONObject.toJSONString(result), UserOpinionCommentsVO.class);
			voResult.forEach(vo -> {
				try{
					if(StringUtils.isNotEmpty(String.valueOf(vo.getCreateUser())) && !"null".equals(String.valueOf(vo.getCreateUser()))){
						//获取用户信息
						User user = this.userClient.userInfoById(vo.getCreateUser()).getData();
						if(user != null){
							vo.setAvatar(user.getAvatar());
							vo.setUserName(user.getRealName());
						}
					}
				}catch (Exception e){
					e.printStackTrace();
				}
			});
			return voResult;
		}
		return null;
	}

	@Override
	public Map<String, Integer> selectCommentsAndGoodCountByUserOpinionKey(Long userOpinionKey) {
		Map<String,Integer> result = new HashMap<>();
		result.put("comments", this.baseMapper.countCommentsByUserOpinionKey(userOpinionKey));
		result.put("good", this.baseMapper.countGoodByUserOpinionKey(userOpinionKey));
		return result;
	}

	@Override
	public IPage<UserOpinionCommentsVO> selectUserOpinionCommentsPage(IPage<UserOpinionCommentsVO> page, UserOpinionCommentsVO userOpinionComments) {
		return page.setRecords(this.baseMapper.selectUserOpinionCommentsPage(page, userOpinionComments));
	}

}
