package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.WxBuser;
import com.iformall.mapper.WxBuserMapper;
import com.iformall.service.WxBuserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author gongbiao
 */
@Service
public class WxBuserServiceImpl implements WxBuserService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxBuserMapper wxBuserMapper;

    @Override
    public PageInfo<WxBuser> listAsPage(WxBuser record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxBuserMapper.findList(record));
    }

    @Override
    public WxBuser getById(Long id) {
        return wxBuserMapper.selectById(id);
    }

    @Override
    public WxBuser getByOpenId(WxBuser record) {
        return wxBuserMapper.findByOpenId(record);
    }

    @Override
    public WxBuser getByObject(WxBuser record) {
        try {
            return wxBuserMapper.selectOne(new QueryWrapper(record));
        } catch (Exception e) {
            logger.error("NOT found: " + e.getMessage());
        }
        return null;
    }

    @Override
    public int saveOrUpdate(WxBuser user) {
        int ret = 0;
        Date curr = new Date();
        if (user.getId() == null) {
            // 检查用户是否已有同一微信开放平台账号
            if (StringUtils.isNotBlank(user.getUnionId()) && StringUtils.isNotBlank(user.getOpenAppId())) {
                WxBuser oldUser = wxBuserMapper.findByUnionId(user);
                if (oldUser != null) {
                    // 已有，更新
                    user.setId(oldUser.getId());
                    // TODO 是否其他信息需要更新
                    ret = wxBuserMapper.updateById(user);
                    return ret;
                }
            }
            final IdWorker idWorker = IdWorker.get();
            user.setId(idWorker.nextId());
            user.setLoginCount(1);
            user.setCreateDate(curr);
            user.setUpdateDate(curr);
            ret = wxBuserMapper.insert(user);
        } else {
            user.setUpdateDate(curr);
            ret = wxBuserMapper.updateById(user);
        }

        return ret;
    }

    @Override
    public void deleteById(Long id) {
        wxBuserMapper.deleteById(id);
    }


    @Override
    public int updateScene(WxBuser record) {
        Date curr = new Date();
        WxBuser userL = new WxBuser();
        userL.setId(record.getId());
        userL.setScene(record.getScene());
        userL.setUpdateDate(curr);
        int ret = wxBuserMapper.updateById(userL);
        return ret;
    }

    @Override
    public int updateLBS(WxBuser record) {
        Date curr = new Date();
        WxBuser userL = new WxBuser();
        userL.setId(record.getId());
        userL.setLongitude(record.getLongitude());
        userL.setLatitude(record.getLatitude());
        userL.setUpdateDate(curr);
        int ret = wxBuserMapper.updateById(userL);
        return ret;
    }

    @Override
    public int updateExtInfo(WxBuser record) {
        Date curr = new Date();
        WxBuser userL = new WxBuser();
        userL.setId(record.getId());
        userL.setExtraInfo(record.getExtraInfo());
        userL.setUpdateDate(curr);
        int ret = wxBuserMapper.updateById(userL);
        return ret;
    }


}
