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

import cn.hutool.core.util.StrUtil;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.views.CorpGroupChatDetailView;
import com.cyksj.model.views.CorpGroupChatView;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/*
 *项目名: netflix
 *文件名: CorpChatController
 *创建者: JavaZou
 *创建时间:2023/6/26 16:24
 */
@RestController
@RequestMapping("/manage/corp/group/chat")
@RequiredArgsConstructor
public class CorpChatController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	/**
	 * 客户群列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<CorpGroupChatView>> getGroupsChatList() {
		SearchResult<CorpGroupChatView> search = beanSearcher.search(CorpGroupChatView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 客户群详情
	 */
	@GetMapping("/get/detail/{id}")
	public Result<SearchResult<CorpGroupChatDetailView>> getGroupsDetail(@PathVariable Long id, Long goodsId, Boolean exists) {
		String condition = StrUtil.EMPTY;
		if (goodsId != null) {
			String connect = (exists == null || exists) ? "in" : "not in";
			condition += String.format("user_id %s (select gr.user_id from (select gt.id from groups_trips gt inner join goods_don_sku sku on sku.id = gt.sku_id and goods_id = %s) s inner join groups_relation gr on gr.groups_id = s.id)", connect, goodsId);
		}
		MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap())
				.field(CorpGroupChatDetailView::getGroupChatId, id);
		if (StrUtil.isNotBlank(condition)) {
			builder.put("condition", condition);
		}
		SearchResult<CorpGroupChatDetailView> search = beanSearcher.search(CorpGroupChatDetailView.class, builder.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}
}




