package com.jpsoft.shinestar.modules.base.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;

import com.jpsoft.shinestar.modules.base.dao.*;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.service.PersonFamilyService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="personFamilyService")
public class PersonFamilyServiceImpl implements PersonFamilyService {
	@Resource(name="personFamilyDAO")
	private PersonFamilyDAO personFamilyDAO;

	@Resource(name="personInfoDAO")
	private PersonInfoDAO personInfoDAO;

	@Resource(name="personPopedomDAO")
	private PersonPopedomDAO personPopedomDAO;

	@Resource(name="companyStructureInfoDAO")
	private CompanyStructureInfoDAO companyStructureInfoDAO;

	@Resource(name="companyInfoDAO")
	private CompanyInfoDAO companyInfoDAO;

	@Override
	public PersonFamily get(String id) {
		// TODO Auto-generated method stub
		return personFamilyDAO.get(id);
	}

	@Override
	public int insert(PersonFamily model) {
		// TODO Auto-generated method stub

		String popedomId = model.getPopedomId();
		PersonPopedom popedom = personPopedomDAO.get(popedomId);

		//小区
		String companyId="";
		//房号
		String structureId="";

		CompanyStructureInfo companyStructureInfo = companyStructureInfoDAO.get(popedom.getCompanyStructureId());

		if(companyStructureInfo!=null){
			CompanyInfo companyInfo = companyInfoDAO.get(companyStructureInfo.getCompanyId());

			structureId = companyStructureInfo.getId();

			if(companyInfo!=null){
				companyId = companyInfo.getId();
			}
		}

		PersonInfo personInfo = new PersonInfo();
		personInfo.setName(model.getName());
		personInfo.setIdCard(model.getIdCard());
		personInfo.setPhone(model.getPhone());
		personInfo.setCreateBy(model.getCreateBy());
		personInfo.setCreateTime(model.getCreateTime());
		personInfo.setDelFlag(false);
		personInfo.setCompanyId(companyId);
		personInfo.setFaceImageUrl(model.getFaceImageUrl());
		personInfo.setFaceEnabled(true);
		personInfo.setFaceBound(true);
		personInfo.setGuestEnabled(false);
		personInfo.setWechatNoticeEnabled(true);
		personInfo.setAllowViewLocal(true);
		personInfo.setIsSync(false);
		personInfo.setSex(model.getSex());

		personInfoDAO.insert(personInfo);


		PersonPopedom personPopedom = new PersonPopedom();
		personPopedom.setId(UUID.randomUUID().toString());
		personPopedom.setPersonId(personInfo.getId());
		personPopedom.setCreateBy(model.getCreateBy());
		personPopedom.setCreateTime(model.getCreateTime());
		personPopedom.setDelFlag(false);
		personPopedom.setCompanyId(companyId);
		personPopedom.setCompanyStructureId(structureId);

		if("0".equals(model.getType())) {
			//家庭成员
			personPopedom.setPersonRoleId("7");
		}
		else{
			//家庭成员
			personPopedom.setPersonRoleId("8");
		}
		personPopedom.setSceneId("2");

		personPopedomDAO.insert(personPopedom);

		personInfo.setPopedom(personPopedom.getId());
		personInfoDAO.update(personInfo);
		
		return personFamilyDAO.insert(model);
	}

	@Override
	public int update(PersonFamily model) {
		// TODO Auto-generated method stub
		return personFamilyDAO.update(model);		
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return personFamilyDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = personFamilyDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<PersonFamily> list() {
		// TODO Auto-generated method stub
		return personFamilyDAO.list();
	}
		
	@Override
	public Page<PersonFamily> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,List<Sort> sortList) {
        Page<PersonFamily> page = PageHelper.startPage(1,10).doSelectPage(()->{
            personFamilyDAO.search(searchParams,sortList);
        });
        
        return page;
	}
}