package com.cyksj.web.controller.suggest;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.UserFeedbackRecordMapper;
import com.cyksj.mapper.UserMapper;
import com.cyksj.model.entity.UserFeedbackProgressRecord;
import com.cyksj.model.entity.UserFeedbackRecord;
import com.cyksj.model.entity.UserRecommendGoodsRecord;
import com.cyksj.model.views.UserFeedbackProgressRecordFrontView;
import com.cyksj.service.suggest.UserSuggestService;
import com.cyksj.service.user.UserBindRelationService;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.param.Operator;
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;

/**
 * server/投诉建议
 * 项目名: yhlxj2
 * 文件名: UserSuggestController
 * 创建者: JavaZou
 * 创建时间:2024/10/11 16:05
 */
@RestController
@RequestMapping("/applets/user/suggest")
@RequiredArgsConstructor
public class UserSuggestController {


	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final UserMapper userMapper;

	private final UserFeedbackRecordMapper userFeedbackRecordMapper;

	private final UserSuggestService userSuggestService;

	private final UserBindRelationService userBindRelationService;


	/**
	 * 平台使用建议
	 */
	@PostMapping("/goods/usage")
	public Result<String> suggestGoodsUsage(@RequestBody UserFeedbackProgressRecord progressRecord) {
		long userId = StpUserUtil.getLoginIdAsLong();
		progressRecord.setUserId(userId);
		userSuggestService.suggestGoodsUsage(progressRecord);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 是否有最新留言
	 */
	@GetMapping("/goods/usage/isReply")
	public Result<Boolean> isSuggestReply() {
		long userId = StpUserUtil.getLoginIdAsLong();
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		Integer selectCount = userFeedbackRecordMapper.selectCount(Wrappers.lambdaQuery(UserFeedbackRecord.class)
				.in(UserFeedbackRecord::getUserId, userIdList)
				.eq(UserFeedbackRecord::getIsNewReply, 1));
		return GatewayResponse.SUCCESS.newBuilder().toResult(selectCount > 0);
	}

	/**
	 * 用户建议反馈列表
	 */
	@GetMapping("/goods/usage/feedback/list")
	public Result<SearchResult<UserFeedbackRecord>> getFeedbackList() {
		long userId = StpUserUtil.getLoginIdAsLong();
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		SearchResult<UserFeedbackRecord> search = beanSearcher.search(UserFeedbackRecord.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(UserFeedbackRecord::getUserId, userIdList).op(Operator.InList)
				.orderBy(UserFeedbackRecord::getUpdateTime).desc()
				.onlySelect(UserFeedbackRecord::getId, UserFeedbackRecord::getGoodsId, UserFeedbackRecord::getIsNewReply, UserFeedbackRecord::getDescription, UserFeedbackRecord::getGoodsTitle, UserFeedbackRecord::getCreatedTime, UserFeedbackRecord::getUpdateTime)
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 用户建议反馈留言
	 */
	@PostMapping("/feedback/msg")
	public Result<String> userFeedbackMsg(@RequestBody UserFeedbackProgressRecord progressRecord) {
		long userId = StpUserUtil.getLoginIdAsLong();
		progressRecord.setUserId(userId);
		userSuggestService.userFeedbackMsg(progressRecord);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 用户查看最新反馈内容
	 */
	@GetMapping("/goods/usage/feedback/content/list")
	public Result<List<UserFeedbackProgressRecordFrontView>> getFeedbackContentList(Long feedbackId) {
		long userId = StpUserUtil.getLoginIdAsLong();
		Assert.notNull(feedbackId, "查看错误");
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		userIdList.add(0l);
		List<UserFeedbackProgressRecordFrontView> search = beanSearcher.searchAll(UserFeedbackProgressRecordFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(UserFeedbackProgressRecordFrontView::getFeedbackId, feedbackId)
				.field(UserFeedbackProgressRecordFrontView::getUserId, userIdList).op(Operator.InList)
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 记录用户推荐上架新品
	 */
	@PostMapping("/recommend/goods")
	public Result<String> recommendGoods(@RequestBody UserRecommendGoodsRecord userRecommendGoodsRecord) {
		long userId = StpUserUtil.getLoginIdAsLong();
		userRecommendGoodsRecord.setUserId(userId);
		userSuggestService.recommendGoods(userRecommendGoodsRecord);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 标记留意已读
	 */
	@PutMapping("/update/status/{feedBackId}")
	public Result<String> updateFeedBackStatus(@PathVariable Long feedBackId) {
		long userId = StpUserUtil.getLoginIdAsLong();
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		Optional.ofNullable(userFeedbackRecordMapper.selectById(feedBackId)).ifPresent(fe->{
			if (userIdList.contains(fe.getUserId())) {
				fe.setIsNewReply(false);
				userFeedbackRecordMapper.updateById(fe);
			}
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}

