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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.corp.kf.CorpKfServicerSessionRecordMapper;
import com.cyksj.mapper.corp.kf.CorpKfUserMapper;
import com.cyksj.model.entity.CorpKfServicerSessionRecord;
import com.cyksj.model.entity.CorpKfUser;
import com.cyksj.model.kf.CorpKfUserInfo;
import com.cyksj.service.corp.WxCorpOps;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目名: yhlxj2
 * 文件名: CorpKfMsgController
 * 创建者: JavaZou
 * 创建时间:2025/1/2 13:59
 */
@RestController
@RequestMapping("/manage/corp/kf")
@RequiredArgsConstructor
public class CorpKfMsgController {
	private final WxCorpOps wxCorpOps;

	private final CorpKfServicerSessionRecordMapper corpKfServicerSessionRecordMapper;

	private final CorpKfUserMapper corpKfUserMapper;

	/**
	 * 发送满意度调查消息
	 */
	@PostMapping("/send/satisfaction/msg")
	public void sendSatisfactionMsg(Long id) throws Exception {
		CorpKfServicerSessionRecord corpKfServicerSessionRecord = corpKfServicerSessionRecordMapper.selectById(id);
		//更新发送满意度字段
		if (corpKfServicerSessionRecord != null) {
			corpKfServicerSessionRecord.setSendSatisfaction(true);
			corpKfServicerSessionRecordMapper.updateById(corpKfServicerSessionRecord);
			wxCorpOps.sendSatisfactionMsg(corpKfServicerSessionRecord.getMsgid(), corpKfServicerSessionRecord.getOpenKfId(), corpKfServicerSessionRecord.getExternalUserid(), null);
		}
	}

	/**
	 * 获取客户用户信息
	 */
	@GetMapping("/get/user/info")
	public Result<CorpKfUserInfo> getUserInfo(String externalUserid) throws Exception {
		CorpKfUserInfo corpKfUserInfo = wxCorpOps.getCorpKfUserInfo(externalUserid);
		CorpKfUser corpKfUser = corpKfUserMapper.selectOne(Wrappers.lambdaQuery(CorpKfUser.class)
				.eq(CorpKfUser::getExternalUserid, externalUserid)
				.last("limit 1"));
		if (corpKfUser == null) {
			try {
				corpKfUser = new CorpKfUser();
				BeanUtil.copyProperties(corpKfUserInfo, corpKfUser);
				corpKfUserMapper.insert(corpKfUser);
			} catch (Exception e) {
			}
		} else {
			corpKfUser.setAvatar(corpKfUserInfo.getAvatar());
			corpKfUser.setNickname(corpKfUserInfo.getNickname());
			corpKfUser.setGender(corpKfUserInfo.getGender());
			corpKfUserMapper.updateById(corpKfUser);
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpKfUserInfo);
	}

	/**
	 * 查询用户会话
	 */
	@GetMapping("/get/user/session")
	public Result<CorpKfServicerSessionRecord> getUserSession(String userid, String externalUserid) {
		CorpKfServicerSessionRecord corpKfServicerSessionRecord = corpKfServicerSessionRecordMapper.selectOne(Wrappers.lambdaQuery(CorpKfServicerSessionRecord.class)
				.eq(CorpKfServicerSessionRecord::getServicerUserid, userid)
				.eq(CorpKfServicerSessionRecord::getExternalUserid, externalUserid)
				.eq(CorpKfServicerSessionRecord::getServiceState, 0)
				.last("limit 1"));
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpKfServicerSessionRecord);
	}
}
