package com.dgtly.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.dgtly.common.utils.DateUtils;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.system.domain.SysUser;
import com.dgtly.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.system.mapper.SysMagnetLogMapper;
import com.dgtly.system.domain.SysMagnetLog;
import com.dgtly.system.service.ISysMagnetLogService;
import com.dgtly.common.core.text.Convert;

/**
 * 磁铁日志Service业务层处理
 * 
 * @author dgtly
 * @date 2021-07-29
 */
@Service
public class SysMagnetLogServiceImpl implements ISysMagnetLogService 
{
    @Autowired
    private SysMagnetLogMapper sysMagnetLogMapper;
    @Autowired
    private SysUserMapper userMapper;


    /**
     * 查询磁铁日志
     * 
     * @param id 磁铁日志ID
     * @return 磁铁日志
     */
    @Override
    public SysMagnetLog selectSysMagnetLogById(Long id)
    {
        return sysMagnetLogMapper.selectSysMagnetLogById(id);
    }

    /**
     * 查询磁铁日志列表
     * 
     * @param sysMagnetLog 磁铁日志
     * @return 磁铁日志
     */
    @Override
    public List<SysMagnetLog> selectSysMagnetLogList(SysMagnetLog sysMagnetLog)
    {
        return sysMagnetLogMapper.selectSysMagnetLogList(sysMagnetLog);
    }

    /**
     * 新增磁铁日志
     * 
     * @param sysMagnetLog 磁铁日志
     * @return 结果
     */
    @Override
    public int insertSysMagnetLog(SysMagnetLog sysMagnetLog)
    {
        try{
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SysUser user =userMapper.selectUserById(sysMagnetLog.getUserId());
            sysMagnetLog.setLoginName(user.getLoginName());
            sysMagnetLog.setSapEmployeeId(user.getSysUserExt().getSapEmployeeId());
            //存时间时，舍弃毫秒数
            String createTimeStr=sf.format(new Date());
            sysMagnetLog.setCreateTime(sf.parse(createTimeStr));
            //计算渠道看板、产品看板、会员看板的停留时长
            //本次是主页、渠道看板、产品看板、会员看板、关闭数据看板
            if("主页".equals(sysMagnetLog.getMagnetName()) || "渠道看板".equals(sysMagnetLog.getMagnetName()) || "产品看板".equals(sysMagnetLog.getMagnetName()) || "会员看板".equals(sysMagnetLog.getMagnetName()) || "关闭数据看板".equals(sysMagnetLog.getMagnetName())
                    || "好邦手主页".equals(sysMagnetLog.getMagnetName()) || "好邦手渠道看板".equals(sysMagnetLog.getMagnetName()) || "好邦手产品看板".equals(sysMagnetLog.getMagnetName()) || "好邦手会员看板".equals(sysMagnetLog.getMagnetName())  || "好邦手关闭数据看板".equals(sysMagnetLog.getMagnetName())
                    || "随身邦主页".equals(sysMagnetLog.getMagnetName()) || "随身邦渠道看板".equals(sysMagnetLog.getMagnetName()) || "随身邦产品看板".equals(sysMagnetLog.getMagnetName()) || "随身邦会员看板".equals(sysMagnetLog.getMagnetName())  || "随身邦关闭数据看板".equals(sysMagnetLog.getMagnetName())){
               //判断是哪个入口
                String agent="";
                if(sysMagnetLog.getMagnetName().contains("好邦手")){
                    agent="好邦手";
                }else if(sysMagnetLog.getMagnetName().contains("随身邦")){
                    agent="随身邦";
                }
                //传参
                Map<String,Object> paramMap=new HashMap<String,Object>();
                paramMap.put("userId",sysMagnetLog.getUserId());
                paramMap.put("whatEnd",sysMagnetLog.getWhatEnd());
                List<String> magentList=new ArrayList<String>();
                magentList.add(agent+"渠道看板");
                magentList.add(agent+"产品看板");
                magentList.add(agent+"会员看板");
                paramMap.put("magentList",magentList);
                //查询最近的上次埋点
                SysMagnetLog sysMagnetLogLast = sysMagnetLogMapper.selectOneByCondition(paramMap);
                if(sysMagnetLogLast!=null && sysMagnetLogLast.getUpdateTime()==null){
                    //记录结束访问时间（仅渠道看板、产品看板、会员看板）
                    sysMagnetLogLast.setUpdateTime(sysMagnetLog.getCreateTime());
                    //记录上次的停留时间
                    long milliseconds = sysMagnetLogLast.getUpdateTime().getTime() - sysMagnetLogLast.getCreateTime().getTime();

                    long seconds = milliseconds / 1000;
                    //停留时间超过24小时，就不再存停留时间了，只保存离开时间
                    if(seconds<86400){
                        sysMagnetLogLast.setStayDuration(seconds);
                    }
                    //保存上次记录
                    sysMagnetLogMapper.updateSysMagnetLog(sysMagnetLogLast);
                }

            }
        }catch(Exception e){
            e.printStackTrace();

        }
        if (StringUtils.isNotEmpty(sysMagnetLog.getLevel())){
            return sysMagnetLogMapper.insertSysMagnetLog(sysMagnetLog);
        }else {
            return 0;
        }
    }

    /**
     * 修改磁铁日志
     * 
     * @param sysMagnetLog 磁铁日志
     * @return 结果
     */
    @Override
    public int updateSysMagnetLog(SysMagnetLog sysMagnetLog)
    {
        sysMagnetLog.setUpdateTime(DateUtils.getNowDate());
        return sysMagnetLogMapper.updateSysMagnetLog(sysMagnetLog);
    }

    /**
     * 删除磁铁日志对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysMagnetLogByIds(String ids)
    {
        return sysMagnetLogMapper.deleteSysMagnetLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除磁铁日志信息
     * 
     * @param id 磁铁日志ID
     * @return 结果
     */
    @Override
    public int deleteSysMagnetLogById(Long id)
    {
        return sysMagnetLogMapper.deleteSysMagnetLogById(id);
    }
}
