package com.goafanti.voucher.service.impl;



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

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.JtVoucherDetailMapper;

import com.goafanti.common.model.JtVoucherDetail;
import com.goafanti.common.model.User;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.JDBCIdGenerator;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.voucher.bo.JtVoucherDetailListBo;
import com.goafanti.voucher.service.VoucherDetailService;
@Service
public class VoucherDetailServiceImpl extends BaseMybatisDao<JtVoucherDetailMapper> implements VoucherDetailService {

	@Autowired
	JtVoucherDetailMapper jtVoucherDetailMapper;
	@Resource
	private JDBCIdGenerator	jDBCIdGenerator;

	@Override
	public int saveVoucherDetail(JtVoucherDetail voucher) {
		return jtVoucherDetailMapper.insertSelective(voucher);
	}

	@Override
	public int updateVoucherDetail(JtVoucherDetail voucher) {
		return jtVoucherDetailMapper.updateByPrimaryKeySelective(voucher);
	}

	@Override
	public int delectVoucherDetail(String id) {
		return jtVoucherDetailMapper.deleteByPrimaryKey(id);
	}

	@Override
	public JtVoucherDetail selectVoucherDetail(String id) {
		return jtVoucherDetailMapper.selectByPrimaryKey(id);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<JtVoucherDetailListBo> selectVoucherDetailList(String name, Integer status, String userMobile,
			Integer source,Integer sort,Integer sortType, Integer pageNo, Integer pageSize) {
		Map<String,Object> params = new HashMap<>();
		if (pageNo==null||pageNo<1) pageNo=1;
		if (pageSize==null||pageSize<1) pageSize=10;
		if (StringUtils.isNotBlank(name)) params.put("name", name);
		if (null != status) params.put("status", status);
		if (null != userMobile) params.put("userMobile", userMobile);
		if (null != source) params.put("source", source);
		if (null != sort) params.put("sort", sort);
		if (null != sortType) params.put("sortType", sortType);
		if(TokenManager.getToken() instanceof User)params.put("uid", TokenManager.getUserId());
		//params.put("uid", "a8cd3226-2e6e-43c7-b4b0-1b72bd9cfa41");
		Pagination<JtVoucherDetailListBo> pagination=	(Pagination<JtVoucherDetailListBo>) findPage("selectVoucherDetailList", "selectVoucherDetailCount",params,
						pageNo, pageSize);
		return pagination;
	}

	@Override
	public int saveUserGetVoucher(String uid, String vid) {
		JtVoucherDetail j=new JtVoucherDetail();
		j.setId(jDBCIdGenerator.generateId().toString());
		j.setReceiveTime(new Date());
		j.setVoucherId(vid);
		j.setUseUid(uid);
		j.setStatus(0);
		return  jtVoucherDetailMapper.insertSelective(j);
	}

	
	public List<String> getTokenVoucher() {
		return jtVoucherDetailMapper.getTokenVoucher(TokenManager.getUserId());
	}

	

	
}
