package com.goafanti.voucher.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

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

import com.goafanti.common.dao.JtVoucherMapper;
import com.goafanti.common.enums.VoucherSourceType;
import com.goafanti.common.model.JtVoucher;
import com.goafanti.common.model.User;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.voucher.bo.JtVoucherListBo;
import com.goafanti.voucher.service.VoucherService;
@Service
public class VoucherServiceImpl extends BaseMybatisDao<JtVoucherMapper> implements VoucherService {

	@Autowired
	JtVoucherMapper jtVoucherMapper;
	@Override
	public int saveVoucher(JtVoucher v) {
		v.setId(UUID.randomUUID().toString());
		v.setActiveState(1);//活动进行中
		if (TokenManager.getToken() instanceof User&&TokenManager.getUserId()!=null) {
			v.setGrantingUid(TokenManager.getUserId());
		}
		//v.setGrantingUid("a8cd3226-2e6e-43c7-b4b0-1b72bd9cfa41");
		return jtVoucherMapper.insertSelective(v);
	}

	@Override
	public int updateVoucher(JtVoucher v) {
		return jtVoucherMapper.updateByPrimaryKeySelective(v);
	}

	@Override
	public int delectVoucher(String id) {
		return jtVoucherMapper.deleteByPrimaryKey(id);
	}

	@Override
	public JtVoucher selectVoucher(String id) {
		return jtVoucherMapper.selectByPrimaryKey(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<JtVoucherListBo> selectVoucherList(String name, Integer limitUse, Integer activeState, Integer source,
			String uid,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 != limitUse) params.put("limitUse", limitUse);
		if (null != activeState) params.put("activeState", activeState);
		if (null != source) params.put("source", source);
		if(uid==null&&TokenManager.getToken() instanceof User)params.put("uid", TokenManager.getUserId());
		if (StringUtils.isNotBlank(uid))params.put("uid", uid);
		//params.put("uid", "1");
		return (Pagination<JtVoucherListBo>) findPage("selectVoucherList", "selectVoucherCount",params,
				pageNo, pageSize);
	}

	@Override
	public List<String> getSourceVoucher(Integer source) {
		return jtVoucherMapper.getSourceVoucher(source);
	}

	@Override
	public List<JtVoucherListBo> selectVoucherByClassify(String name,Integer isHot, Integer size) {
		List<JtVoucherListBo> list=jtVoucherMapper.selectVoucherByClassify( name,  isHot, size);
		return list; 
	}

	@Override
	public List<JtVoucherListBo> selectActivityVoucher() {
		List<JtVoucherListBo> list=new ArrayList<JtVoucherListBo>();
		list.add(jtVoucherMapper.selectByName("承制资格认证抵用券",1));
		list.add(jtVoucherMapper.selectByName("保密资格认证抵用券",1));
		list.add(jtVoucherMapper.selectByName("生产许可认证抵用券",1));
		list.add(jtVoucherMapper.selectByName("高新认定抵用券",1));
		return list;
	}

}
