/*
 *      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.ManufacturerCert;
import org.springblade.modules.custom.vo.ManufacturerCertVO;
import org.springblade.modules.custom.mapper.ManufacturerCertMapper;
import org.springblade.modules.custom.service.IManufacturerCertService;
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 ManufacturerCertServiceImpl extends ServiceImpl<ManufacturerCertMapper, ManufacturerCert> implements IManufacturerCertService {

	@Override
	public IPage<ManufacturerCertVO> selectManufacturerCertPage(IPage<ManufacturerCertVO> page, ManufacturerCertVO manufacturerCert) {
		return page.setRecords(baseMapper.selectManufacturerCertPage(page, manufacturerCert));
	}

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

}
