package com.mortals.xhx.module.bussinesscard.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 com.mortals.xhx.module.bussinesscard.model.BussinesscardEntity;
import com.mortals.xhx.module.bussinesscard.model.BussinesscardRecordQuery;
import com.mortals.xhx.module.feedback.model.FeedbackEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

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.bussinesscard.dao.BussinesscardRecordDao;
import com.mortals.xhx.module.bussinesscard.model.BussinesscardRecordEntity;
import com.mortals.xhx.module.bussinesscard.service.BussinesscardRecordService;
import lombok.extern.slf4j.Slf4j;

/**
* BussinesscardRecordService
* 名片访问记录信息 service实现
*
* @author zxfei
* @date 2024-11-20
*/
@Service("bussinesscardRecordService")
@Slf4j
public class BussinesscardRecordServiceImpl extends AbstractCRUDServiceImpl<BussinesscardRecordDao, BussinesscardRecordEntity, Long> implements BussinesscardRecordService {

    @Autowired
    private UserService userService;

    @Override
    protected BussinesscardRecordEntity findBefore(BussinesscardRecordEntity params, PageInfo pageInfo, Context context) throws AppException {
        if(StringUtils.isNotEmpty(params.getQuery())){
            List<UserQuery> orConditionList = new ArrayList<>();
            UserQuery phone = new UserQuery();
            phone.setPhone(params.getQuery());
            orConditionList.add(phone);
            UserQuery realName = new UserQuery();
            realName.setRealName(params.getQuery());
            orConditionList.add(realName);
            UserQuery query = new UserQuery();
            query.setOrConditionList(orConditionList);
            UserEntity userEntity = userService.selectOne(query);
            if(userEntity!=null){
                params.setViewId(userEntity.getId());
            }else {
                params.setViewId(-1L);
            }
        }
        return params;
    }

    @Override
    public void saveRecord(BussinesscardEntity bussinesscardEntity, Context context) {
        if(context!=null&&context.getUser()!=null) {
            UserEntity userEntity = userService.get(context.getUser().getId());
            BussinesscardRecordEntity recordEntity = this.selectOne(new BussinesscardRecordQuery().bussinesscardId(bussinesscardEntity.getId()).viewId(context.getUser().getId()));
            if(recordEntity!=null){
                Map condition = new HashMap();
                condition.put("id",recordEntity.getId());
                Map data = new HashMap();
                data.put("sumViewsIncrement",1);
                data.put("viewTime",new Date());
                data.put("updateTime",new Date());
                if(StringUtils.isEmpty(recordEntity.getPhotoPath())){
                    data.put("photoPath",userEntity==null?"":userEntity.getPhotoPath());
                }
                this.dao.update(data,condition);
            }else {
                recordEntity = new BussinesscardRecordEntity();
                recordEntity.initAttrValue();
                recordEntity.setBussinesscardId(bussinesscardEntity.getId());
                recordEntity.setSumViews(1);
                recordEntity.setViewId(context.getUser().getId());
                recordEntity.setViewName(context.getUser().getRealName());
                recordEntity.setViewTime(new Date());
                recordEntity.setCreateTime(new Date());
                recordEntity.setPhotoPath(userEntity==null?"":userEntity.getPhotoPath());
                recordEntity.setCreateUserId(1l);
                this.save(recordEntity);
            }
        }
    }

    @Override
    protected void findAfter(BussinesscardRecordEntity params, PageInfo pageInfo, Context context, List<BussinesscardRecordEntity> list) throws AppException {
        super.findAfter(params, pageInfo, context, list);
        if(CollectionUtils.isNotEmpty(list)){
            List<Long> userIdList = list.stream().map(BussinesscardRecordEntity::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));
            list.forEach(item->{
                UserEntity userEntity = userMap.get(item.getCreateUserId());
                if(userEntity!=null) {
                    item.setNickName(userEntity.getNickName());
                    item.setRealName(userEntity.getRealName());
                    item.setMobile(userEntity.getPhone());
                    if(StringUtils.isEmpty(item.getEmail())) {
                        item.setEmail(userEntity.getEmail());
                    }
                    item.setCompanyName(userEntity.getCompanyAdress());
                }
            });
        }
    }
}