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

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.modules.guosen.productratestrategy.dto.ProductRateDTO;
import org.springblade.modules.guosen.productratestrategy.dto.ProductRateStrategyDTO;
import org.springblade.modules.guosen.productratestrategy.entity.ProductRateStrategy;
import org.springblade.modules.guosen.productratestrategy.vo.ProductRateStrategyVO;
import org.springblade.modules.guosen.productratestrategy.mapper.ProductRateStrategyMapper;
import org.springblade.modules.guosen.productratestrategy.service.IProductRateStrategyService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-10-21
 */
@Service
public class ProductRateStrategyServiceImpl extends BaseServiceImpl<ProductRateStrategyMapper, ProductRateStrategy> implements IProductRateStrategyService {

	@Override
	public IPage<ProductRateStrategyVO> selectProductRateStrategyPage(IPage<ProductRateStrategyVO> page, ProductRateStrategyVO productRateStrategy) {
		return page.setRecords(baseMapper.selectProductRateStrategyPage(page, productRateStrategy));
	}

	@Override
	public R<Boolean> createStrategy(ProductRateStrategyDTO dto) {
		List<ProductRateDTO> rateList = dto.getRateList();
		if(rateList != null && rateList.size() > 0){
			String json = JSONObject.toJSONString(rateList);
			if(dto.getId() == null){ //新增
				LambdaQueryWrapper<ProductRateStrategy> lambda = new LambdaQueryWrapper<>();
				lambda.eq(ProductRateStrategy::getName, dto.getName());
				List<ProductRateStrategy> list = this.list(lambda);
				if(list != null && list.size() > 0){
					return R.fail("已存在相同的产品策略名称");
				}
				ProductRateStrategy strategy = new ProductRateStrategy();
				strategy.setName(dto.getName());
				strategy.setProductRateJson(json);
				strategy.setPaymentScene(dto.getPaymentScene());
				boolean save = this.save(strategy);
				return R.status(save);
			}else{ //更新
				ProductRateStrategy strategy = new ProductRateStrategy();
				strategy.setId(dto.getId());
				strategy.setProductRateJson(json);
				strategy.setPaymentScene(dto.getPaymentScene());
				boolean update = this.updateById(strategy);
				return R.status(update);
			}
		}else{
			return R.status(true);
		}
	}

}
