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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.sing.active.entity.ActiveRecord;
import org.springblade.sing.active.mapper.ActiveRecordMapper;
import org.springblade.sing.active.service.IActiveRecordService;
import org.springblade.sing.goods.entity.ActiveIdAndGoodsId;
import org.springblade.sing.goods.entity.PointGoods;
import org.springblade.sing.goods.service.IActiveIdAndGoodsIdService;
import org.springblade.sing.goods.vo.PointGoodsVO;
import org.springblade.sing.goods.mapper.PointGoodsMapper;
import org.springblade.sing.goods.service.IPointGoodsService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.support.Query;
import org.springframework.util.Assert;

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

/**
 * 积分商城 服务实现类
 *
 * @author BladeX
 * @since 2021-11-09
 */
@Service
public class PointGoodsServiceImpl extends BaseServiceImpl<PointGoodsMapper, PointGoods> implements IPointGoodsService {

	@Autowired
	private IActiveRecordService activeRecordService;
	@Autowired
	private IActiveIdAndGoodsIdService activeIdAndGoodsIdService;

	@Override
	public IPage<PointGoodsVO> selectPointGoodsPage(IPage<PointGoodsVO> page, PointGoodsVO pointGoods) {
		return page.setRecords(baseMapper.selectPointGoodsPage(page, pointGoods));
	}

    @Override
    public IPage<PointGoods> queryList(Long activeId,Query query,Integer searchType) {
		IPage<PointGoods> pointGoodsIPage = new Page<>(query.getCurrent(),query.getSize());
		LambdaQueryWrapper<PointGoods> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PointGoods::getStatus,1);
		if (!StringUtil.isEmpty(activeId)){
			List<Long> ids = new ArrayList<>();
			//查表
			List<ActiveIdAndGoodsId> activeIdAndGoodsIds = activeIdAndGoodsIdService.list(new LambdaQueryWrapper<ActiveIdAndGoodsId>()
				.eq(ActiveIdAndGoodsId::getActiveId, activeId));
			//获取商品id
			for (ActiveIdAndGoodsId activeIdAndGoodsId : activeIdAndGoodsIds) {
				ids.add(activeIdAndGoodsId.getGoodsId());
			}
			Assert.notEmpty(ids,"此活动不存在");
			ActiveRecord activeRecord = activeRecordService.getById(activeId);
			Assert.notNull(activeRecord,"此活动不存在");

			wrapper.in(PointGoods::getId,ids);
			List<PointGoods> goods = baseMapper.selectList(wrapper);
			Assert.notEmpty(goods,"此活动没有相关商品,请前往主页积分商城兑换");
		}
		if (searchType != null){
			switch (searchType){
				case 1:{
					//销量降序
					wrapper.last("order by total-remain DESC");
					break;
				}
				case 2:{
					//价格降序
					wrapper.orderByDesc(PointGoods::getPoint);
					break;
				}
				case 3:{
					//价格升序
					wrapper.orderByAsc(PointGoods::getPoint);
					break;
				}
//				case 4:{
//					wrapper.last("order by total-remain");
//					break;
//				}
				default:wrapper.orderByAsc(PointGoods::getOrder);
			}
		}
		return super.page(pointGoodsIPage,wrapper);
    }

    @Override
    public boolean shelvesByIds(List<String> ids) {
		List<PointGoods> list = baseMapper.selectBatchIds(ids);
		for (PointGoods pointGoods : list) {
			pointGoods.setStatus(1);
		}
		return super.updateBatchById(list);
	}

	@Override
	public boolean unShelvesByIds(List<String> ids) {
		List<PointGoods> list = baseMapper.selectBatchIds(ids);
		for (PointGoods pointGoods : list) {
			pointGoods.setStatus(0);
		}
		return super.updateBatchById(list);
	}
}
