package com.goafanti.cognizance.service.impl;

import java.math.BigDecimal;
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.bo.ActivityNumberBo;
import com.goafanti.cognizance.bo.OrgActivityDetailCostBo;
import com.goafanti.cognizance.service.OrgActivityService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.OrgActivityCostMapper;
import com.goafanti.common.dao.OrgActivityMapper;
import com.goafanti.common.model.OrgActivity;
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 OrgActivityServiceImpl extends BaseMybatisDao<OrgActivityMapper> implements OrgActivityService {
	@Autowired
	private OrgActivityMapper		orgActivityMapper;
	@Autowired
	private OrgActivityCostMapper	orgActivityCostMapper;

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrgActivity> listOrgActivity(String activityNumber, String activityName, String sDate,
			String eDate, Integer pageNo, Integer pageSize, String uid) throws ParseException {
		Map<String, Object> params = new HashMap<>();

		Date startDate = null;
		Date endDate = null;

		if (!StringUtils.isBlank(activityNumber)) {
			params.put("activityNumber", activityNumber);
		}

		if (!StringUtils.isBlank(activityName)) {
			params.put("activityName", activityName);
		}

		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<OrgActivity>) findPage("findOrgActivityListByPage", "findOrgActivityCount", params, pageNo,
				pageSize);
	}

	@Override
	public OrgActivity insert(OrgActivity orgActivity) {
		orgActivityMapper.insert(orgActivity);
		return orgActivity;
	}

	@Override
	public int updateByPrimaryKeySelective(OrgActivity orgActivity) {
		return orgActivityMapper.updateByPrimaryKeySelective(orgActivity);
	}

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

	@Override
	public OrgActivity selectOrgActivityByPrimaryKey(String id) {
		return orgActivityMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<ActivityNumberBo> selectOrgActivityNumberBoByUid(String uid) {
		return orgActivityMapper.selectOrgActivityNumberBoByUid(uid);
	}

	@Override
	public void updateOrgActivity(OrgActivity orgActivity) {
		orgActivityMapper.updateByPrimaryKeySelective(orgActivity);
		List<OrgActivityCost> cost = orgActivityCostMapper.selectOrgActivityCostByAid(orgActivity.getId());
		if (null != cost && cost.size() > 0) {
			for (OrgActivityCost c : cost) {
				if (null != c) {
					c.setActivityNumber(orgActivity.getActivityNumber());
					c.setStartDate(orgActivity.getStartDate());
					c.setEndDate(orgActivity.getEndDate());
					orgActivityCostMapper.updateByPrimaryKeySelective(c);
				}
			}
		}
	}

	@Override
	public OrgActivity selectDetailByIdAndYear(String id, int year) {
		OrgActivityDetailCostBo oadcb = orgActivityCostMapper.selectDetailCostByIdAndYear(id, year);
		OrgActivity oa = orgActivityMapper.selectByPrimaryKey(id);
		if (null != oadcb) {
			oa.setFirstYearExpenditure(null == oadcb.getFirstYearCost() ? new BigDecimal(0) : oadcb.getFirstYearCost());
			oa.setSecondYearExpenditure(
					null == oadcb.getSecondYearCost() ? new BigDecimal(0) : oadcb.getSecondYearCost());
			oa.setThirdYearExpenditure(null == oadcb.getThirdYearCost() ? new BigDecimal(0) : oadcb.getThirdYearCost());
		}
		return oa;
	}

	@Override
	public List<OrgActivity> selectAcivityList(Integer year, String uid) {
		return orgActivityMapper.selectAcivityList(year, uid);
	}
}
