package com.mortals.xhx.module.bussinesscard.service.impl;
import com.mortals.xhx.base.system.user.model.UserEntity;
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 org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

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;
import org.springframework.util.StringUtils;

/**
* 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
    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);
            }
        }
    }
}