package com.yh.saas.plugin.configuration.announcement.service.impl;

import static com.yh.saas.plugin.configuration.exception.EnterpriseErrorCodeEnums.ANNOUNCEMENT_NOT_NAME;

import java.util.HashSet;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.common.support.util.query.QueryUtils;
import com.yh.saas.plugin.configuration.announcement.entity.AnnoTypeInfo;
import com.yh.saas.plugin.configuration.announcement.mapper.AnnoTypeInfoMapper;
import com.yh.saas.plugin.configuration.announcement.service.IAnnoTypeInfoService;
import com.yh.saas.plugin.configuration.exception.EnterpriseException;

/**
 * <p>
 * 公告模块 - 公告分类 - 业务实现类
 * </p>
 *
 * @Auther ZhangMingYu
 * @since 2019年8月14日16:00:48
 */
@Service
public class AnnoTypeInfoServiceImpl extends ServiceImpl<AnnoTypeInfoMapper, AnnoTypeInfo>
		implements IAnnoTypeInfoService {

	@Override
	public List<AnnoTypeInfo> queryList(AnnoTypeInfo annoTypeInfo) {
		String compId = AuthSecurityUtils.getCurrentUserInfo().getCompId();

		Wrapper<AnnoTypeInfo> wrapper = new EntityWrapper<>();

		wrapper.eq(AnnoTypeInfo.QueryFiles.COMP_ID, compId);
		if (StringUtils.isEmpty(annoTypeInfo.getOrderColunm())) {
			wrapper.orderBy(AnnoTypeInfo.OrderFiles.SERIAL_NUMBER);
		} else {

			if (StringUtils.isEmpty(annoTypeInfo.getOrder())) {
				wrapper.orderBy(annoTypeInfo.getOrderColunm());
			} else {
				HashSet<String> orderSet = new HashSet<>();
				orderSet.add(annoTypeInfo.getOrderColunm());
				wrapper.orderDesc(orderSet);
			}

		}

		return selectList(wrapper);
	}

	@Override
	@Transactional
	public boolean create(AnnoTypeInfo annoTypeInfo) {
		List<AnnoTypeInfo> annoTypeInfos = selectList(QueryUtils.convertExampleToWrapper(annoTypeInfo));
		if (!CollectionUtils.isEmpty(annoTypeInfos)) {
			throw new EnterpriseException(ANNOUNCEMENT_NOT_NAME);
		}
		annoTypeInfo.setId(IdGenerator.generateUUID());
		return annoTypeInfo.insert();
	}

	@Override
	@Transactional
	public void change(List<AnnoTypeInfo> annoTypeInfos) {
		updateBatchById(annoTypeInfos);
	}

	@Override
	@Transactional
	public void remove(AnnoTypeInfo annoTypeInfo) {
		this.deleteById(annoTypeInfo.getId());
	}

}
