/*
 *      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 org.springblade.core.tool.utils.DigestUtil;
import org.springblade.modules.custom.entity.Manufacturer;
import org.springblade.modules.custom.entity.ManufacturerLabel;
import org.springblade.modules.custom.vo.ManufacturerLabelVO;
import org.springblade.modules.custom.mapper.ManufacturerLabelMapper;
import org.springblade.modules.custom.service.IManufacturerLabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

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

/**
 * 酒厂标签关系表 服务实现类
 *
 * @author BladeX
 * @since 2023-05-18
 */
@Service
public class ManufacturerLabelServiceImpl extends ServiceImpl<ManufacturerLabelMapper, ManufacturerLabel> implements IManufacturerLabelService {

	@Override
	public IPage<ManufacturerLabelVO> selectManufacturerLabelPage(IPage<ManufacturerLabelVO> page, ManufacturerLabelVO manufacturerLabel) {
		return page.setRecords(baseMapper.selectManufacturerLabelPage(page, manufacturerLabel));
	}

	@Override
	public void saveManufacturerLabel(Manufacturer manufacturer) {
		boolean saveFlag = true;
		List<ManufacturerLabel> list = this.lambdaQuery().eq(ManufacturerLabel::getManufacturerId, manufacturer.getId()).list();
		// 保存标签
		if (CollUtil.isNotEmpty(list)) {
			List<Long> collect = list.stream().map(ManufacturerLabel::getLabelId).collect(Collectors.toList());
			String originalMd5 = DigestUtil.md5Hex(CollUtil.join(collect, ","));
			String newMd5 = DigestUtil.md5Hex(manufacturer.getLabelIds());
			// 通过MD5加密结果判断是否需要保存,不一致则说明有更改
			saveFlag = !originalMd5.equals(newMd5);
			if (saveFlag) {
				this.removeByIds(list.stream().map(ManufacturerLabel::getId).collect(Collectors.toList()));
			}
		}
		if (saveFlag) {
			String[] split = manufacturer.getLabelIds().split(",");
			List<ManufacturerLabel> manufacturerLabelList = new ArrayList<>();
			for (String s : split) {
				ManufacturerLabel manufacturerLabel = new ManufacturerLabel();
				manufacturerLabel.setManufacturerId(manufacturer.getId());
				manufacturerLabel.setLabelId(Long.parseLong(s));
				manufacturerLabelList.add(manufacturerLabel);
			}
			this.saveBatch(manufacturerLabelList);
		}
	}

}
