package com.goafanti.sc.service.impl;

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.dao.ScDemandMapper;
import com.goafanti.common.dao.ScOrganizationMapper;
import com.goafanti.common.dao.ScPublisherMapper;
import com.goafanti.common.model.ScDemand;
import com.goafanti.common.model.ScOrganization;
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.ScDemandListBo;
import com.goafanti.sc.bo.inputDemands;
import com.goafanti.sc.service.ScDemandService;

@Service
public class ScDmandServiceImpl extends BaseMybatisDao<ScDemandMapper> implements ScDemandService 	{
	
	@Autowired
	private ScDemandMapper	 scDemandMapper;
	@Autowired
	private ScPublisherMapper	scPublisherMapper;
	@Autowired
	private ScOrganizationMapper	scOrganizationMapper;

	

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ScDemandListBo> DemandList(String reserveButtUnit, String type, String name,String orgName, 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(orgName))params.put("orgName", orgName);
		if(StringUtils.isNotBlank(reserveButtUnit))params.put("reserveButtUnit", reserveButtUnit);
		if(StringUtils.isNotBlank(type))params.put("type", type);
		Pagination<ScDemandListBo>	p=	(Pagination<ScDemandListBo>) findPage("findDemandListByPage",
				"findDemandCount",params,
				pNo, pSize);
		List<ScDemandListBo> l=(List<ScDemandListBo>) p.getList();
		for (ScDemandListBo s : l) {
		if(s!=null&&s.getIndustry()!=null){
		if (s.getIndustry().equals("1")) {
				s.setIndustry( "先进制造与自动化");
		}if (s.getIndustry().equals("2")) {
			s.setIndustry( "电子信息技术");
		}if (s.getIndustry().equals("3")) {
			s.setIndustry( "新材料技术");
		}if (s.getIndustry().equals("4")) {
			s.setIndustry( "生物与新药");
		}if (s.getIndustry().equals("5")) {
			s.setIndustry( "资源与环境");
		}if (s.getIndustry().equals("6")) {
			s.setIndustry( "新能源");
			}
		}
		}
				return p;
	}

	@Override
	public int delectDemand(String id) {
		ScDemand s=new ScDemand();
		s.setId(id);
		s.setDeleteSign(1);
		return scDemandMapper.updateByPrimaryKeySelective(s);
	}

	@Override
	public int updateDemand(inputDemands a) {
		scDemandMapper.updateByPrimaryKeySelective(a);
		ScDemand d=scDemandMapper.selectByPrimaryKey(a.getId());
		ScPublisher s=new ScPublisher();
		ScOrganization o= new ScOrganization();
		s.setId(d.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.getPublisherName())s.setContacts(a.getPublisherName());
		if(null!=a.getProvince())s.setProvince(Integer.valueOf(a.getProvince()));
		if(null!=a.getPintroduce())s.setIntroduce(a.getPintroduce());
		if(null!=a.getRemarks())s.setRemarks(a.getRemarks());
		if(null!=a.getPname())s.setName(a.getPname());
		if(null!=a.getCity())s.setCity(Integer.valueOf(a.getCity()));
		if(null!=a.getAddress())s.setAddress(a.getAddress());
		scPublisherMapper.updateByPrimaryKeySelective(s);
		o.setPublisherId(d.getPublisherId());
		if(null!=a.getFeature())o.setFeature(a.getFeature());
		if(null!=a.getNature())o.setNature(a.getNature());
		if(null!=a.getLegalPerson())o.setLegalPerson(a.getLegalPerson());
		if(null!=a.getWebsite())o.setWebsite(a.getWebsite());
		scOrganizationMapper.updateBypublisherId(o);
		return 1;
	}

	@Override
	public inputDemands demandDetails(String id) {
		inputDemands s=scDemandMapper.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;
	}

	@Override
	public int addDemands(inputDemands a) {
		ScPublisher s=new ScPublisher();
		ScOrganization o= new ScOrganization();
		String pid=UUID.randomUUID().toString();
		a.setId(UUID.randomUUID().toString());
		a.setPublisherId(pid);
		scDemandMapper.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.getPublisherName()))s.setContacts(a.getPublisherName());
		if(null!=a.getProvince()&&!a.getProvince().equals(""))s.setProvince(Integer.valueOf(a.getProvince()));
		if(StringUtils.isNotBlank(a.getPintroduce()))s.setIntroduce(a.getPintroduce());
		if(StringUtils.isNotBlank(a.getRemarks()))s.setRemarks(a.getRemarks());
		if(StringUtils.isNotBlank(a.getPname()))s.setName(a.getPname());
		if(null!=a.getCity()&&!a.getCity().equals(""))s.setCity(Integer.valueOf(a.getCity()));
		if(StringUtils.isNotBlank(a.getAddress()))s.setAddress(a.getAddress());
		scPublisherMapper.insertSelective(s);
		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());
		if(StringUtils.isNotBlank(a.getLegalPerson()))o.setLegalPerson(a.getLegalPerson());
		if(StringUtils.isNotBlank(a.getWebsite()))o.setWebsite(a.getWebsite());
		scOrganizationMapper.insertSelective(o);
		return 1;
	}

	
	
	
	
}
