package com.goafanti.cognizance.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.goafanti.cognizance.bo.AnnualReportBo;
import com.goafanti.cognizance.bo.AnnualReportMainBo;
import com.goafanti.cognizance.bo.AnnualReportPropertyRightBo;
import com.goafanti.cognizance.service.OrgAnnualReportService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.OrgAnnualReportMapper;
import com.goafanti.common.model.OrgAnnualReport;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;

@Service
public class OrgAnnualReportServiceImpl extends BaseMybatisDao<OrgAnnualReportMapper>
		implements OrgAnnualReportService {
	@Autowired
	private OrgAnnualReportMapper orgAnnualReportMapper;

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrgAnnualReport> listOrgAnnualReport(Integer year, Integer state, Integer pageNo,
			Integer pageSize, String uid) {
		Map<String, Object> params = new HashMap<>();

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

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

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

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

		if (pageSize == null || pageSize < 0) {
			pageSize = 10;
		}
		return (Pagination<OrgAnnualReport>) findPage("findOrgAnnualReportListByPage", "findOrgAnnualReportCount",
				params, pageNo, pageSize);
	}

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

	@Override
	public OrgAnnualReport insert(OrgAnnualReport orgAnnualReport) {
		orgAnnualReportMapper.insert(orgAnnualReport);
		return orgAnnualReport;
	}

	@Override
	public int updateByPrimaryKeySelective(OrgAnnualReport orgAnnualReport) {
		return orgAnnualReportMapper.updateByPrimaryKeySelective(orgAnnualReport);
	}

	@Override
	public AnnualReportPropertyRightBo selectAnnualReportPropertyRightBoByYearAndUid(Integer year, String uid) {
		return orgAnnualReportMapper.selectAnnualReportPropertyRightBoByYearAndUid(year, uid);
	}

	@Override
	public BigDecimal selectAnnualReportTerritoryByYearAndUid(Integer year, String uid) {
		return orgAnnualReportMapper.selectAnnualReportTerritoryByYearAndUid(year, uid);
	}

	@Override
	public AnnualReportMainBo selectAnnualReportMainBoByYearAndUid(Integer year, String uid) {
		return orgAnnualReportMapper.selectAnnualReportMainBoByYearAndUid(year, uid);
	}

	@Override
	public OrgAnnualReport selectAnnualReportByYearAndUid(Integer year, String uid) {
		return orgAnnualReportMapper.selectAnnualReportByYearAndUid(year, uid);
	}

	@Override
	public AnnualReportBo selectAnnualReportBo(String uid, Integer year) {
		AnnualReportBo report = new AnnualReportBo();

		AnnualReportPropertyRightBo p = orgAnnualReportMapper.selectAnnualReportPropertyRightBoByYearAndUid(year, uid);
		if (null != p) {
			report.setInventionPatent(null == p.getInventionPatent() ? 0 : p.getInventionPatent());
			report.setDefensePatent(null == p.getDefensePatent() ? 0 : p.getDefensePatent());
			report.setNationalCrop(null == p.getNationalCrop() ? 0 : p.getNationalCrop());
			report.setNewPlantCariety(null == p.getNewPlantCariety() ? 0 : p.getNewPlantCariety());
			report.setNationalDrug(null == p.getNationalDrug() ? 0 : p.getNationalDrug());
			report.setChineseMedicine(null == p.getChineseMedicine() ? 0 : p.getChineseMedicine());
			report.setUtilityPatent(null == p.getUtilityPatent() ? 0 : p.getUtilityPatent());
			report.setCircuitDesign(null == p.getCircuitDesign() ? 0 : p.getCircuitDesign());
			report.setExteriorPatent(null == p.getExteriorPatent() ? 0 : p.getExteriorPatent());
			report.setSoftwareWorks(null == p.getSoftwareWorks() ? 0 : p.getSoftwareWorks());
		}

//		BigDecimal t = orgAnnualReportMapper.selectAnnualReportTerritoryByYearAndUid(year, uid);
//		report.setTerritory(null == t ? new BigDecimal(0) : t);

		AnnualReportMainBo m = orgAnnualReportMapper.selectAnnualReportMainBoByYearAndUid(year, uid);
		if (null != m) {
			report.setUid(m.getUid());
			report.setUnitName(m.getUnitName());
			report.setOrgCode(m.getOrgCode());
			report.setLocationProvince(m.getLocationProvince());
			report.setLocationCity(m.getLocationCity());
			report.setLocationArea(m.getLocationArea());
			report.setListed(m.getListed().toString());
			if (null != m.getListedDate()) {
				report.setListedDate(DateFormatUtils.format(m.getListedDate(), AFTConstants.YYYYMMDD));
			}
			report.setListedType(m.getListedType().toString());
			report.setStockCode(m.getStockCode());
			report.setFirmTotal(null == m.getFirmTotal() ? 0 : m.getFirmTotal());
			report.setTechTotal(null == m.getTechTotal() ? 0 : m.getTechTotal());
			report.setNewEmployment(null == m.getNewEmployment() ? 0 : m.getNewEmployment());
			report.setGraduateNumber(null == m.getGraduateNumber() ? 0 : m.getGraduateNumber());
			report.setSalesRevenue(null == m.getSalesRevenue() ? new BigDecimal(0) : m.getSalesRevenue());
			report.setGrossProfit(null == m.getGrossProfit() ? new BigDecimal(0) : m.getGrossProfit());
			report.setNetAsset(null == m.getNetAsset() ? new BigDecimal(0) : m.getNetAsset());
			report.setCertificateNumber(m.getCertificateNumber());
			if (null != m.getIssuingDate()) {
				report.setIssuingDate(DateFormatUtils.format(m.getIssuingDate(), AFTConstants.YYYYMMDDHHMMSS));
			}
			report.setContacts(m.getContacts());
			report.setResearchCost(null == m.getResearchCost() ? new BigDecimal(0) : m.getResearchCost());
			report.setTotalRevenue(null == m.getTotalRevenue() ? new BigDecimal(0) : m.getTotalRevenue());
			report.setLastYearRevenue(null == m.getLastYearRevenue() ? new BigDecimal(0) : m.getLastYearRevenue());
		}
		return report;
	}

}
