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

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.ElectricClientInfoDAO;
import com.jpsoft.shinestar.modules.base.dao.RoomInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.ElectricClientInfo;
import com.jpsoft.shinestar.modules.base.entity.RoomInfo;
import com.jpsoft.shinestar.modules.base.service.RoomInfoService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

@Transactional
@Component(value="roomInfoService")
public class RoomInfoServiceImpl implements RoomInfoService {
	@Resource(name="roomInfoDAO")
	private RoomInfoDAO roomInfoDAO;

	@Resource(name="electricClientInfoDAO")
	private ElectricClientInfoDAO electricClientInfoDAO;


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

	@Override
	public int insert(RoomInfo model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return roomInfoDAO.insert(model);
	}

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

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

	@Override
	public int deleteAll(RoomInfo roomInfo){
		if("1".equals(roomInfo.getType())){
			//园区
			//删除楼栋和房间和户号信息
			List<RoomInfo> buildingList = findByTypeAndParentId("2",roomInfo.getId());
			for(RoomInfo buliding : buildingList){
				buliding.setDelFlag(true);
				roomInfo.setUpdateBy(roomInfo.getUpdateBy());
				roomInfo.setUpdateTime(new Date());
				update(buliding);
				List<RoomInfo> roomInfoList = findByTypeAndParentId("3",buliding.getId());
				for(RoomInfo room : roomInfoList){
					room.setDelFlag(true);
					room.setUpdateBy(roomInfo.getUpdateBy());
					room.setUpdateTime(new Date());
					update(room);
					deleteClient(room);
				}
			}
		}else if("2".equals(roomInfo.getType())){
			//楼栋
			//删除房间和户号信息
			List<RoomInfo> roomInfoList = findByTypeAndParentId("3",roomInfo.getId());
			for(RoomInfo room : roomInfoList){
				room.setDelFlag(true);
				room.setUpdateBy(roomInfo.getUpdateBy());
				room.setUpdateTime(new Date());
				update(room);
				deleteClient(room);
			}
		}else if("3".equals(roomInfo.getType())){
			//删除户号信息
			deleteClient(roomInfo);
		}
		return roomInfoDAO.update(roomInfo);
	}

	@Override
	public int deleteClient(RoomInfo roomInfo) {
		ElectricClientInfo electricClientInfo = electricClientInfoDAO.findOneByRoomId(roomInfo.getId());
		if(electricClientInfo != null) {
			electricClientInfo.setDelFlag(true);
			electricClientInfo.setUpdateBy(roomInfo.getUpdateBy());
			electricClientInfo.setUpdateTime(new Date());
			return electricClientInfoDAO.update(electricClientInfo);
		}else {
			return 0;
		}
	}

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

	@Override
	public List<RoomInfo> findByTypeAndParentId(String type, String parentId) {
		return roomInfoDAO.findByTypeAndParentId(type,parentId);
	}

	@Override
	public List<String> findIdByTypeAndParentIds(String type,List<String> parentIds){
		return roomInfoDAO.findIdByTypeAndParentIds(type,parentIds);
	}
}