package com.dgtly.system.service.impl;

import java.util.List;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.system.domain.AlertConfiguration;
import com.dgtly.system.domain.SysUser;
import com.dgtly.system.mapper.AlertConfigurationMapper;
import com.dgtly.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.system.mapper.AlertUpvoteLogMapper;
import com.dgtly.system.domain.AlertUpvoteLog;
import com.dgtly.system.service.IAlertUpvoteLogService;
import com.dgtly.common.core.text.Convert;

/**
 * 弹框点赞日志Service业务层处理
 * 
 * @author dgtlc
 * @date 2024-12-24
 */
@Service
public class AlertUpvoteLogServiceImpl implements IAlertUpvoteLogService 
{
    @Autowired
    private AlertUpvoteLogMapper alertUpvoteLogMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private AlertConfigurationMapper alertConfigurationMapper;

    /**
     * 查询弹框点赞日志
     * 
     * @param alertUpvoteId 弹框点赞日志ID
     * @return 弹框点赞日志
     */
    @Override
    public AlertUpvoteLog selectAlertUpvoteLogById(Long alertUpvoteId)
    {
        return alertUpvoteLogMapper.selectAlertUpvoteLogById(alertUpvoteId);
    }

    /**
     * 查询弹框点赞日志列表
     * 
     * @param alertUpvoteLog 弹框点赞日志
     * @return 弹框点赞日志
     */
    @Override
    public List<AlertUpvoteLog> selectAlertUpvoteLogList(AlertUpvoteLog alertUpvoteLog)
    {
        return alertUpvoteLogMapper.selectAlertUpvoteLogList(alertUpvoteLog);
    }

    /**
     * 新增弹框点赞日志
     * 
     * @param alertUpvoteLog 弹框点赞日志
     * @return 结果
     */
    @Override
    public int insertAlertUpvoteLog(AlertUpvoteLog alertUpvoteLog)
    {
        alertUpvoteLog.setCreateTime(DateUtils.getNowDate());
        if (alertUpvoteLog.getStatus()==0){
            return 0;
        }else {
            extracted(alertUpvoteLog);
            return alertUpvoteLogMapper.insertAlertUpvoteLog(alertUpvoteLog);
        }

    }

    /**
     * 修改弹框点赞日志
     * 
     * @param alertUpvoteLog 弹框点赞日志
     * @return 结果
     */
    @Override
    public int updateAlertUpvoteLog(AlertUpvoteLog alertUpvoteLog)
    {
        alertUpvoteLog.setUpdateTime(DateUtils.getNowDate());
        if (alertUpvoteLog.getStatus()==0){
            return 0;
        }else {
            extracted(alertUpvoteLog);
            return alertUpvoteLogMapper.updateAlertUpvoteLog(alertUpvoteLog);
        }
    }

    /**
     * 删除弹框点赞日志对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlertUpvoteLogByIds(String ids)
    {
        return alertUpvoteLogMapper.deleteAlertUpvoteLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除弹框点赞日志信息
     * 
     * @param alertUpvoteId 弹框点赞日志ID
     * @return 结果
     */
    @Override
    public int deleteAlertUpvoteLogById(Long alertUpvoteId)
    {
        return alertUpvoteLogMapper.deleteAlertUpvoteLogById(alertUpvoteId);
    }

    /**
     * @description: 根据用户ID、弹框ID查询是否有记录
     * @param userId 用户ID
     * @param alertId 弹框ID
     * @return com.dgtly.system.domain.AlertUpvoteLog
     * @author lc
     * @date 2024-12-25 11:12
     */
    @Override
    public AlertUpvoteLog selectUpvoteByUidAndAid(Long userId, Long alertId) {
        return alertUpvoteLogMapper.selectUpvoteByUidAndAid(userId, alertId);
    }

    private void extracted(AlertUpvoteLog alertUpvoteLog) {
        AlertConfiguration alertConfiguration = alertConfigurationMapper.selectAlertConfigurationById(alertUpvoteLog.getAlertId());
        alertUpvoteLog.setAlertTitle(alertConfiguration.getAlertTitle());
        alertUpvoteLog.setAlertContent(alertConfiguration.getAlertContent());
        alertUpvoteLog.setPhotoUrl(alertConfiguration.getPhotoUrl());
        SysUser sysUser = sysUserMapper.selectAllUserByUserId(alertUpvoteLog.getUserId());
        alertUpvoteLog.setUserName(sysUser.getUserName());
        alertUpvoteLog.setLoginName(sysUser.getLoginName());
        alertUpvoteLog.setSapEmployeeId(sysUser.getSysUserExt().getSapEmployeeId());
        alertUpvoteLog.setPostName(sysUser.getSysUserExt().getPostName());
        String salesLevel = sysUser.getSysUserExt().getSalesLevel();
        alertUpvoteLog.setSalesLevel(salesLevel);
        if ("company_level".equals(salesLevel)) {
            alertUpvoteLog.setCompanyName(sysUser.getSysUserExt().getOrgName());
        } else if("office_level".equals(salesLevel)||"saledept_level".equals(salesLevel)){
            AlertUpvoteLog nameByCode = alertUpvoteLogMapper.selectMasterNameByCode(null, sysUser.getSysUserExt().getOrgCode(), null);
            if (nameByCode!=null) {
                alertUpvoteLog.setCompanyName(nameByCode.getCompanyName());
                alertUpvoteLog.setOfficeName(nameByCode.getOfficeName());
            }
        }else if("emp_level".equals(salesLevel)||"suboffice_level".equals(salesLevel)){
            AlertUpvoteLog nameByCode = alertUpvoteLogMapper.selectMasterNameByCode(null, null, sysUser.getSysUserExt().getOrgCode());
            if (nameByCode!=null) {
                alertUpvoteLog.setCompanyName(nameByCode.getCompanyName());
                alertUpvoteLog.setOfficeName(nameByCode.getOfficeName());
                alertUpvoteLog.setSubOfficeName(nameByCode.getSubOfficeName());
            }
        }else if("customer_level".equals(salesLevel)||"reseller_level".equals(salesLevel)){
            AlertUpvoteLog nameByCode = alertUpvoteLogMapper.selectMasterNameByCode(sysUser.getSysUserExt().getOrgCode(), null,null );
            if (nameByCode!=null) {
                alertUpvoteLog.setCompanyName(nameByCode.getCompanyName());
                alertUpvoteLog.setOfficeName(nameByCode.getOfficeName());
                alertUpvoteLog.setSubOfficeName(nameByCode.getSubOfficeName());
            }
        } else if ("ysl_emp".equals(salesLevel)) {
            alertUpvoteLog.setSubOfficeName(sysUser.getSysUserExt().getOrgName());
        }
    }
}
