package com.willalp.push.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.bean.BeanUtils;
import com.willalp.push.domain.HsAppPush;
import com.willalp.push.mapper.HsAppPushMapper;
import com.willalp.push.service.IHsAppPushService;
import com.willalp.yiqing.domain.vo.UserQueResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * APP用户推送标识信息Service业务层处理
 *
 * @author willalp
 * @date 2022-05-13
 */
@Service
public class HsAppPushServiceImpl extends ServiceImpl<HsAppPushMapper, HsAppPush> implements IHsAppPushService {
    @Resource
    private HsAppPushMapper hsAppPushMapper;

    /**
     * 查询APP用户推送标识信息列表
     *
     * @param hsAppPush APP用户推送标识信息
     * @return APP用户推送标识信息
     */
    @Override
    public List<HsAppPush> selectHsAppPushList(HsAppPush hsAppPush) {
        return hsAppPushMapper.selectHsAppPushList(hsAppPush);
    }

    @Override
    public int savePushInfo(UserQueResultVo userQueResultVo, String ipAddr) {
        //userQueResultVo.verify(userQueResultVo);
        HsAppPush appPushDb = hsAppPushMapper.selectOne(new QueryWrapper<HsAppPush>()
                .eq("user_no", userQueResultVo.getUserNo()));
        //存在并且一致不修改不新增
        if (ObjectUtil.isNotNull(appPushDb) && StringUtils.equals(userQueResultVo.getPushCid(), appPushDb.getPushCid())) {
            return 1;
        } else if (null != appPushDb && !StringUtils.equals(userQueResultVo.getPushCid(), appPushDb.getPushCid())) {
            appPushDb.setPushCid(userQueResultVo.getPushCid());
            return getBaseMapper().updateById(appPushDb);
        } else {
            //不存在时新增
            HsAppPush appPush = new HsAppPush();
            BeanUtils.copyBeanProp(appPush, userQueResultVo);
            appPush.setIpAddr(ipAddr);
            appPush.setCreateTime(new Date());
            return getBaseMapper().insert(appPush);
        }
    }

    @Override
    public String getOpenIdByUserNo(String un) {
        return getBaseMapper().getOpenIdByUserNo(un);
    }

    @Override
    public List<HsAppPush> getOpenIds() {
        return getBaseMapper().getOpenIds();
    }
}
