package digital.laboratory.platform.entrustment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.entrustment.entity.Taker;
import digital.laboratory.platform.entrustment.mapper.TakerMapper;
import digital.laboratory.platform.entrustment.service.TakerService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 采集员服务实现类
 *
 * @author Zhang Xiaolong created at  2022-08-25
 * @describe 采集员 服务实现类
 */
@Service
public class TakerServiceImpl extends ServiceImpl<TakerMapper, Taker> implements TakerService {
	/**
	 * 更新采集员
	 * 以采集员的名字为关键字, 如果同名采集员存在, 更新其他属性; 如果同名采集员不存在, 则新增数据库记录
	 * @param taker
	 */
	@Override
	public boolean renew(Taker taker) {
		if (StrUtil.isBlank(taker.getName())) {
			return false;
		}
		List<Taker> list = baseMapper.selectList(Wrappers.<Taker>query()
				.eq("owner_user_id", taker.getOwnerUserId())
				.eq("name", taker.getName())
		);

		if (list.size() > 0) {
			//== 存在同名送检员, 更新之
			taker.setId(list.get(0).getId());
			baseMapper.update(taker, Wrappers.<Taker>query()
					.eq("owner_user_id", taker.getOwnerUserId())
					.eq("name", taker.getName())
			);
		}
		else {
			taker.setId(IdWorker.get32UUID().toUpperCase());
			baseMapper.insert(taker);
		}
		return true;
	}


	@Override
	public Taker getByName(String name, String ownerUserId) {
		Taker taker = baseMapper.selectOne(Wrappers.<Taker>query()
				.eq("owner_user_id", ownerUserId)
				.eq("name", name)
		);

		return taker;
	}
}
