/*
 *      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.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.sing.active.entity.ActiveRecord;
import org.springblade.sing.goods.entity.ActiveIdAndGoodsId;
import org.springblade.sing.goods.service.IActiveIdAndGoodsIdService;
import org.springblade.sing.goods.vo.ActiveIdAndGoodsIdVO;
import org.springblade.sing.goods.wrapper.ActiveIdAndGoodsIdWrapper;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 控制器
 *
 * @author BladeX
 * @since 2021-12-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("sing_goods/activeidandgoodsid")
@Api(value = "", tags = "接口")
public class ActiveIdAndGoodsIdController extends BladeController {

	private final IActiveIdAndGoodsIdService activeIdAndGoodsIdService;

//	private final IPointGoodsService pointGoodsService;
//
//	@PostConstruct
//	public void init(){
//		List<PointGoods> list = pointGoodsService.list();
//		for (PointGoods pointGoods : list) {
//			activeIdAndGoodsIdService.saveBatch(Arrays.stream(pointGoods.getActiveId().split(",")).map(ele->new ActiveIdAndGoodsId(){{
//				setGoodsId(pointGoods.getId());
//				setActiveId(Long.valueOf(ele));
//			}}).collect(Collectors.toList()));
//		}
//	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入activeIdAndGoodsId")
	public R<ActiveIdAndGoodsIdVO> detail(ActiveIdAndGoodsId activeIdAndGoodsId) {
		ActiveIdAndGoodsId detail = activeIdAndGoodsIdService.getOne(Condition.getQueryWrapper(activeIdAndGoodsId));
		return R.data(ActiveIdAndGoodsIdWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入activeIdAndGoodsId")
	public R<IPage<ActiveIdAndGoodsIdVO>> list(ActiveIdAndGoodsId activeIdAndGoodsId, Query query, String ids) {
		QueryWrapper<ActiveIdAndGoodsId> queryWrapper = Condition.getQueryWrapper(activeIdAndGoodsId);
		queryWrapper.lambda().in(ObjectUtils.isNotEmpty(activeIdAndGoodsId.getActiveId()) && StringUtils.isNotEmpty(ids),
			ActiveIdAndGoodsId::getGoodsId, Func.toLongList(ids));
		queryWrapper.lambda().in(ObjectUtils.isNotEmpty(activeIdAndGoodsId.getGoodsId()) && StringUtils.isNotEmpty(ids),
			ActiveIdAndGoodsId::getActiveId, Func.toLongList(ids));
		IPage<ActiveIdAndGoodsId> pages = activeIdAndGoodsIdService.page(Condition.getPage(query), queryWrapper);
		return R.data(ActiveIdAndGoodsIdWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入activeIdAndGoodsId")
	public R<IPage<ActiveIdAndGoodsIdVO>> page(ActiveIdAndGoodsIdVO activeIdAndGoodsId, Query query) {
		IPage<ActiveIdAndGoodsIdVO> pages = activeIdAndGoodsIdService.selectActiveIdAndGoodsIdPage(Condition.getPage(query), activeIdAndGoodsId);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入activeIdAndGoodsId")
	public R save(@Valid @RequestBody ActiveIdAndGoodsId activeIdAndGoodsId) {
		return R.status(activeIdAndGoodsIdService.save(activeIdAndGoodsId));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入activeIdAndGoodsId")
	public R update(@Valid @RequestBody ActiveIdAndGoodsId activeIdAndGoodsId) {
		return R.status(activeIdAndGoodsIdService.updateById(activeIdAndGoodsId));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入activeIdAndGoodsId")
	public R submit(@Valid @RequestBody ActiveIdAndGoodsId activeIdAndGoodsId) {
		return R.status(activeIdAndGoodsIdService.saveOrUpdate(activeIdAndGoodsId));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(activeIdAndGoodsIdService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 更新活动与积分商品的关系
	 */
	@PostMapping("/updateActiveAndPointGoods")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "更新活动与积分商品的关系", notes = "传入ids")
	public R updateActiveAndPointGoods(@ApiParam(value = "主键id", required = true) @RequestParam Long id,
									   @ApiParam(value = "主键ids", required = true) @RequestParam String ids,
									   @ApiParam(value = "删除ids", required = true) @RequestParam String delIds,
									   @ApiParam(value = "类型", required = true) @RequestParam String type) {
		List<Long> longs = Func.toLongList(ids);
		List<ActiveIdAndGoodsId> collect = longs.stream().filter(eleId ->
			!Long.valueOf(0L).equals(eleId)).map(eleId -> new ActiveIdAndGoodsId() {{
			if (StringUtils.equals(type, ActiveRecord.class.getSimpleName())) {
				setActiveId(id);
				setGoodsId(eleId);
			} else {
				setGoodsId(id);
				setActiveId(eleId);
			}
		}}).collect(Collectors.toList());
		deletePointGoodsAndActiveId(id, type, Func.toLongList(delIds));
		return R.status(activeIdAndGoodsIdService.saveBatch(collect));
	}

	/**
	 * 删除积分商品的活动关联
	 *
	 * @param id
	 * @param type
	 * @param delIds
	 * @return
	 */
	private boolean deletePointGoodsAndActiveId(Long id, String type, List<Long> delIds) {
		LambdaUpdateWrapper<ActiveIdAndGoodsId> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
		if (StringUtils.equals(type, ActiveRecord.class.getSimpleName())) {
			lambdaUpdateWrapper.eq(ActiveIdAndGoodsId::getActiveId, id).in(ActiveIdAndGoodsId::getGoodsId, delIds);
		} else {
			lambdaUpdateWrapper.eq(ActiveIdAndGoodsId::getGoodsId, id).in(ActiveIdAndGoodsId::getActiveId, delIds);
		}
		return activeIdAndGoodsIdService.remove(lambdaUpdateWrapper);
	}
}
