package com.ruoyi.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EsModel;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.domain.dto.zfEventdto;
import com.ruoyi.mapper.TravelCountMapper;
import com.ruoyi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static com.ruoyi.constant.MenuAuthority.*;
/**
*
* 服务实现类
*
*
* @author ojq
* @since 2023-10-06
*/
@Service
public class TravelCountServiceImpl extends ServiceImpl implements TravelCountService, ModuleSearchable {
@Autowired
private TravelDetailService travelDetailService;
@Autowired
private TravelCountService travelCountService;
@Autowired
ZInfoUserService zInfoUserService;
@Resource
ZAuthorityService zAuthorityService;
@Override
public AjaxResult selectDataList(Long userId, Integer pageNum, Integer pageSize, TravelCount travelCount, String happenStartTime, String happenEndTime) {
//要查自己家庭的
ZInfoUser myself = zInfoUserService.getMyself();
if (myself == null) {
// System.out.println("ssssss");
return AjaxResult.success("您没加入到对应的家庭,请联系管理员");
}
Long familyId = myself.getFamilyId();
//查看父母的数据:
Long fatherFaId = 0L;
if(myself.getFatherId()!=null)
fatherFaId = zInfoUserService.getInfoById(myself.getFatherId()).getFamilyId();
Long motherFaId = 0L;
if(myself.getMomId()!=null)
motherFaId = zInfoUserService.getInfoById(myself.getMomId()).getFamilyId();
//也要查别人授权的
List authority = zAuthorityService.getAuthority();
List idList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(TRAVEL_LIST)).map(ZAuthority::getFid).collect(Collectors.toList());
//加上自己家庭的id
idList.add(familyId);
//加上父母家族id
idList.add(fatherFaId);
idList.add(motherFaId);
List fms = zInfoUserService.findByUaidToFaid(myself.getUaid()).stream().map(ZInfoUser::getFamilyId).collect(Collectors.toList());
if(!fms.isEmpty())
{
idList.addAll(fms);
}
LambdaQueryWrapper lqw = buildCondition(travelCount, idList);
Page travelCountPage = new Page<>(pageNum, pageSize);
Page pageResult = page(travelCountPage, lqw);
List beanRecords = pageResult.getRecords();//得到查询出来的数据
HashMap data = MapUtils.getResult(pageResult, beanRecords);
return AjaxResult.success(data);
}
@Override
public AjaxResult addData(TravelCount travelCount) {
ZInfoUser myself = zInfoUserService.getMyself();
Long familyId = myself.getFamilyId();
if (familyId == null) {
throw new RuntimeException("您还未加入任何家庭");
}
List authority = zAuthorityService.getAuthority();
List familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(TRAVEL_LIST_ADD)).map(ZAuthority::getFid).collect(Collectors.toList());
familyIdList.add(familyId);
if (travelCount.getFamilyId() != null && !familyIdList.contains(travelCount.getFamilyId())) {
throw new RuntimeException("你没有权限操作此家庭的数据");
}
if (travelCount.getFamilyId() == null) {
//默认添加自己家庭的数据
travelCount.setFamilyId(familyId);
}
if (save(travelCount)) {
return AjaxResult.success();
} else {
return AjaxResult.error();
}
}
@Override
public AjaxResult updateData(TravelCount travelCount) {
ZInfoUser myself = zInfoUserService.getMyself();
Long familyId = myself.getFamilyId();
//先根据id查询出数据的familyId,看看有没有权限操作
Long dataFamilyId = getById(travelCount.getId()).getFamilyId();
List authority = zAuthorityService.getAuthority();
List familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(TRAVEL_LIST_UPDATE)).map(ZAuthority::getFid).collect(Collectors.toList());
familyIdList.add(familyId);
if (dataFamilyId != null && !familyIdList.contains(dataFamilyId)) {
throw new RuntimeException("你没有权限操作此家庭的数据");
}
if (updateById(travelCount)) {
return AjaxResult.success();
} else {
return AjaxResult.error();
}
}
/**
* 批量删除统计表数据
*/
@Override
@Transactional
public void removeData(List list) {
List dataList = this.listByIds(list);
ZInfoUser myself = zInfoUserService.getMyself();
Long familyId = myself.getFamilyId();
List authority = zAuthorityService.getAuthority();
List familyIdList = authority.stream().filter(auth -> auth.getAuthority().toString().equals(TRAVEL_LIST_REMOVE)).map(ZAuthority::getFid).collect(Collectors.toList());
familyIdList.add(familyId);
for (TravelCount data : dataList) {
if (!familyIdList.contains(data.getFamilyId())) {
throw new RuntimeException("你没有权限操作此家庭的数据");
}
}
//在删除统计表数据之前,要先删除明细表的数据
travelDetailService.removeBatchByCid(list);
//再删除自己表的数据
travelCountService.removeBatchByIds(list);
}
@Override
public TravelCount getDataById(Long id) {
TravelCount travelCount = getById(id);
inputTotalData(travelCount);
return travelCount;
}
private void inputTotalData(TravelCount travelCount){
//先找到对应一条统计数据的所有详细数据
List travelDetailList = travelDetailService.getDataByCid(travelCount.getId(),null);
double entranceTotal = 0;
double eatTotal = 0;
double stayTotal = 0;
double travelTotal = 0;
double shopTotal = 0;
//把所有详细数据的费用值累加起来
for (TravelDetail detail : travelDetailList) {
entranceTotal+=detail.getEntrance();
eatTotal+=detail.getEat();
stayTotal+=detail.getStay();
travelTotal+=detail.getTravel();
shopTotal += detail.getShop();
}
travelCount.setEntranceTotal(entranceTotal);
travelCount.setEatTotal(eatTotal);
travelCount.setStayTotal(stayTotal);
travelCount.setTravelTotal(travelTotal);
travelCount.setShopTotal(shopTotal);
DecimalFormat df = new DecimalFormat("0.00");
travelCount.setTotalPrice(Double.parseDouble(df.format(entranceTotal+eatTotal+travelCount.getGroupTotal()+stayTotal+travelTotal)));
}
private LambdaQueryWrapper buildCondition(TravelCount travelCount, List familyIdList) {
LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
lqw.orderByDesc(TravelCount::getCreateTime);
lqw.in(familyIdList != null,TravelCount::getFamilyId, familyIdList);
lqw.orderByDesc(TravelCount::getCreateTime);
lqw.like(travelCount.getName()!=null,TravelCount::getName,travelCount.getName());
lqw.like(travelCount.getDestination()!=null,TravelCount::getDestination,travelCount.getDestination());
lqw.like(travelCount.getProperty()!=null,TravelCount::getProperty,travelCount.getProperty());
lqw.like(travelCount.getManner()!=null,TravelCount::getManner,travelCount.getManner());
lqw.like(travelCount.getName()!=null,TravelCount::getName,travelCount.getName());
lqw.like(travelCount.getName()!=null,TravelCount::getName,travelCount.getName());
lqw.like(travelCount.getName()!=null,TravelCount::getName,travelCount.getName());
if (StringUtils.isNotEmpty(travelCount.getIsHide())) {
lqw.and(wrapper -> wrapper
.ne(TravelCount::getIsHide, "是")
.or()
.isNull(TravelCount::getIsHide)
);
}
if (travelCount.getHasAttachment() != null && travelCount.getHasAttachment().equals("是")) {
lqw.apply("url IS NOT NULL AND url != ''");
}
// lqw.in(StringUtils.isNotEmpty(travelCount.getCompanionList()),travelCount::getCompanion,travelCount.getCompanionList());
// if (travelCount.getYear() != 0) {
// System.out.println("pppppppppppppppppppppppppppppppppppppppppppppppppppp"+travelCount.getYear());
// lqw.apply("YEAR(happen_time) = {0}", travelCount.getYear());
// }
if (CollectionUtils.isNotEmpty(travelCount.getCompanionList())) {
List companionList = travelCount.getCompanionList();
// 清理和去重
companionList = companionList.stream()
.map(String::trim)
.filter(s -> !s.isEmpty())
.distinct()
.collect(Collectors.toList());
if (!companionList.isEmpty()) {
// 确保所有参与人都在companion字段中
for (String companion : companionList) {
lqw.apply("FIND_IN_SET({0}, REPLACE(REPLACE(companion, ' ', ''), ',', ',')) > 0",
companion);
}
}
}
System.out.println("查询条件: " + lqw.getCustomSqlSegment());
return lqw;
}
@Override
public String getModuleCode() {
return "旅游";
}
@Override
// @Cacheable(value = "economy_search", key = "T(String).format('2045_%s_%s_%s_%s',#companion != null ? #companion : 'null',#happenStartTime != null ? #happenStartTime.getTime() : 0,#happenEndTime != null ? #happenEndTime.getTime() : 0,#hasAttachment != null ? #hasAttachment : 'null')")
public List> search(String companion, Date happenStartTime, Date happenEndTime, String hasAttachment) {
TravelCount travelCount = new TravelCount();
travelCount.setCompanion(companion);
String[] split = companion.split(",");
List list = Arrays.asList(split);
travelCount.setCompanionList(list);
travelCount.setHasAttachment(hasAttachment);
travelCount.setIsHide("否");
System.out.println("[[[[[[[["+travelCount);
return dataList(travelCount);
}
public List dataList(TravelCount travelCount) {
LambdaQueryWrapper lqw = buildCondition(travelCount, null);
List beanRecords = list(lqw);
return encapData(beanRecords);
}
public List encapData(List lis)
{
List daoRes = new ArrayList<>() ;
for(TravelCount obj : lis)
{
zfEventdto atd = new zfEventdto();
atd.setId(Math.toIntExact(obj.getId()));
atd.setModule("旅游");
atd.setName(obj.getCompanion());
atd.setCreateTime(obj.getCreateTime());
daoRes.add(atd);
}
return daoRes;
}
}