/*
 *      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 cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.springblade.common.enums.StatusEnum;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.modules.custom.entity.*;
import org.springblade.modules.custom.service.*;
import org.springblade.modules.custom.vo.BartenderVO;
import org.springblade.modules.custom.vo.ManufacturerVO;
import org.springblade.modules.custom.mapper.ManufacturerMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.custom.wrapper.BartenderWrapper;
import org.springblade.modules.custom.wrapper.ManufacturerWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 厂商表 服务实现类
 *
 * @author BladeX
 * @since 2023-03-01
 */
@Service
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer> implements IManufacturerService {

	@Autowired
	private IManufacturerCertService manufacturerCertService;
	@Autowired
	private IBartenderService bartenderService;
	@Autowired
	private IBartenderSkillService bartenderSkillService;
	@Autowired
	private IBaseWineService baseWineService;

	@Override
	public IPage<ManufacturerVO> selectManufacturerPage(IPage<ManufacturerVO> page, ManufacturerVO manufacturer) {
		return page.setRecords(baseMapper.selectManufacturerPage(page, manufacturer));
	}

	@Override
	public ManufacturerVO detail(Long manufacturerId) {
		Manufacturer detail = this.getById(manufacturerId);
		Assert.notNull(detail, "该酒厂信息不存在");
		List<ManufacturerCert> certList = manufacturerCertService.lambdaQuery().eq(ManufacturerCert::getManufacturerId, detail.getId()).list();
		if (CollUtil.isNotEmpty(certList)) {
			detail.setCertList(certList.stream().map(ManufacturerCert::getCertImage).collect(Collectors.toList()));
		}
		ManufacturerVO manufacturerVO = ManufacturerWrapper.build().entityVO(detail);
		// 查询酒厂调酒师
		List<Bartender> bartenderList = bartenderService.lambdaQuery().eq(Bartender::getManufacturerId, manufacturerId).list();
		List<BartenderVO> bartenderVOS = BartenderWrapper.build().listVO(bartenderList);
		if (CollUtil.isNotEmpty(bartenderList)) {
			List<Long> ids = bartenderList.stream().map(Bartender::getId).collect(Collectors.toList());
			// 查询调酒师技能
			List<BartenderSkill> bartenderSkills = bartenderSkillService.lambdaQuery().in(BartenderSkill::getBartenderId, ids).list();
			Map<Long, List<BartenderSkill>> map = bartenderSkills.stream().collect(Collectors.groupingBy(BartenderSkill::getBartenderId));
			bartenderVOS.forEach(e -> e.setSkillList(map.get(e.getId())));
		}
		manufacturerVO.setBartenderList(bartenderVOS);
		// 基酒
		LambdaQueryWrapper<BaseWine> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseWine::getManufacturerId, detail.getId()).eq(BaseWine::getStatus, StatusEnum.ENABLE.getValue());
		Query query = new Query();
		query.setSize(4);
		IPage<BaseWine> page = baseWineService.page(Condition.getPage(query), queryWrapper);
		manufacturerVO.setBaseWineList(page.getRecords());
		return manufacturerVO;
	}

}
