/*
 *      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.guosen.present.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.guosen.goods.entity.Goods;
import org.springblade.modules.guosen.goods.service.IGoodsService;
import org.springblade.modules.guosen.goodsRecord.service.IGoodsRecordService;
import org.springblade.modules.guosen.medal.entity.Medal;
import org.springblade.modules.guosen.medal.service.IMedalService;
import org.springblade.modules.guosen.present.dto.PresentDTO;
import org.springblade.modules.guosen.present.entity.Present;
import org.springblade.modules.guosen.present.mapper.PresentMapper;
import org.springblade.modules.guosen.present.service.IPresentService;
import org.springblade.modules.guosen.present.vo.PresentVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-06-11
 */
@Service
public class PresentServiceImpl extends BaseServiceImpl<PresentMapper, Present> implements IPresentService {

	@Resource
	private IGoodsService goodsService;

	@Override
	public IPage<PresentVO> selectPresentPage(IPage<PresentVO> page, PresentVO present) {
		return page.setRecords(baseMapper.selectPresentPage(page, present));
	}

	@Override
	@Transactional
	public Boolean submit(PresentVO presentVO) {
		Long donateMedalId = presentVO.getDonateMedalId();
		String goodsName = presentVO.getGoodsName();
		String goodsPic = presentVO.getGoodsPic();
		Integer goodsPostAmount = presentVO.getGoodsPostAmount();
		if (presentVO.getId() == null && presentVO.getIsSendGoods() == 1) {
			//新增礼物且赠送实物
			//赠送实物
			Goods goods = new Goods();
			goods.setDonateMedalId(donateMedalId);
			goods.setGoodsName(goodsName);
			goods.setGoodsPic(goodsPic);
			goods.setGoodsPostAmount(goodsPostAmount);
			goodsService.save(goods);
			presentVO.setGoodsId(goods.getId());
		}

		if(presentVO.getId() != null){
			//修改礼物
			Present present = this.getById(presentVO.getId());
			Long goodsId = present.getGoodsId();
			Goods goods = goodsService.getById(goodsId);

			if (presentVO.getIsSendGoods() == 1) {
				if (!goodsName.equals(goods.getGoodsName()) || !donateMedalId.equals(goods.getDonateMedalId())
					|| !goodsPic.equals(goods.getGoodsPic()) || !goodsPostAmount.equals(goods.getGoodsPostAmount()))
				{
					//修改过了实物的属性，重新更换实物
					Goods goods1 = new Goods();
					goods1.setDonateMedalId(donateMedalId);
					goods1.setGoodsName(goodsName);
					goods1.setGoodsPic(goodsPic);
					goods1.setGoodsPostAmount(goodsPostAmount);
					goodsService.save(goods1);
					presentVO.setGoodsId(goods1.getId());
				}
			}


		}
		return this.saveOrUpdate(presentVO);
	}

}
