//package com.yuchen.saas.park.service.service.irrigate.impl;
//
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.yuchen.saas.manage.system.api.entity.NestOrgUser;
//import com.yuchen.saas.manage.system.api.feign.IOrgUserClient;
//import com.yuchen.saas.park.api.dto.irrigate.IrrigatePointLocationPageDTO;
//import com.yuchen.saas.park.api.dto.irrigate.IrrigatePointLocationSaveDTO;
//import com.yuchen.saas.park.api.dto.irrigate.IrrigatePointLocationUpdateDTO;
//import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
//import com.yuchen.saas.park.api.entity.park.irrigate.PmgBatIrrigateArea;
//import com.yuchen.saas.park.api.entity.park.irrigate.PmgBatIrrigatePointLocation;
//import com.yuchen.saas.park.api.vo.irrigate.IrrigatePointLocationPageVO;
//import com.yuchen.saas.park.service.mapper.irrigate.PmgBatIrrigatePointLocationMapper;
//import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
//import com.yuchen.saas.park.service.service.irrigate.IPmgBatIrrigateAreaService;
//import com.yuchen.saas.park.service.service.irrigate.IPmgBatIrrigatePointLocationService;
//import org.nest.springwrap.core.log.exception.ServiceException;
//import org.nest.springwrap.core.mp.base.BaseEntity;
//import org.nest.springwrap.core.mp.base.BaseServiceImpl;
//import org.nest.springwrap.core.mp.support.Condition;
//import org.nest.springwrap.core.mp.support.NestPage;
//import org.nest.springwrap.core.secure.utils.AuthUtil;
//import org.nest.springwrap.core.tool.api.R;
//import org.nest.springwrap.core.tool.utils.Func;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
///**
// * <p>
// * 浇灌点位信息表 服务实现类
// * </p>
// *
// * @author linyb
// * @since 2024-05-20
// */
//@Service
//public class PmgBatIrrigatePointLocationServiceImpl extends BaseServiceImpl<PmgBatIrrigatePointLocationMapper, PmgBatIrrigatePointLocation> implements IPmgBatIrrigatePointLocationService {
//    @Resource
//    private IOrgUserClient iOrgUserClient;
//
//    @Resource
//    private PmgBatParkInfoService pmgBatParkInfoService;
//
//    @Resource
//    private IPmgBatIrrigateAreaService pmgBatIrrigateAreaService;
//    @Override
//    public NestPage<IrrigatePointLocationPageVO> getPage(IrrigatePointLocationPageDTO dto) {
//
//        LambdaQueryWrapper<PmgBatIrrigatePointLocation> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Func.isNotEmpty(dto.getParkId()),PmgBatIrrigatePointLocation::getParkId,dto.getParkId());
//        queryWrapper.eq(Func.isNotEmpty(dto.getIrrigateAreaId()),PmgBatIrrigatePointLocation::getIrrigateAreaId,dto.getIrrigateAreaId());
//        queryWrapper.like(Func.isNotEmpty(dto.getIrrigatePointLocationName()),PmgBatIrrigatePointLocation::getIrrigatePointLocationName,dto.getIrrigatePointLocationName());
//        IPage<PmgBatIrrigatePointLocation> page = this.page(Condition.getPage(dto), queryWrapper);
//        NestPage<IrrigatePointLocationPageVO> resultPage = new NestPage<>();
//        resultPage.setTotal(page.getTotal());
//        resultPage.setSize(page.getSize());
//        resultPage.setCurrent(page.getCurrent());
//        if (Func.isEmpty(page.getRecords())){
//            resultPage.setRecords(new ArrayList<>());
//            return resultPage;
//        }
//        // 组织用户列表
//        List<Long> orgUserIdList = page.getRecords().stream().map(PmgBatIrrigatePointLocation::getCreateOrgUser).distinct().collect(Collectors.toList());
//        List<Long> parkIdList = page.getRecords().stream().map(PmgBatIrrigatePointLocation::getParkId).distinct().collect(Collectors.toList());
//        List<Long> arerIdList = page.getRecords().stream().map(PmgBatIrrigatePointLocation::getIrrigateAreaId).distinct().collect(Collectors.toList());
//        List<NestOrgUser> orgUserList =  Optional.ofNullable(iOrgUserClient.findOrgUserListByIds(orgUserIdList).getData()).orElse(new ArrayList<>());
//        List<PmgBatIrrigateArea> areaList = pmgBatIrrigateAreaService.listByIds(arerIdList);
//        Map<Long, String> orgUserMap = orgUserList.stream().collect(Collectors.toMap(BaseEntity::getId, NestOrgUser::getName));
//        Map<Long, String> parkMap = pmgBatParkInfoService.listByIds(parkIdList).stream().collect(Collectors.toMap(BaseEntity::getId, PmgBatParkInfo::getParkName));
//        Map<Long, String> areaMap = areaList.stream().collect(Collectors.toMap(BaseEntity::getId, PmgBatIrrigateArea::getIrrigateAreaName));
//        List<IrrigatePointLocationPageVO> collect = page.getRecords().stream().map(e -> {
//            IrrigatePointLocationPageVO irrigatePointLocationPageVO = Func.copy(e, IrrigatePointLocationPageVO.class);
//            irrigatePointLocationPageVO.setCreateOrgUserName(orgUserMap.get(e.getCreateOrgUser()));
//            irrigatePointLocationPageVO.setIrrigateAreaName(areaMap.get(e.getIrrigateAreaId()));
//            irrigatePointLocationPageVO.setParkName(parkMap.get(e.getParkId()));
//            return irrigatePointLocationPageVO;
//        }).collect(Collectors.toList());
//        resultPage.setRecords(collect);
//        return resultPage;
//    }
//
//    @Override
//    public IrrigatePointLocationPageVO info(Long id) {
//        PmgBatIrrigatePointLocation pmgBatIrrigatePointLocation = this.getById(id);
//        if (Func.isEmpty(pmgBatIrrigatePointLocation)){
//            throw new ServiceException("数据不存在！");
//        }
//        IrrigatePointLocationPageVO irrigatePointLocationPageVO = Func.copy(pmgBatIrrigatePointLocation, IrrigatePointLocationPageVO.class);
//        PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(irrigatePointLocationPageVO.getParkId());
//        if (Func.isNotEmpty(parkInfo)){
//            irrigatePointLocationPageVO.setParkName(parkInfo.getParkName());
//        }
//        R<NestOrgUser> nestOrgUserR = iOrgUserClient.detail(irrigatePointLocationPageVO.getCreateOrgUser());
//        if (R.isSuccess(nestOrgUserR)&&Func.isNotEmpty(nestOrgUserR.getData())){
//            irrigatePointLocationPageVO.setCreateOrgUserName(nestOrgUserR.getData().getName());
//        }
//        PmgBatIrrigateArea pmgBatIrrigateArea = pmgBatIrrigateAreaService.getById(irrigatePointLocationPageVO.getIrrigateAreaId());
//        if (Func.isNotEmpty(pmgBatIrrigateArea)){
//            irrigatePointLocationPageVO.setIrrigateAreaName(pmgBatIrrigateArea.getIrrigateAreaName());
//        }
//        return irrigatePointLocationPageVO;
//    }
//
//    @Override
//    public List<IrrigatePointLocationPageVO> getList(Long parkId) {
//        LambdaQueryWrapper<PmgBatIrrigatePointLocation> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(PmgBatIrrigatePointLocation::getParkId,parkId);
//        List<PmgBatIrrigatePointLocation> list = this.list(queryWrapper);
//        if (Func.isEmpty(list)){
//            return new ArrayList<>();
//        }
//        // 组织用户列表
//        List<Long> orgUserIdList = list.stream().map(PmgBatIrrigatePointLocation::getCreateOrgUser).collect(Collectors.toList());
//        List<Long> parkIdList =list.stream().map(PmgBatIrrigatePointLocation::getParkId).collect(Collectors.toList());
//        List<NestOrgUser> orgUserList = Optional.ofNullable(iOrgUserClient.findOrgUserListByIds(orgUserIdList).getData()).orElse(new ArrayList<>());
//        List<Long> arerIdList = list.stream().map(PmgBatIrrigatePointLocation::getIrrigateAreaId).distinct().collect(Collectors.toList());
//        List<PmgBatIrrigateArea> areaList = pmgBatIrrigateAreaService.listByIds(arerIdList);
//        Map<Long, String> orgUserMap = orgUserList.stream().collect(Collectors.toMap(BaseEntity::getId, NestOrgUser::getName));
//        Map<Long, String> parkMap = pmgBatParkInfoService.listByIds(parkIdList).stream().collect(Collectors.toMap(BaseEntity::getId, PmgBatParkInfo::getParkName));
//        Map<Long, String> areaMap = areaList.stream().collect(Collectors.toMap(BaseEntity::getId, PmgBatIrrigateArea::getIrrigateAreaName));
//        return list.stream().map(e -> {
//            IrrigatePointLocationPageVO irrigatePointLocationPageVO = Func.copy(e, IrrigatePointLocationPageVO.class);
//            irrigatePointLocationPageVO.setCreateOrgUserName(orgUserMap.get(e.getCreateOrgUser()));
//            irrigatePointLocationPageVO.setParkName(parkMap.get(e.getParkId()));
//            irrigatePointLocationPageVO.setIrrigateAreaName(areaMap.get(e.getIrrigateAreaId()));
//            return irrigatePointLocationPageVO;
//        }).collect(Collectors.toList());
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Boolean save(IrrigatePointLocationSaveDTO dto) {
//        int count = this.count(new LambdaQueryWrapper<PmgBatIrrigatePointLocation>()
//                        .eq(PmgBatIrrigatePointLocation::getParkId,dto.getParkId())
//                .eq(PmgBatIrrigatePointLocation::getIrrigatePointLocationName, dto.getIrrigatePointLocationName()));
//        if (count>0){
//            throw new ServiceException("点位名称已经存在！");
//        }
//        PmgBatIrrigatePointLocation pmgBatIrrigatePointLocation = Func.copy(dto, PmgBatIrrigatePointLocation.class);
//        pmgBatIrrigatePointLocation.setCreateOrgUser(AuthUtil.getUser().getOrgUserId());
//        return this.save(pmgBatIrrigatePointLocation);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Boolean update(IrrigatePointLocationUpdateDTO dto) {
//        PmgBatIrrigatePointLocation batIrrigatePointLocation = this.getById(dto.getId());
//        if (Func.isEmpty(batIrrigatePointLocation)){
//            throw new ServiceException("数据不存在！");
//        }
//        int count = this.count(new LambdaQueryWrapper<PmgBatIrrigatePointLocation>()
//                .eq(PmgBatIrrigatePointLocation::getIrrigatePointLocationName, dto.getIrrigatePointLocationName())
//                .eq(PmgBatIrrigatePointLocation::getParkId, dto.getParkId())
//                .ne(PmgBatIrrigatePointLocation::getId,dto.getId()));
//        if (count>0){
//            throw new ServiceException("点位名称已经存在！");
//        }
//        Func.copy(dto, batIrrigatePointLocation);
//        return this.updateById(batIrrigatePointLocation);
//    }
//}
