package com.huatek.frame.base.service.impl;

import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.frame.base.dto.AreaDTO;
import com.huatek.frame.base.entity.Area;
import com.huatek.frame.base.mapper.AreaDao;
import com.huatek.frame.base.mapper.AreaMapper;
import com.huatek.frame.base.service.AreaService;
import com.huatek.frame.base.util.Constants;
import com.huatek.frame.base.vo.AreaVO;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.util.tools.HuatekTools;

/**
 * 区域 ServiceImpl
 *
 * @author key_dong
 * @date 2018-7-11 14:03:46
 */
@Component
@Service(interfaceClass = AreaService.class)
@Transactional(rollbackFor = Exception.class)
public class AreaServiceImpl implements AreaService {

	@Autowired
	private AreaMapper areaMapper;

	@Autowired
	private AreaDao areaDao;  

	@Override
	public Map<String, Object> findAreaPage(AreaDTO dto) {
		PageHelper.startPage(dto.getPage(), dto.getLimit());
		dto.setDeleted(Constants.DEFAULT_NO); 
		Page<AreaVO> areas = areaMapper.selectAreaPage(dto);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, areas.getTotal(), "", areas.getResult());
	}

	@Override
	public Map<String, Object> addArea(Area area) {
		area.setDeleted(Constants.DEFAULT_NO);
		areaDao.add(area);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> findArea(String id) {
		AreaVO vo = new AreaVO();
		if (!HuatekTools.isEmpty(id)) {
			Area entity = areaDao.get(id);
			BeanUtils.copyProperties(entity, vo);
		}
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
	}

	@Override
	public Map<String, Object> updateArea(Area area) {
		areaDao.updateLocal(area);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> deleteArea(String[] ids) {
		for (String id : ids) {
			Area area = new Area();
			area.setDeleted(Constants.DEFAULT_YES);
			area.setId(id);
			areaDao.updateLocal(area);
		}
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	} 
}