/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springblade.common.enums.StatusEnum;
import org.springblade.common.enums.YesOrNoEnum;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.modules.custom.entity.Bartender;
import org.springblade.modules.custom.entity.BartenderSkill;
import org.springblade.modules.custom.entity.BaseWine;
import org.springblade.modules.custom.mapper.BartenderSkillMapper;
import org.springblade.modules.custom.vo.BartenderVO;
import org.springblade.modules.custom.mapper.BartenderMapper;
import org.springblade.modules.custom.service.IBartenderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 调酒师表 服务实现类
 *
 * @author BladeX
 * @since 2023-02-08
 */
@Service
public class BartenderServiceImpl extends ServiceImpl<BartenderMapper, Bartender> implements IBartenderService {


	@Resource
	private BartenderSkillMapper bartenderSkillMapper;

	@Override
	public IPage<BartenderVO> selectBartenderPage(IPage<BartenderVO> page, BartenderVO bartender) {
		return page.setRecords(baseMapper.selectBartenderPage(page, bartender));
	}

	@Override
	public IPage<BartenderVO> getBartenderList(Query query) {
		LambdaQueryWrapper<Bartender> wrapper = Wrappers.lambdaQuery(Bartender.class)
				.eq(Bartender::getStatus, StatusEnum.ENABLE.getValue())
				.eq(Bartender::getIsDelete, YesOrNoEnum.NO.getValue())
				.orderByAsc(Bartender::getSort);
		IPage<Bartender> page = this.page(Condition.getPage(query), wrapper);

		List<Bartender> bartenders = page.getRecords();

		List<BartenderVO> bartenderVOS = new ArrayList<>();

		for (Bartender bartender : bartenders) {
			BartenderVO bartenderVO = new BartenderVO();
			BeanUtils.copyProperties(bartender,bartenderVO);
			LambdaQueryWrapper<BartenderSkill> queryWrapper = Wrappers.lambdaQuery(BartenderSkill.class)
					.eq(BartenderSkill::getIsDelete, YesOrNoEnum.NO.getValue())
					.orderByAsc(BartenderSkill::getSort);
			List<BartenderSkill> bartenderSkills = bartenderSkillMapper.selectList(queryWrapper);
			bartenderVO.setSkillList(bartenderSkills);
			bartenderVOS.add(bartenderVO);
		}
		IPage<BartenderVO> pageVO = new Page<>();
		pageVO.setPages(page.getPages());
		pageVO.setSize(page.getSize());
		pageVO.setCurrent(page.getCurrent());
		pageVO.setTotal(page.getTotal());
		pageVO.setRecords(bartenderVOS);
		return pageVO;
	}

}
