package com.tzy.controller.appuser;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tzy.annotation.ApiLog;
import com.tzy.app.dto.SimpleMerchantInfo;
import com.tzy.common.dto.InDto;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.enums.BusinessType;
import com.tzy.common.config.handle.ApiVersion;
import com.tzy.sportcard.api.domain.AppNotifyConfig;
import com.tzy.sportcard.api.domain.AppUserInfoDto;
import com.tzy.sportcard.api.service.IAppNotifyConfigService;
import com.tzy.sportcard.api.service.MerchantApiService;
import com.tzy.sportcard.api.service.MineApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author by po'yi
 * @Classname AppNotifyController
 * @Description
 * @Date 2023/8/8 13:26
 */
@Api(value = "app消息通知")
@RestController
@RequestMapping("/api/{version}/notify")
@Slf4j
public class AppNotifyController {

	@Resource
	private MineApiService mineApiService;
	@Resource
	private IAppNotifyConfigService notifyConfigService;
	@Autowired
	private MerchantApiService merchantApiService;


	@ApiLog(title = "获取个人消息通知配置", businessType = BusinessType.SEARCH)
	@ResponseBody
	@PostMapping(value = "/user/config")
	@ApiVersion(1.0)
	@ApiOperation("获取个人消息通知配置")
	public OutDTO getUserNotifyConfig(@RequestBody InDto inDto) {
		AppUserInfoDto user = mineApiService.checkUserNew(inDto);
		List<AppNotifyConfig> configs = notifyConfigService.selectNotifyConfigByUserId(user.getId());
		return OutDTO.ok().put("notifyConfigs",configs);
	}

	@ApiLog(title = "修改通知配置", businessType = BusinessType.UPDATE)
	@ResponseBody
	@PostMapping(value = "/edit/config")
	@ApiVersion(1.0)
	@ApiOperation("修改通知配置")
	public OutDTO editUserConfig(@RequestBody InDto inDto) {
		AppUserInfoDto user = mineApiService.checkUserNew(inDto);
		AppNotifyConfig notifyConfig = inDto.buildParam(new AppNotifyConfig());
		notifyConfig.setUserId(user.getId().longValue());
		notifyConfigService.updateAppNotifyConfig(notifyConfig);
		notifyConfigService.editUserNotifyType(user.getId());
		return OutDTO.ok();
	}


	@ApiLog(title = "获取关注商家通知配置", businessType = BusinessType.UPDATE)
	@ResponseBody
	@PostMapping(value = "/user/merchant")
	@ApiVersion(1.0)
	@ApiOperation("获取关注商家通知配置")
	public OutDTO getMerchantNotifyConfig(@RequestBody InDto inDto) {
		Integer id = (Integer) inDto.get("id");
		if (id == null) {
			return OutDTO.error500("参数为空!");
		}
		AppUserInfoDto user = mineApiService.checkUserNew(inDto);
		AppNotifyConfig config = notifyConfigService.selectNotifyConfigById(id);
		if (config == null || config.getUserId() != user.getId().longValue()) {
			return OutDTO.error500("数据异常,请重试!");
		}
		List<SimpleMerchantInfo> followMerchants = merchantApiService.selectFollowMerchants(user.getId());
		if (!followMerchants.isEmpty() && "private_merchant".equals(config.getNotifyType())
				&& StringUtils.isNotEmpty(config.getNotifyConfig())) {
			JSONObject configJson = JSON.parseObject(config.getNotifyConfig());
			String merchantIds = configJson.getString("merchantIds");
			if (StringUtils.isNotEmpty(merchantIds)) {
				List<String> merchantIdList = Arrays.asList(merchantIds.split(","));
				followMerchants.forEach(m -> m.setNotifyFlag(merchantIdList.contains(m.getId().toString())));
			}
		}
		return OutDTO.ok().put("followMerchants", followMerchants);
	}


	@ApiLog(title = "消息列表", businessType = BusinessType.SEARCH)
	@ResponseBody
	@PostMapping(value = "/msg/list")
	@ApiVersion(1.0)
	@ApiOperation("消息列表")
	public OutDTO getUserNotifyMsgList(@RequestBody InDto inDto) {
		AppUserInfoDto user = mineApiService.checkUserNew(inDto);
		List<AppNotifyConfig> configs = notifyConfigService.getUserNotifyMsgList(user.getId());
		return OutDTO.ok().put("notifyConfigs",configs);
	}
}
