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

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.frame.base.dto.GroupDTO;
import com.huatek.frame.base.entity.Group;
import com.huatek.frame.base.mapper.GroupDao;
import com.huatek.frame.base.mapper.GroupMapper;
import com.huatek.frame.base.service.GroupService;
import com.huatek.frame.base.util.Constants;
import com.huatek.frame.base.vo.GroupVO;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.WherePrams;
import com.huatek.torch.sql.where.W;
import com.huatek.torch.util.date.TimeUtil;
import com.huatek.torch.util.tools.HuatekTools;
import com.huatek.torch.util.uuid.Uuid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * 系统_组织 ServiceImpl
 *
 * @author key_dong
 * @date 2018-7-11 14:03:46
 */
@Component
@Service(interfaceClass = GroupService.class)
@Transactional(rollbackFor = Exception.class)
public class GroupServiceImpl implements GroupService {

	@Autowired
	private GroupMapper groupMapper;

	@Autowired
	private GroupDao groupDao;

	@Override
	public Map<String, Object> findGroupPage(GroupDTO dto) {
		PageHelper.startPage(dto.getPage(), dto.getLimit());
		dto.setDeleted(Constants.DEFAULT_NO);
		Page<GroupVO> groups = groupMapper.selectGroupPage(dto);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, groups.getTotal(), "", groups.getResult());
	}

	@Override
	public Map<String, Object> addGroup(Group group) {
		List<Group> lists = groupDao.list(new WherePrams().where("group_code", group.getGroupCode()).and("deleted", 0));
		if (!ObjectUtils.isEmpty(lists)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "Group.CODE.REPETITION", "");
		}
		lists = groupDao.list(new WherePrams().where("group_name", group.getGroupName()).and("deleted",0));
		if (!ObjectUtils.isEmpty(lists)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "Group.NAME.REPETITION", "");
		}
		group.setDeleted(Constants.DEFAULT_NO);
		group.setCreateDate(TimeUtil.getLocalDateTime());
		group.setId(Uuid.getUUID());
		groupDao.add(group);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> findGroup(String id) {
		GroupVO vo = new GroupVO();
		if (!HuatekTools.isEmpty(id)) {
			Group entity = groupDao.get(id);
			BeanUtils.copyProperties(entity, vo);
		}
		WherePrams where = new WherePrams();
		where.where("deleted", Constants.DEFAULT_NO);
		List<Group> datas = groupDao.list(where);
		vo.setDatas(datas);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
	}

	@Override
	public Map<String, Object> updateGroup(Group entity) {
		List<Group> lists = groupDao
				.list(new WherePrams().where("group_code", entity.getGroupCode()).and("id", W.NE, entity.getId()).and("deleted", 0));
		if (!ObjectUtils.isEmpty(lists)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "Group.CODE.REPETITION", "");
		}
		lists = groupDao
				.list(new WherePrams().where("group_name", entity.getGroupName()).and("id", W.NE, entity.getId()).and("deleted", 0));
		if (!ObjectUtils.isEmpty(lists)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "Group.NAME.REPETITION", "");
		}
		groupDao.updateLocal(entity);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

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

}