package com.kmall.admin.fromcomm.service.impl;

import com.kmall.admin.fromcomm.dao.FormIdsDao;
import com.kmall.api.fromcomm.entity.FormIdsEntity;
import com.kmall.api.fromcomm.service.FormIdsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 小程序form_id收集Service实现类
 *
 * @author Scott
 * @email
 * @date 2017-12-16 15:32:50
 */
@Service("formidsService")
public class FormIdsServiceImpl implements FormIdsService {
    @Autowired
    private FormIdsDao formIdsDao;

    @Override
    public FormIdsEntity queryObject(Long id) {
        return formIdsDao.queryObject(id);
    }

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

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

    @Override
    public int save(FormIdsEntity formIds) {
        return formIdsDao.save(formIds);
    }

    @Override
    public int update(FormIdsEntity formIds) {
        return formIdsDao.update(formIds);
    }

    @Override
    public int delete(Integer id) {
        return formIdsDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return formIdsDao.deleteBatch(ids);
    }

    @Override
    public FormIdsEntity getFormIds(Long userId) {
        return formIdsDao.queryByUserId(userId);
    }

    @Override
    public FormIdsEntity getFormIdsByMerchOrderSn(String merchOrderSn) {
        return formIdsDao.getFormIdsByMerchOrderSn(merchOrderSn);
    }


    @Override
    public void releaseFormIds(FormIdsEntity formIds) {
        formIds.setValidNum(formIds.getValidNum() - 1);
        formIdsDao.update(formIds);
    }

    @Override
    public int save(Long userId, String formId, Integer validNum,String merchOrderSn) {
        // 保存form_id
        FormIdsEntity formIdsVo = new FormIdsEntity();
        long currentTime = System.currentTimeMillis() + 2 * 60 * 60 * 1000;
        Date date = new Date(currentTime);
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String nowTime="";
//        nowTime= df.format(date);
        formIdsVo.setExpireTime(date);
        formIdsVo.setFormId(formId);
        formIdsVo.setValidNum(3);
        formIdsVo.setUserId(userId);
        formIdsVo.setMerchOrderSn(merchOrderSn);
        return formIdsDao.save(formIdsVo);
    }
}
