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.bo.AnnualReportPropertyRightBo;
import com.goafanti.cognizance.bo.OrgIntellectualPropertyDetailBo;
import com.goafanti.cognizance.service.OrgIntellectualPropertyService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.OrgIntellectualPropertyMapper;
import com.goafanti.common.model.OrgIntellectualProperty;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;

@Service
public class OrgIntellectualPropertyServiceImpl extends BaseMybatisDao<OrgIntellectualPropertyMapper>
		implements OrgIntellectualPropertyService {
	@Autowired
	private OrgIntellectualPropertyMapper orgIntellectualPropertyMapper;

	@Override
	public OrgIntellectualProperty insert(OrgIntellectualProperty orgIntellectualProperty) {
		orgIntellectualPropertyMapper.insert(orgIntellectualProperty);
		return orgIntellectualProperty;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrgIntellectualProperty> listIntellectual(Integer pageNo, Integer pageSize, String uid,
			String startDate, String endDate) {
		Map<String, Object> params = new HashMap<>();
		Date sDate = null;
		Date eDate = null;
		if (!StringUtils.isBlank(uid)) {
			params.put("uid", uid);
		}

		if (!StringUtils.isBlank(startDate)) {
			try {
				sDate = DateUtils.parseDate(startDate, AFTConstants.YYYYMMDD);
			} catch (ParseException e) {
			}
		}

		if (!StringUtils.isBlank(endDate)) {
			try {
				eDate = DateUtils.addDays(DateUtils.parseDate(endDate, AFTConstants.YYYYMMDD), 1);
			} catch (ParseException e) {
			}
		}

		if (null != sDate) {
			params.put("sDate", sDate);
		}

		if (null != eDate) {
			params.put("eDate", eDate);
		}

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

		if (pageSize == null || pageSize < 0) {
			pageSize = 10;
		}
		return (Pagination<OrgIntellectualProperty>) findPage("findOrgIntellectualPropertyListByPage",
				"findOrgIntellectualPropertyCount", params, pageNo, pageSize);
	}

	@Override
	public int delete(String id) {
		return orgIntellectualPropertyMapper.deleteByPrimaryKey(id);
	}

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

	@Override
	public int updateByPrimaryKeySelective(OrgIntellectualProperty orgIntellectualProperty) {
		return orgIntellectualPropertyMapper.updateByPrimaryKeySelective(orgIntellectualProperty);
	}

	@Override
	public OrgIntellectualProperty selectOrgIntellectualPropertyByPid(String pid) {
		return orgIntellectualPropertyMapper.selectOrgIntellectualPropertyByPid(pid);
	}

	@Override
	public OrgIntellectualProperty selectByPrimaryKey(String id) {
		return orgIntellectualPropertyMapper.selectByPrimaryKey(id);
	}

	@Override
	public OrgIntellectualPropertyDetailBo selectPatentTypeDetail(String id) {
		return orgIntellectualPropertyMapper.selectPatentTypeDetail(id);
	}

	@Override
	public OrgIntellectualPropertyDetailBo selectCopyrightTypeDetail(String id) {
		return orgIntellectualPropertyMapper.selectCopyrightTypeDetail(id);
	}

	@Override
	public AnnualReportPropertyRightBo selectIntellectualPropertyCount(Integer year, String uid) {
		AnnualReportPropertyRightBo p = orgIntellectualPropertyMapper.selectIntellectualPropertyCount(year, uid);
		if (null != p) {
			p.setInventionPatent(null == p.getInventionPatent() ? 0 : p.getInventionPatent());
			p.setDefensePatent(null == p.getDefensePatent() ? 0 : p.getDefensePatent());
			p.setNationalCrop(null == p.getNationalCrop() ? 0 : p.getNationalCrop());
			p.setNewPlantCariety(null == p.getNewPlantCariety() ? 0 : p.getNewPlantCariety());
			p.setNationalDrug(null == p.getNationalDrug() ? 0 : p.getNationalDrug());
			p.setChineseMedicine(null == p.getChineseMedicine() ? 0 : p.getChineseMedicine());
			p.setUtilityPatent(null == p.getUtilityPatent() ? 0 : p.getUtilityPatent());
			p.setCircuitDesign(null == p.getCircuitDesign() ? 0 : p.getCircuitDesign());
			p.setExteriorPatent(null == p.getExteriorPatent() ? 0 : p.getExteriorPatent());
			p.setSoftwareWorks(null == p.getSoftwareWorks() ? 0 : p.getSoftwareWorks());
		}
		return p;
	}

	@Override
	public List<OrgIntellectualProperty> selectIntellectualPropertyList(Integer year, String uid) {
		return orgIntellectualPropertyMapper.selectIntellectualPropertyList(year, uid);
	}

	@Override
	public OrgIntellectualProperty findByUidAndIntellectualPropertyNumber(String uid,
			String intellectualPropertyNumber) {
		return orgIntellectualPropertyMapper.findByUidAndIntellectualPropertyNumber(uid, intellectualPropertyNumber);
	}

}
