package com.goafanti.techproject.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
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.TechWebsiteMapper;
import com.goafanti.common.enums.DeleteStatus;
import com.goafanti.common.model.TechWebsite;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.techproject.bo.TechWebsiteDetailBo;
import com.goafanti.techproject.bo.TechWebsiteListBo;
import com.goafanti.techproject.service.TechWebsiteService;

@Service
public class TechWebsiteServiceImpl extends BaseMybatisDao<TechWebsiteMapper> implements TechWebsiteService {
	@Autowired
	private TechWebsiteMapper techWebsiteMapper;

	@Override
	public Map<String, String> getDepartment(String uid) {
		Map<String, String> map = new TreeMap<String, String>();
		List<TechWebsite> list = techWebsiteMapper.selectTechWebsiteByUid(uid);
		if (null != list && 0 != list.size()) {
			for (TechWebsite t : list) {
				map.put(t.getId(), t.getDepartment());
			}
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<TechWebsiteListBo> listTechWebsite(Integer province, String unitName, Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<>();
		if (null != province) {
			params.put("province", province);
		}
		if (!StringUtils.isBlank(unitName)) {
			params.put("unitName", unitName);
		}

		if (pageNo == null || pageNo < 0) {
			pageNo = 1;
		}

		if (pageSize == null || pageSize < 0) {
			pageSize = 10;
		}
		return (Pagination<TechWebsiteListBo>) findPage("findTechWebsiteListByPage", "findTechWebsiteCount", params, pageNo,
				pageSize);
	}

	@Override
	public int saveWebsite(TechWebsite t) {
		if (StringUtils.isBlank(t.getId())) {
			t.setId(UUID.randomUUID().toString());
			t.setDeletedSign(DeleteStatus.UNDELETE.getCode());
			Calendar now = Calendar.getInstance();
			now.set(Calendar.MILLISECOND, 0);
			t.setCreateTime(now.getTime());
			return techWebsiteMapper.insert(t);
		} else {
			return techWebsiteMapper.updateByPrimaryKeySelective(t);
		}
		
	}

	@Override
	public TechWebsite selectByPrimaryKey(String id) {
		return techWebsiteMapper.selectByPrimaryKey(id);
	}

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

	@Override
	public TechWebsiteDetailBo selectTechWebsiteDetail(String id) {
		return techWebsiteMapper.selectTechWebsiteDetail(id);
	}
}
