package com.goafanti.demand.service.impl;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.DemandFollowDetailMapper;
import com.goafanti.common.dao.DemandFollowMapper;
import com.goafanti.common.enums.DemandFollowResult;
import com.goafanti.common.model.DemandFollow;
import com.goafanti.common.model.DemandFollowDetail;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.demand.bo.DemandFollowDetailListBo;
import com.goafanti.demand.bo.DemandFollowListBo;
import com.goafanti.demand.service.DemandFollowService;


@Service
public class DemandFollowServiceImpl extends BaseMybatisDao<DemandFollowMapper> implements DemandFollowService {

	@Autowired
	private DemandFollowMapper	demandFollowMapper;
	
	@Autowired
	private DemandFollowDetailMapper	demandFollowDetailMapper;


	@Override
	public int insertDemandFollow(DemandFollow d) {
		d.setId(UUID.randomUUID().toString());
		d.setResult(DemandFollowResult.FOLLOW.getCode());
		demandFollowMapper.insert(d);
		return 1;
	}

	@Override
	public int insertDemandFollowDetail(DemandFollowDetail d,String createTimeFormattedDate) {
		d.setId(UUID.randomUUID().toString());
		try {
			d.setCreateTime(DateUtils.parseDate(createTimeFormattedDate,AFTConstants.YYYYMMDD));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		demandFollowDetailMapper.insert(d);
		DemandFollow df=demandFollowMapper.selectByPrimaryKey(d.getDemandFollowId());
		df.setLastFollowTime(d.getCreateTime());
		df.setResult(d.getResult());
		demandFollowMapper.updateByPrimaryKey(df);
		return 1;
		}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<DemandFollowListBo> selectDemandFollow(String id, Integer pNo, Integer pSize) {
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0) {
			pSize = 10;
		}
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("id", id);
		Pagination<DemandFollowListBo> p=(Pagination<DemandFollowListBo>) findPage("findDemandFollowListByPage", "findDemandFollowCount", params, pNo, pSize);
		List<DemandFollowListBo> list =(List<DemandFollowListBo>) p.getList();
		for (DemandFollowListBo d : list) {
			d.setDemandFollowDetail(demandFollowDetailMapper.selectDemandFollowDetail(d.getId()));
			
		}
		return p;
	}

	@Override
	public int deleteDemandFollow(String id) {
		return demandFollowMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateDemandFollow(DemandFollow d) {
		
		return demandFollowMapper.updateByPrimaryKeySelective(d);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<DemandFollowDetailListBo> selectDemandFollowDetail(String id,Integer pNo, Integer pSize) {
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0) {
			pSize = 10;
		}
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("id", id);
		return (Pagination<DemandFollowDetailListBo>) findPage("findDemandFollowDetailListByPage", "findDemandFollowDetailCount", params, pNo, pSize);
			
		}
	}

	
