package com.kmall.common.service.impl;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.common.dao.SysSmsLogDao;
import com.kmall.common.entity.SmsConfig;
import com.kmall.common.entity.SysSmsLogEntity;
import com.kmall.common.service.SysConfigService;
import com.kmall.common.service.SysSmsLogService;
import com.kmall.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author Scott
 * @date 2017-12-16 23:38:05
 */
@Service("smsLogService")
public class SysSmsLogServiceImpl implements SysSmsLogService {
    @Autowired
    private SysSmsLogDao smsLogDao;

    @Override
    public SysSmsLogEntity queryObject(String id) {
        return smsLogDao.queryObject(id);
    }

    @Override
    public List<SysSmsLogEntity> queryList(Map<String, Object> map) {
        return smsLogDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return smsLogDao.queryTotal(map);
    }

    @Override
    public int save(SysSmsLogEntity smsLog) {

        Map<String, Object> valideDate = MapBeanUtil.fromObject(smsLog);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }

        return smsLogDao.save(smsLog);
    }

    @Override
    public int update(SysSmsLogEntity smsLog) {

        Map<String, Object> valideDate = MapBeanUtil.fromObject(smsLog);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }

        return smsLogDao.update(smsLog);
    }

    @Override
    public int delete(String id) {
        return smsLogDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return smsLogDao.deleteBatch(ids);
    }

    @Override
    public SysSmsLogEntity sendSms(SysSmsLogEntity smsLog) {
        if (null == smsLog.getStime()) {
            smsLog.setStime(new Date());
        }
        smsLog.setMobile(smsLog.getMobile());
        smsLog.setSmsCode(smsLog.getSmsCode());
        //保存发送记录
        save(smsLog);
        return smsLog;
    }

    @Override
    public SysSmsLogEntity querySmsCodeByUserId(Long userId) {
        return smsLogDao.querySmsCodeByUserId(userId);
    }
}
