/*
 *    Copyright (c) 2018-2025, hnqz 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 pig4cloud.com 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: hnqz
 */
package com.qunzhixinxi.hnqz.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.entity.ReAlbumDrug;
import com.qunzhixinxi.hnqz.admin.entity.WmWkAlbum;
import com.qunzhixinxi.hnqz.admin.entity.input.WmWkAlbumApiInput;
import com.qunzhixinxi.hnqz.admin.mapper.WmWkAlbumMapper;
import com.qunzhixinxi.hnqz.admin.service.ReAlbumDrugService;
import com.qunzhixinxi.hnqz.admin.service.WmWkAlbumService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 文库-相册
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:24
 */
@Service
@AllArgsConstructor
public class WmWkAlbumServiceImpl extends ServiceImpl<WmWkAlbumMapper, WmWkAlbum> implements WmWkAlbumService {

	private final ReAlbumDrugService reAlbumDrugService;

	private final WmWkAlbumMapper wmWkAlbumMapper;

	@Override
	public Page pageByCondition(Page page, WmWkAlbumApiInput input) {

		return wmWkAlbumMapper.pageByCondition(page, input);
	}

	@Override
	public Page pageByConditionNew(Page page, WmWkAlbumApiInput input) {

		return wmWkAlbumMapper.pageByConditionNew(page, input);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveWmWkAlbum(WmWkAlbum wmWkAlbum) {
		this.baseMapper.insert(wmWkAlbum);
		System.out.println(wmWkAlbum.getId());
		ReAlbumDrug reAlbumDrug;
		List<ReAlbumDrug> reAlbumDrugList = new ArrayList<>();
		if(StringUtils.isNotEmpty(wmWkAlbum.getGlyp())){
			String[] ypIds = wmWkAlbum.getGlyp().split(",");
			for (String ypId : ypIds) {
				reAlbumDrug = new ReAlbumDrug();
				reAlbumDrug.setAlbumId(Integer.parseInt(wmWkAlbum.getId()));
				reAlbumDrug.setDrugId(Integer.parseInt(ypId));
				reAlbumDrugList.add(reAlbumDrug);
			}
		}
		System.err.println(reAlbumDrugList);

		if(CollectionUtils.isNotEmpty(reAlbumDrugList)){
			reAlbumDrugService.saveBatch(reAlbumDrugList);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateWmWkAlbumById(WmWkAlbum wmWkAlbum) {
		this.baseMapper.updateById(wmWkAlbum);
		/**
		 * 先删除当前关联药品 ，再新增
		 */
		reAlbumDrugService.remove(Wrappers.<ReAlbumDrug>lambdaQuery().eq(ReAlbumDrug::getAlbumId,wmWkAlbum.getId()));
		System.out.println(wmWkAlbum.getId());
		ReAlbumDrug reAlbumDrug = new ReAlbumDrug();
		List<ReAlbumDrug> reAlbumDrugList = new ArrayList<>();
		if(StringUtils.isNotEmpty(wmWkAlbum.getGlyp())){
			String[] ypIds = wmWkAlbum.getGlyp().split(",");
			for (String ypId : ypIds) {
				reAlbumDrug = new ReAlbumDrug();
				reAlbumDrug.setAlbumId(Integer.parseInt(wmWkAlbum.getId()));
				reAlbumDrug.setDrugId(Integer.parseInt(ypId));
				reAlbumDrugList.add(reAlbumDrug);
			}
		}
		System.err.println(reAlbumDrugList);

		if(CollectionUtils.isNotEmpty(reAlbumDrugList)){
			reAlbumDrugService.saveBatch(reAlbumDrugList);
		}
	}
}
