package com.ruoyi.web.controller.api;
import java.time.LocalDate;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.BasePage;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.query.AppUserQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.query.ConsultationQuery;
import com.ruoyi.web.controller.query.InformationQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
*
* 咨询列表 前端控制器
*
*
* @author luodangjia
* @since 2024-09-19
*/
@RestController
@RequestMapping("/t-consultation")
public class TConsultationController {
@Resource
private TConsultationService informationService;
@Resource
private TRegionService regionService;
@Resource
private TTechnicalTitleService tTechnicalTitleService;
@Resource
private TTitleMajorService majorService;
@Resource
private RedisCache redisCache;
@Resource
private TokenService tokenService;
@Resource
private TConsultationClassificationService consultationClassificationService;
@ApiOperation(value = "通过省名查询下级城市",tags = {"后台-咨询管理-咨询列表","web-咨询查询"})
@PostMapping(value = "/getcity")
public R> getcity(@RequestParam String provinceName) {
List list = regionService.lambdaQuery().eq(TRegion::getProvinceName, provinceName).eq(TRegion::getIsOpen, 1).list();
return R.ok(list);
}
@Resource
private TAppUserService appUserService;
//新增咨询
@ApiOperation(value = "添加",tags = "后台-咨询管理-咨询列表")
@PostMapping(value = "/add")
public R add(@Validated @RequestBody TConsultation information) {
informationService.save(information);
AppUserQuery appUserQuery = new AppUserQuery();
appUserQuery.setMajorId1(information.getMajorId());
appUserQuery.setTitleId1(information.getTitleId());
appUserQuery.setRegionId1(Long.valueOf(information.getRegionId()));
appUserQuery.setLevelId1(information.getLevel());
List newIds = appUserService.getNewIds(appUserQuery);
for (Long newId : newIds) {
redisCache.setCacheObject("ALLERT:"+newId,information.getClassificationId());
}
return R.ok();
}
//修改咨询
@ApiOperation(value = "修改",tags = "后台-咨询管理-咨询列表")
@PostMapping(value = "/edit")
public R edit(@RequestBody TConsultation information) {
informationService.updateById(information);
AppUserQuery appUserQuery = new AppUserQuery();
appUserQuery.setMajorId1(information.getMajorId());
appUserQuery.setTitleId1(information.getTitleId());
appUserQuery.setRegionId1(Long.valueOf(information.getRegionId()));
appUserQuery.setLevelId1(information.getLevel());
List newIds = appUserService.getNewIds(appUserQuery);
for (Long newId : newIds) {
redisCache.setCacheObject("ALLERT:"+newId,information.getClassificationId());
}
return R.ok();
}
@Resource
private TConsultationMessageService messageService;
//删除咨询
@ApiOperation(value = "删除",tags = "后台-咨询管理-咨询列表")
@PostMapping(value = "/deleteByIds")
public R deleteByIds(String ids) {
informationService.removeByIds(Arrays.asList(ids.split(",")));
messageService.remove(Wrappers.lambdaQuery(TConsultationMessage.class).in(TConsultationMessage::getConsultationId,Arrays.asList(ids.split(","))));
return R.ok();
}
//查询咨询
@ApiOperation(value = "查询",tags = {"后台-咨询管理-咨询列表","web-咨询查询"})
@PostMapping(value = "/list")
public R> list(@RequestBody ConsultationQuery informationQuery) {
Long userId = tokenService.getLoginUser().getUserId();
Page page = informationService.lambdaQuery()
.like(!StringUtils.isEmpty(informationQuery.getClassificationName()), TConsultation::getClassificationName, informationQuery.getClassificationName())
.eq(informationQuery.getRegionId() != null, TConsultation::getRegionId, informationQuery.getRegionId())
.eq(informationQuery.getTechnicalId() != null, TConsultation::getTitleId, informationQuery.getTechnicalId())
.eq(informationQuery.getClassificationId() != null, TConsultation::getClassificationId, informationQuery.getClassificationId())
.eq(informationQuery.getMajorId() != null, TConsultation::getMajorId, informationQuery.getMajorId())
.eq(informationQuery.getLevel() != null, TConsultation::getLevel, informationQuery.getLevel())
.orderByDesc(TConsultation::getClassificationSort)
.orderByDesc(TConsultation::getPubTime)
.page(Page.of(informationQuery.getPageNum(), informationQuery.getPageSize()));
Set cacheSet = redisCache.getCacheSet("Consultation:" + userId);
for (TConsultation record : page.getRecords()) {
TRegion byId = regionService.getById(record.getRegionId());
record.setRegionName(byId.getProvinceName()+"-"+byId.getName());
TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTitleId());
TTitleMajor byId2 = majorService.getById(record.getMajorId());
record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName());
TConsultationClassification byId3 = consultationClassificationService.getById(record.getClassificationId());
record.setTConsultationClassificationName(byId3.getConsultationName());
if (cacheSet!=null){
if (cacheSet.contains(record.getId())){
record.setIsCollect(1);
}else {
record.setIsCollect(0);
}
}else {
record.setIsCollect(0);
}
}
return R.ok(page);
}
@Resource
private TLevelService levelService;
@ApiOperation(value = "详情",tags = {"后台-咨询管理-咨询列表","web-咨询查询"})
@PostMapping(value = "/detail")
public R detail(@RequestParam Long id ) {
TConsultation byId = informationService.getById(id);
TTechnicalTitle byId1 = tTechnicalTitleService.getById(byId.getTitleId());
byId.setTechnicalName(byId1.getTitileName());
TTitleMajor byId2 = majorService.getById(byId.getMajorId());
byId.setMajorName(byId2.getMajorName());
TLevel byId3 = levelService.getById(byId.getLevel());
byId.setLevelName(byId3.getName());
TRegion byId4 = regionService.getById(byId.getRegionId());
byId.setTRegion(byId4);
return R.ok(byId);
}
@ApiOperation(value = "观看",tags = {"web-咨询查询"})
@PostMapping(value = "/watch")
public R watch(@RequestParam Long id ) {
TConsultation byId = informationService.getById(id);
byId.setWatchNum(byId.getWatchNum()+1);
informationService.updateById(byId);
return R.ok();
}
@ApiOperation(value = "留言",tags = {"web-咨询查询"})
@PostMapping(value = "/message")
public R message(@RequestBody TConsultationMessage message) {
Long userId = tokenService.getLoginUser().getUserId();
message.setUserId(userId);
messageService.save(message);
return R.ok();
}
@ApiOperation(value = "收藏咨询",tags = {"web-个人中心"})
@PostMapping(value = "/info/list")
public R> infolist(@RequestBody BasePage basePage) {
Long userId = tokenService.getLoginUser().getUserId();
Set cacheSet = redisCache.getCacheSet("Consultation:" + userId);
if (!cacheSet.isEmpty()) {
Page page = informationService.lambdaQuery().in(TConsultation::getId, cacheSet).page(Page.of(basePage.getPageNum(), basePage.getPageSize()));
return R.ok(page);
}else {
return R.ok(new Page<>());
}
}
@ApiOperation(value = "咨询",tags = {"web-个人中心"})
@PostMapping(value = "/collect/is")
public R collectis(@RequestParam Long consultationId) {
Long userId = tokenService.getLoginUser().getUserId();
Set cacheSet = redisCache.getCacheSet("Consultation:" + userId);
if (cacheSet.contains(consultationId)){
return R.ok(true);
}else {
return R.ok(false);
}
}
}