package com.template.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.seewo.open.sdk.DefaultSeewoClient;
import com.seewo.open.sdk.SeewoClient;
import com.seewo.open.sdk.auth.Account;
import com.template.api.SmartVisitorControllerAPI;
import com.template.common.utils.CommonUtil;
import com.template.common.utils.RequestUtils;
import com.template.common.utils.TimeExchange;
import com.template.common.utils.paramUtils;
import com.template.config.ParkConfig;
import com.template.config.SeewoConfig;
import com.template.model.enumModel.eApproveStatu;
import com.template.model.enumModel.eExamineStatu;
import com.template.model.enumModel.eVisitorType;
import com.template.model.pojo.*;
import com.template.model.request.otherAppointmentRequest;
import com.template.model.request.parentsAppointmentRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.seewo.HomeSchoolServiceSendNoteToKidParam;
import com.template.model.seewo.HomeSchoolServiceSendNoteToKidRequest;
import com.template.model.seewo.HomeSchoolServiceSendNoteToKidResult;
import com.template.model.vo.AffiliateUserVo;
import com.template.model.vo.VisitorPageVo;
import com.template.services.SmartUserService;
import com.template.services.SmartVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;
/**
*
* 前端控制器
*
*
* @author ceshi
* @since 2023-12-04
*/
@RestController
public class SmartVisitorController implements SmartVisitorControllerAPI {
@Autowired
private SmartVisitorService smartVisitorService;
@Autowired
private SmartUserService smartUserService;
@Resource
private SeewoConfig seewoConfig;
@Resource
private ParkConfig parkConfig;
/**
* 新增访客预约
*
* @param smartApply 访客预约数据
* @param bindingResult
* @return
*/
@Override
public CommonResult insertSmartVisitor(SmartVisitor smartApply, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
int result = smartVisitorService.insertSmartVisitor(smartApply);
return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
}
/**
* 更新访客预约
*
* @param sa 访客预约数据
* @param bindingResult
* @return
*/
@Override
public CommonResult updateSmartVisitorById(SmartVisitor sa, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
int result = smartVisitorService.updateSmartVisitor(sa);
return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
}
/**
* 访客预约分页数据查询
*
* @param currentPage 当前页数
* @param pageCount 一页数据条数
* @param name 查询名称
* @return
*/
@Override
public CommonResult queryPageSmartVisitor(int currentPage, int pageCount, String name) {
PageUtils result = smartVisitorService.queryPageSmartVisitors(currentPage, pageCount, name);
return CommonResult.ok(result);
}
@Override
public CommonResult deleteSmartVisitorById(int id) {
SmartVisitor data = smartVisitorService.getSmartById(id);
if (data == null) {
return CommonResult.fail("当前数据不存在,删除失败!");
}
int result = smartVisitorService.deleteSmartVisitorById(id);
return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
}
@Override
public CommonResult parentsAppointment(parentsAppointmentRequest par, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
//参数判断 判断受访学生信息是否为空
if (par.getStudents() == null) {
return CommonResult.fail("请选择受访学生");
}
if (par.getStudents().size() <= 0) {
return CommonResult.fail("请选择受访学生");
}
//根据用户ID查询相关信息
SmartUser su = smartUserService.getSmartById(par.getUserId());
if (su == null) {
return CommonResult.fail("当前用户信息不合法,无法进行预约!");
}
String startTime = par.getVisitorTime();
String endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd HH:mm:ss"));
//要进行家长数据重复判断
//那是不是第二次预约的时间不能在那个可访问的时间段内
//例如:比如,第一次约了7:00-10:00;那第二次:6:00-7:30 或 8:11-13:00都不行
Integer count = smartVisitorService.queryVisitorCount(su.getIdCard(), startTime, endTime);
if(count <= 0){
return CommonResult.fail("该时间段已预约,请勿重复预约!");
}
List svs = new ArrayList<>();
for (AffiliateUserVo student : par.getStudents()) {
SmartVisitor sv = new SmartVisitor();
sv.setUserId(par.getUserId());
sv.setUserName(su.getName());
sv.setUserPhone(su.getPhone());
sv.setUserNumber(su.getIdCard());
sv.setPeerNum(par.getPeerNum());
//sv.setCarNum(par.getCarNum());家长预约不能填写车牌号
sv.setVisitReason(par.getVisitReason());
sv.setStatu(eApproveStatu.Audit.getValue());
sv.setVisitorTime(TimeExchange.StringToDate(par.getVisitorTime(), "yyyy-MM-dd HH:mm:ss"));
sv.setVisitorDeadline(TimeExchange.StringToDate(TimeExchange.TimeDesH(sv.getVisitorTime(), 4), "yyyy-MM-dd HH:mm:ss"));
sv.setRespondent(student.getId());
sv.setResponcode(student.getCardNo());
sv.setRespondentName(student.getName());
//将第一个受访学生的部门ID带过去
sv.setDepartmentId(student.getDepartmentId());
sv.setVisitorType(eVisitorType.Parent.getValue());
svs.add(sv);
}
boolean result = smartVisitorService.insertVisitorBatch(svs);
return result ? CommonResult.ok("预约成功,等待审批通过") : CommonResult.fail("预约失败");
}
@Override
public CommonResult otherAppointment(otherAppointmentRequest oar, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
//根据用户ID查询相关信息
SmartUser su = smartUserService.getSmartById(oar.getUserId());
if (su == null) {
return CommonResult.fail("当前用户信息不合法,无法进行预约!");
}
String startTime = oar.getVisitorTime();
String endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd HH:mm:ss"));
//要进行访客数据重复判断
//那是不是第二次预约的时间不能在那个可访问的时间段内
//例如:比如,第一次约了7:00-10:00;那第二次:6:00-7:30 或 8:11-13:00都不行
Integer count = smartVisitorService.queryVisitorCount(su.getIdCard(), startTime, endTime);
if(count <= 0){
return CommonResult.fail("该时间段已预约,请勿重复预约!");
}
SmartVisitor sv = new SmartVisitor();
sv.setUserId(oar.getUserId());
sv.setUserName(su.getName());
sv.setUserPhone(su.getPhone());
sv.setUserNumber(su.getIdCard());
sv.setPeerNum(oar.getPeerNum());
sv.setCarNum(oar.getCarNum());
sv.setVisitReason(oar.getVisitReason());
sv.setStatu(eApproveStatu.Audit.getValue());
sv.setVisitorTime(TimeExchange.StringToDate(startTime, "yyyy-MM-dd HH:mm:ss"));
sv.setVisitorDeadline(TimeExchange.StringToDate(endTime, "yyyy-MM-dd HH:mm:ss"));
sv.setRespondentName(oar.getRespondentName());
sv.setRespondentPhone(oar.getRespondentPhone());
sv.setVisitorType(eVisitorType.Other.getValue());
int result = smartVisitorService.insertSmartVisitor(sv);
return result > 0 ? CommonResult.ok("预约成功,等待审批通过") : CommonResult.ok("预约失败");
}
@Override
public CommonResult appointmentPageRecord(int currentPage, int pageCount, Integer userId, int type) {
PageUtils result = smartVisitorService.smartVisitorPageByUserId(currentPage, pageCount, userId, type);
return CommonResult.ok(result);
}
/**
* 访客记录审核
*
* @param id 数据ID
* @param type 操作类型:
* 同意,并推送:1
* 拒绝:2
* @return
*/
@Override
@Transactional(rollbackFor = {Exception.class})
public CommonResult examineRecord(int id, int type) throws Exception {
SmartVisitor visitor = smartVisitorService.getSmartById(id);
if (visitor == null) {
return CommonResult.fail("访客记录已失效,审核失败!");
}
try {
if (type == eExamineStatu.Agree.getValue()) {
visitor.setStatu(eApproveStatu.Pushed.getValue());
//家长访客预约才需要将消息推送到希沃电子班牌
if (visitor.getVisitorType().intValue() == eVisitorType.Parent.getValue()) {
String content = visitor.getRespondentName() + "你的家长将于" + TimeExchange.chineseDateTime(visitor.getVisitorTime()) + "到校!";
//将预约信息推送到希沃班牌
CommonResult seewo = pushInfo(visitor.getUserPhone(), visitor.getResponcode(), content);
if (!seewo.getCode().equals("200")) {
throw new Exception("审核失败");
}
}
//将数据写入门禁系统逻辑
else {
//家长不能使用车牌系统 其他的可以使用车牌逻辑
if(!ObjectUtils.isEmpty(visitor.getCarNum())){
String appId = parkConfig.getAppId();
String carNo = visitor.getCarNum();
String parkKey = parkConfig.getParkKey();
String rand = String.valueOf(Math.random());
String reserveTime = TimeExchange.DateToString(visitor.getVisitorTime(),"yyyy-MM-dd HH:mm:ss") ;
String reserveEndTime = TimeExchange.DateToString(visitor.getVisitorDeadline(),"yyyy-MM-dd HH:mm:ss");
String version = "v1.0";
String appSecret = parkConfig.getAppSecret();
String url = "http://openapi.szymzh.com/Api/Inform/Reservation";
JSONObject jsonobject = new JSONObject();
jsonobject.put("appid", appId);
jsonobject.put("carNo", carNo);
jsonobject.put("parkKey", parkKey);
jsonobject.put("rand", rand);
jsonobject.put("reserveEndTime", reserveEndTime);
jsonobject.put("reserveTime", reserveTime);
jsonobject.put("version", "v1.0");
//appid=ymdd36ed157ac423e2&carNo=赣U123659&parkKey=wdcmq9rc&rand=9.94995525689689966&reserveEndTime=2023-12-22 20:12:10&reserveTime=2023-12-21 18:12:10&version=v1.0&50596cd243dc4547b4c05f01f8ea02a4
String md5Str = "appid=" + appId + "&carNo=" + carNo + "&parkKey=" + parkKey + "&rand=" + rand + "&reserveEndTime=" + reserveEndTime + "&reserveTime=" + reserveTime + "&version=" +version+ "&" + appSecret;
String sign = CommonUtil.MD5(md5Str);
//sign签名
jsonobject.put("sign", sign);
//返回的结果中 code为1表示成功
String result = RequestUtils.httpPost(url, jsonobject.toJSONString());
System.out.println(result);
if(!result.contains("预约成功")){
throw new Exception("审核失败");
}
}
}
} else if (type == eExamineStatu.Refused.getValue()) {
visitor.setStatu(eApproveStatu.Refused.getValue());
}
int result = smartVisitorService.updateSmartVisitor(visitor);
if (result <= 0) {
throw new Exception("审核失败");
}
} catch (Exception e) {
throw new Exception("审核失败");
}
return CommonResult.ok("审核成功");
}
/**
* 将预约信息推送到希沃电子班牌
*
* @param parentPhone 预约手机号(家长手机号 在希沃中学生会和家长手机号绑定 可以绑定多个家长)
* @param studentCode 学生编号 希沃中的学生编号和万载系统中的一致
* @param content 消息内容
* @return
*/
public CommonResult pushInfo(String parentPhone, String studentCode, String content) {
//初始化客户端
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
HomeSchoolServiceSendNoteToKidParam param = new HomeSchoolServiceSendNoteToKidParam();
//响应体,MimeType为 application/json
HomeSchoolServiceSendNoteToKidParam.RequestBody requestBody = HomeSchoolServiceSendNoteToKidParam.RequestBody.builder()
.build();
param.setRequestBody(requestBody);
//query
HomeSchoolServiceSendNoteToKidParam.Query query = HomeSchoolServiceSendNoteToKidParam.Query.builder()
.appId(seewoConfig.getAppId())
.schoolUid(seewoConfig.getSchoolId())
.senderPhone(parentPhone)
.studentCode(studentCode)
.type(1)
.content(content)
.autoRegister(0)
.build();
requestBody.setQuery(query);
param.setRequestBody(requestBody);
HomeSchoolServiceSendNoteToKidRequest request = new HomeSchoolServiceSendNoteToKidRequest(param);
System.out.println("入参:" + request);
//如果想要调用沙箱环境,请通过设置 request 对象的 serverUrl 属性,如:
//request.setServerUrl("https://openapi.test.seewo.com")
//执行请求,如果想获取到com.seewo.open.sdk.HttpResponse对象,请调用 seewoClient.execute 方法
HomeSchoolServiceSendNoteToKidResult result = seewoClient.invoke(request);
System.out.println("出参:" + result);
if (!result.getMessage().equals("success")) {
CommonResult.fail();
}
return CommonResult.ok();
}
}