package com.mortals.xhx.module.feedback.service.impl;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.feedback.dao.FeedbackDao;
import com.mortals.xhx.module.feedback.model.FeedbackEntity;
import com.mortals.xhx.module.feedback.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;

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

/**
* FeedbackService
* 反馈信息 service实现
*
* @author zxfei
* @date 2023-09-18
*/
@Service("feedbackService")
@Slf4j
public class FeedbackServiceImpl extends AbstractCRUDServiceImpl<FeedbackDao, FeedbackEntity, Long> implements FeedbackService {

    @Autowired
    private UserService userService;

    @Override
    protected void saveBefore(FeedbackEntity entity, Context context) throws AppException {
        super.saveBefore(entity,context);
        if(StringUtils.isEmpty(entity.getTitle())){
            entity.setTitle("来自微信小程序的反馈");
        }
        if(context!=null&&context.getUser()!=null){
            UserEntity userEntity = userService.get(context.getUser().getId());
            if(userEntity!=null){
                entity.setFeedbackName(userEntity.getRealName());
                entity.setPhotoPath(userEntity.getPhotoPath());
                entity.setFeedbackName(userEntity.getRealName());
                entity.setCompanyName(userEntity.getCompanyAdress());
                if(StringUtils.isEmpty(entity.getContactInfo())){
                    entity.setContactInfo(userEntity.getMobile());
                    if(StringUtils.isEmpty(entity.getContactInfo())){
                        entity.setContactInfo(userEntity.getPhone());
                    }
                }
                if(StringUtils.isEmpty(entity.getEmail())){
                    entity.setEmail(userEntity.getEmail());
                }
            }
        }
    }

    @Override
    protected void updateBefore(FeedbackEntity entity, Context context) throws AppException {
        super.saveBefore(entity,context);
        if(StringUtils.isNotEmpty(entity.getReplyContent())){
            entity.setReply(1);
        }
    }

    @Override
    protected void findAfter(FeedbackEntity params, PageInfo pageInfo, Context context, List<FeedbackEntity> list) throws AppException {
        super.findAfter(params, pageInfo, context, list);
        if(CollectionUtils.isNotEmpty(list)){
            List<Long> userIdList = list.stream().map(FeedbackEntity::getCreateUserId).collect(Collectors.toList());
            List<UserEntity> userList = userService.find(new UserQuery().idList(userIdList));
            Map<Long,UserEntity> userMap = userList.parallelStream().collect(Collectors.toMap(x -> x.getId(), z -> z, (o, n) -> n));
            for(FeedbackEntity item:list){
                if(StringUtils.isNotEmpty(item.getReplyContent())){
                    item.setReply(1);
                }
                UserEntity userEntity = userMap.get(item.getCreateUserId());
                if(userEntity!=null) {
                    item.setPhotoPath(userEntity.getPhotoPath());
                    item.setFeedbackName(userEntity.getRealName());
                    item.setCompanyName(userEntity.getCompanyAdress());
                    if(StringUtils.isEmpty(item.getContactInfo())){
                        item.setContactInfo(userEntity.getMobile());
                    }
                    if(StringUtils.isEmpty(item.getEmail())){
                        item.setEmail(userEntity.getEmail());
                    }
                    if(StringUtils.isEmpty(item.getContactInfo())){
                        item.setContactInfo(userEntity.getMobile());
                        if(StringUtils.isEmpty(item.getContactInfo())){
                            item.setContactInfo(userEntity.getPhone());
                        }
                    }
                }
            }
        }
    }
}