package com.cyksj.web.controller.manage.distribute;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.GoodsDonSkuMapper;
import com.cyksj.model.entity.DistributePosterGoods;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.views.DistributePosterGoodsView;
import com.cyksj.service.distribute.DistributePosterGoodsService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 项目名: yhlxj
 * 文件名: DistributePosterController
 * 创建者: JavaZou
 * 创建时间:2024/6/24 15:38
 */

@RestController
@RequestMapping("/manage/distribute/poster")
@RequiredArgsConstructor
public class DistributePosterController {
	private final DistributePosterGoodsService distributePosterGoodsService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final GoodsDonSkuMapper skuMapper;

	/**
	 * 新增海报
	 */
	@PostMapping("/post")
	public Result<String> post(@RequestBody DistributePosterGoods distributePosterGoods) throws Exception {
		distributePosterGoodsService.saveOrUpdatePo(distributePosterGoods);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除海报
	 */
	@DeleteMapping("/del/{id}")
	public Result<String> del(@PathVariable Long id) {
		distributePosterGoodsService.removeById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑海报
	 */
	@PutMapping("/update")
	public Result<String> update(@RequestBody DistributePosterGoods distributePosterGoods) throws Exception {
		distributePosterGoodsService.saveOrUpdatePo(distributePosterGoods);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 海报列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<DistributePosterGoodsView>> get() {
		SearchResult<DistributePosterGoodsView> search = beanSearcher.search(DistributePosterGoodsView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		search.getDataList().forEach(e -> {
			if (StrUtil.isNotBlank(e.getSkuIds())) {
				try {
					String specVal = skuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
									.in(GoodsDonSku::getId, Jsons.parseSet(e.getSkuIds(), Long.class)))
							.stream().map(GoodsDonSku::getSpecVal).collect(Collectors.joining(","));
					e.setSpecVal(specVal);
				} catch (Exception ex) {
				}
			}
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 海报上下架
	 */
	@PutMapping("/update/status/{id}")
	public Result<String> updateStatus(@PathVariable Long id) {
		Optional.ofNullable(distributePosterGoodsService.getById(id))
				.ifPresent(e -> {
					e.setStatus(!e.getStatus());
					distributePosterGoodsService.updateById(e);
				});
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 关联平台
	 */
	@GetMapping("/get/relate/goods")
	public Result<List<DistributePosterGoodsView>> getRelateGoods() {
		List<DistributePosterGoodsView> distributePosterGoodsViews = beanSearcher.searchAll(DistributePosterGoodsView.class, MapUtils.flatBuilder(request.getParameterMap())
				.onlySelect(DistributePosterGoodsView::getGoodsId, DistributePosterGoodsView::getTitle)
				.orderBy(DistributePosterGoodsView::getGoodsId).asc()
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(distributePosterGoodsViews);
	}
}
