package com.goafanti.cognizance.service.impl;

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

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

import com.goafanti.cognizance.service.OrgActivityCostService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.OrgActivityCostMapper;
import com.goafanti.common.model.OrgActivityCost;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
@Service
public class OrgActivityCostServiceImpl extends BaseMybatisDao<OrgActivityCostMapper> implements OrgActivityCostService {
	@Autowired
	OrgActivityCostMapper orgActivityCostMapper;

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrgActivityCost> listOrgActivityCost(String activityNumber, String sDate,
			String eDate, Integer pageNo, Integer pageSize, String uid, Integer year) throws ParseException {
		Map<String, Object> params = new HashMap<>();
		
		Date startDate = null;
		Date endDate = null;
		
		if (!StringUtils.isBlank(activityNumber)){
			params.put("activityNumber", activityNumber);
		}
		
		if (year != null){
			params.put("year", year);
		}
		
		if (!StringUtils.isBlank(sDate)){
			startDate = DateUtils.parseDate(sDate, AFTConstants.YYYYMMDD);
			params.put("startDate", startDate);
		}
		
		if (!StringUtils.isBlank(eDate)){
			endDate = DateUtils.addDays(DateUtils.parseDate(eDate, AFTConstants.YYYYMMDD), 1);
			params.put("endDate", endDate);
		}
		
		if(!StringUtils.isBlank(uid)){
			params.put("uid", uid);
		}
		
		if (pageNo == null || pageNo < 0) {
			pageNo = 1;
		}

		if (pageSize == null || pageSize < 0) {
			pageSize = 10;
		}
		return (Pagination<OrgActivityCost>) findPage("findOrgActivityCostListByPage", "findOrgActivityCostCount", params, pageNo,
				pageSize);
	}
	

	@Override
	public int batchDeleteByPrimaryKey(List<String> id) {
		return orgActivityCostMapper.batchDeleteByPrimaryKey(id);
	}


	@Override
	public OrgActivityCost insert(OrgActivityCost orgActivityCost) {
		orgActivityCostMapper.insert(orgActivityCost);
		return orgActivityCost;
	}


	@Override
	public int updateByPrimaryKeySelective(OrgActivityCost orgActivityCost) {
		return orgActivityCostMapper.updateByPrimaryKeySelective(orgActivityCost);
	}


	@Override
	public List<OrgActivityCost> selectOrgActivityCostByAid(String aid) {
		return orgActivityCostMapper.selectOrgActivityCostByAid(aid);
	}


	@Override
	public int deleteByAid(List<String> aid) {
		return orgActivityCostMapper.batchDeleteByAid(aid);
	}


	@Override
	public OrgActivityCost selectOrgActivityCostByUidAndAidAndYear(String aid, String uid, Integer year) {
		return orgActivityCostMapper.selectOrgActivityCostByUidAndAidAndYear(aid, uid, year);
	}


	@Override
	public OrgActivityCost selectByPrimaryKey(String id) {
		return orgActivityCostMapper.selectByPrimaryKey(id);
	}


	@Override
	public OrgActivityCost selectOrgActivityCostByAidAndYear(String aid, Integer year) {
		return orgActivityCostMapper.selectOrgActivityCostByAidAndYear(aid, year);
	}
}
