package com.goafanti.sc.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goafanti.common.dao.ScAchievementMapper;
import com.goafanti.common.dao.ScPublisherMapper;
import com.goafanti.common.model.ScAchievement;
import com.goafanti.common.model.ScPublisher;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.sc.bo.ScAchievementListBo;
import com.goafanti.sc.bo.inputAchievement;
import com.goafanti.sc.service.ScAchievementService;

@Service
public class ScAchievementServiceImpl extends BaseMybatisDao<ScAchievementMapper> implements ScAchievementService 	{
	
	@Autowired
	private ScAchievementMapper	 scAchievementMapper;
	@Autowired
	private ScPublisherMapper	scPublisherMapper;

	@Override
	public int addAchievements(inputAchievement a) {
		ScPublisher s=new ScPublisher();
		String pid=UUID.randomUUID().toString();
		a.setId(UUID.randomUUID().toString());
		a.setPublisherId(pid);
		scAchievementMapper.insertSelective(a);
		s.setId(pid);
		if(StringUtils.isNotBlank(a.getTelephone()))s.setTelephone(a.getTelephone());
		if(StringUtils.isNotBlank(a.getEmail()))s.setEmail(a.getEmail());
		if(StringUtils.isNotBlank(a.getZipCode()))s.setZipCode(a.getZipCode());
		if(StringUtils.isNotBlank(a.getpIntroduce()))s.setIntroduce(a.getpIntroduce());
		if(null!=a.getProvince())s.setProvince(a.getProvince());
		if(null!=a.getCity())s.setCity(a.getCity());
		if(StringUtils.isNotBlank(a.getPublisherName()))s.setContacts(a.getPublisherName());
		scPublisherMapper.insertSelective(s);
		/*ScOrganization o= new ScOrganization();
		o.setId(UUID.randomUUID().toString());
		o.setPublisherId(pid);
		if(StringUtils.isNotBlank(a.getFeature()))o.setFeature(a.getFeature());
		if(StringUtils.isNotBlank(a.getNature()))o.setNature(a.getNature());
		scOrganizationMapper.insertSelective(o);*/
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ScAchievementListBo> listAchievement(String ownerName, String patentNumber, String name,
			Integer pSize, Integer pNo) {
		Map<String, Object> params =new HashMap<>();
		if (pNo == null || pNo < 0)	pNo = 1;
		if (pSize == null || pSize < 0 || pSize > 10) pSize = 10;
		if(StringUtils.isNotBlank(name))params.put("name", name);
		if(StringUtils.isNotBlank(patentNumber))params.put("patentNumber", patentNumber);
		if(StringUtils.isNotBlank(ownerName))params.put("ownerName", ownerName);
		return (Pagination<ScAchievementListBo>) findPage("findAchievementListByPage",
				"findAchievementCount",params,
				pNo, pSize);
	}

	@Override
	public int delectachievement(String id) {
		ScAchievement s=new ScAchievement();
		s.setId(id);
		s.setDeleteSign(1);
		return scAchievementMapper.updateByPrimaryKeySelective(s);
	}

	@Override
	public int updateAchievement(inputAchievement a) {
		scAchievementMapper.updateByPrimaryKeySelective(a);
		ScAchievement sa=scAchievementMapper.selectByPrimaryKey(a.getId());
		ScPublisher s=new ScPublisher();
		s.setId(sa.getPublisherId());
		if(null!=a.getTelephone())s.setTelephone(a.getTelephone());
		if(null!=a.getEmail())s.setEmail(a.getEmail());
		if(null!=a.getZipCode())s.setZipCode(a.getZipCode());
		if(null!=a.getpIntroduce())s.setIntroduce(a.getpIntroduce());
		if(null!=a.getProvince())s.setProvince(a.getProvince());
		if(null!=a.getCity())s.setCity(a.getCity());
		if(null!=a.getPublisherName())s.setContacts(a.getPublisherName());
		scPublisherMapper.updateByPrimaryKeySelective(s);
		return 1;
	}

	@Override
	public inputAchievement AchievementDetails(String id) {
			inputAchievement s=scAchievementMapper.selectById(id);
			if(s!=null&&s.getIndustry()!=null){
				if (s.getIndustry()==1) {
						s.setIndustryName( "先进制造与自动化");
				}if (s.getIndustry()==2) {
					s.setIndustryName( "电子信息技术");
				}if (s.getIndustry()==3) {
					s.setIndustryName( "新材料技术");
				}if (s.getIndustry()==4) {
					s.setIndustryName( "生物与新药");
				}if (s.getIndustry()==5) {
					s.setIndustryName( "资源与环境");
				}if (s.getIndustry()==6) {
					s.setIndustryName( "新能源");
					}
			}
			return s;
		
	}
	
	
	
}
