package com.cyksj.web.controller.sys;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.UserSysMsgNotifyMapper;
import com.cyksj.model.entity.UserSysMsgNotify;
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;

/**
 * 项目名: yhlxj2
 * 文件名: UserSysMsgController
 * 创建者: JavaZou
 * 创建时间:2024/2/4 16:58
 */
@RequestMapping("/sys/msg")
@RestController
@RequiredArgsConstructor
public class UserSysMsgController {

	private final BeanSearcher beanSearcher;

	private final UserBindRelationService userBindRelationService;

	private final UserSysMsgNotifyMapper userSysMsgNotifyMapper;

	private final HttpServletRequest request;


	/**
	 * 用户系统消息
	 */
	@GetMapping("/get")
	public Result<SearchResult<UserSysMsgNotify>> getSysMsg() {
		long userId = StpUserUtil.getLoginIdAsLong();
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		SearchResult<UserSysMsgNotify> search = beanSearcher.search(UserSysMsgNotify.class, MapUtils.flatBuilder(request.getParameterMap()).field(UserSysMsgNotify::getUserId, userIdList).op(Operator.InList).field(UserSysMsgNotify::getDeleted, 1).orderBy(UserSysMsgNotify::getIsView).orderBy(UserSysMsgNotify::getCreatedTime).desc().onlySelect(UserSysMsgNotify::getId, UserSysMsgNotify::getAccount, UserSysMsgNotify::getRelationId, UserSysMsgNotify::getMsg, UserSysMsgNotify::getIsView).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 查看消息
	 */
	@GetMapping("/view/detail/{id}")
	public Result viewMsgDetail(@PathVariable Long id) {
		long userId = StpUserUtil.getLoginIdAsLong();
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		Optional.ofNullable(userSysMsgNotifyMapper.selectOne(Wrappers.lambdaQuery(UserSysMsgNotify.class).eq(UserSysMsgNotify::getId, id).in(UserSysMsgNotify::getUserId, userIdList).eq(UserSysMsgNotify::getDeleted, 1))).ifPresent(e -> {
			if (!e.getIsView()) {
				e.setIsView(false);
				userSysMsgNotifyMapper.updateById(e);
			}
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除消息
	 */
	@DeleteMapping("/del/{id}")
	public Result delMsg(@PathVariable Long id) {
		long userId = StpUserUtil.getLoginIdAsLong();
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		Optional.ofNullable(userSysMsgNotifyMapper.selectOne(Wrappers.lambdaQuery(UserSysMsgNotify.class).eq(UserSysMsgNotify::getId, id).in(UserSysMsgNotify::getUserId, userIdList).eq(UserSysMsgNotify::getDeleted, 1))).ifPresent(e -> {
			e.setDeleted(false);
			userSysMsgNotifyMapper.updateById(e);
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
