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

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.UserFeedbackProgressRecordMapper;
import com.cyksj.mapper.UserFeedbackRecordMapper;
import com.cyksj.mapper.manage.cms.CmsUserMapper;
import com.cyksj.model.entity.CmsUser;
import com.cyksj.model.entity.UserFeedbackProgressRecord;
import com.cyksj.model.entity.UserFeedbackRecord;
import com.cyksj.model.views.UserFeedbackRecordView;
import com.cyksj.model.views.UserRecommendGoodsRecordView;
import com.cyksj.model.views.UserRecommendGoodsUserView;
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;

/**
 * admin/投诉与建议
 * 项目名: yhlxj2
 * 文件名: UserFeedbackController
 * 创建者: JavaZou
 * 创建时间:2024/10/11 15:35
 */
@RestController
@RequestMapping("/manage/suggest")
@RequiredArgsConstructor
public class UserFeedbackController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final UserFeedbackRecordMapper userFeedbackRecordMapper;

	private final CmsUserMapper cmsUserMapper;

	private final UserFeedbackProgressRecordMapper userFeedbackProgressRecordMapper;

	/**
	 * 使用建议列表
	 */
	@GetMapping("/feedback/list")
	public Result<SearchResult<UserFeedbackRecordView>> getFeedbackList() {
		SearchResult<UserFeedbackRecordView> search = beanSearcher.search(UserFeedbackRecordView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 标记解决
	 */
	@PutMapping("/feedback/mark/status/{id}")
	public Result<String> markUserFeedbackStatus(@PathVariable Long id) {
		userFeedbackRecordMapper.update(null, Wrappers.lambdaUpdate(UserFeedbackRecord.class)
				.set(UserFeedbackRecord::getStatus, 1)
				.eq(UserFeedbackRecord::getId, id)
				.eq(UserFeedbackRecord::getStatus, 0));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 用户反馈留言进度列表
	 */
	@GetMapping("/feedback/progress/list")
	public Result<SearchResult<UserFeedbackProgressRecord>> getFeedbackProgressList() {
		SearchResult<UserFeedbackProgressRecord> search = beanSearcher.search(UserFeedbackProgressRecord.class, MapUtils.flatBuilder(request.getParameterMap()).orderBy(UserFeedbackProgressRecord::getId).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}


	/**
	 * 留言回复
	 */
	@PostMapping("/feedback/reply")
	public Result<String> replyFeedback(@RequestBody UserFeedbackProgressRecord userFeedbackProgressRecord) {
		long adminId = StpUtil.getLoginIdAsLong();
		CmsUser cmsUser = cmsUserMapper.selectById(adminId);
		userFeedbackProgressRecord.setNickname(cmsUser.getNickname());
		userFeedbackProgressRecordMapper.insert(userFeedbackProgressRecord);

		UserFeedbackRecord userFeedbackRecord = userFeedbackRecordMapper.selectById(userFeedbackProgressRecord.getFeedbackId());
		userFeedbackRecord.setIsNewReply(true);
		userFeedbackRecordMapper.updateById(userFeedbackRecord);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}



	//====================================推荐新品======================================

	/**
	 * 推荐新品列表
	 */
	@GetMapping("/recommend/list")
	public Result<SearchResult<UserRecommendGoodsRecordView>> getRecommendList() {
		SearchResult<UserRecommendGoodsRecordView> search = beanSearcher.search(UserRecommendGoodsRecordView.class, MapUtils.flatBuilder(request.getParameterMap()).orderBy(UserRecommendGoodsRecordView::getUpdateTime).desc().build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}


	/**
	 * 按推荐人数查看列表
	 */
	@GetMapping("/recommend/user/list")
	public Result<SearchResult<UserRecommendGoodsUserView>> getRecommendGoodsUserList() {
		SearchResult<UserRecommendGoodsUserView> search = beanSearcher.search(UserRecommendGoodsUserView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}
}
