//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.GreenPlantsPageDTO;
//import com.yuchen.saas.park.api.dto.irrigate.GreenPlantsSaveDTO;
//import com.yuchen.saas.park.api.dto.irrigate.GreenPlantsUpdateDTO;
//import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
//import com.yuchen.saas.park.api.entity.park.irrigate.PmgBatIrrigateGreenPlants;
//import com.yuchen.saas.park.api.vo.irrigate.GreenPlantsPageVO;
//import com.yuchen.saas.park.service.mapper.irrigate.PmgBatIrrigateGreenPlantsMapper;
//import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
//import com.yuchen.saas.park.service.service.irrigate.IPmgBatIrrigateGreenPlantsService;
//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.nest.springwrap.core.tool.validator.ValidatorUtils;
//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-09
// */
//@Service
//public class PmgBatIrrigateGreenPlantsServiceImpl extends BaseServiceImpl<PmgBatIrrigateGreenPlantsMapper, PmgBatIrrigateGreenPlants> implements IPmgBatIrrigateGreenPlantsService {
//    @Resource
//    private IOrgUserClient iOrgUserClient;
//
//    @Resource
//    private PmgBatParkInfoService pmgBatParkInfoService;
//    @Override
//    public NestPage<GreenPlantsPageVO> getPage(GreenPlantsPageDTO dto) {
//        ValidatorUtils.validateEntity(dto);
//        LambdaQueryWrapper<PmgBatIrrigateGreenPlants> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(PmgBatIrrigateGreenPlants::getParkId,dto.getParkId());
//        queryWrapper.like(Func.isNotEmpty(dto.getGreenPlantsName()),PmgBatIrrigateGreenPlants::getGreenPlantsName,dto.getGreenPlantsName());
//        IPage<PmgBatIrrigateGreenPlants> page = this.page(Condition.getPage(dto), queryWrapper);
//        NestPage<GreenPlantsPageVO> 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(PmgBatIrrigateGreenPlants::getCreateOrgUser).collect(Collectors.toList());
//        List<Long> parkIdList = page.getRecords().stream().map(PmgBatIrrigateGreenPlants::getParkId).collect(Collectors.toList());
//        List<NestOrgUser> orgUserList =  Optional.ofNullable(iOrgUserClient.findOrgUserListByIds(orgUserIdList).getData()).orElse(new ArrayList<>());
//        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));
//        List<GreenPlantsPageVO> collect = page.getRecords().stream().map(e -> {
//            GreenPlantsPageVO pageVO = Func.copy(e, GreenPlantsPageVO.class);
//            pageVO.setCreateOrgUserName(orgUserMap.get(e.getCreateOrgUser()));
//            pageVO.setParkName(parkMap.get(e.getParkId()));
//            return pageVO;
//        }).collect(Collectors.toList());
//        resultPage.setRecords(collect);
//        return resultPage;
//    }
//
//    @Override
//    public GreenPlantsPageVO info(Long id) {
//        PmgBatIrrigateGreenPlants pmgBatIrrigateGreenPlants = this.getById(id);
//        if (Func.isEmpty(pmgBatIrrigateGreenPlants)){
//            throw new ServiceException("数据不存在！");
//        }
//        GreenPlantsPageVO greenPlantsPageVO = Func.copy(pmgBatIrrigateGreenPlants, GreenPlantsPageVO.class);
//        PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(greenPlantsPageVO.getParkId());
//        if (Func.isNotEmpty(parkInfo)){
//            greenPlantsPageVO.setParkName(parkInfo.getParkName());
//        }
//        R<NestOrgUser> nestOrgUserR = iOrgUserClient.detail(greenPlantsPageVO.getCreateOrgUser());
//        if (R.isSuccess(nestOrgUserR)&&Func.isNotEmpty(nestOrgUserR.getData())){
//            greenPlantsPageVO.setCreateOrgUserName(nestOrgUserR.getData().getName());
//        }
//        return greenPlantsPageVO;
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Boolean save(GreenPlantsSaveDTO dto) {
//        int count = this.count(new LambdaQueryWrapper<PmgBatIrrigateGreenPlants>().eq(PmgBatIrrigateGreenPlants::getParkId, dto.getParkId())
//                .eq(PmgBatIrrigateGreenPlants::getGreenPlantsCode, dto.getGreenPlantsCode()));
//        if (count>0){
//            throw new ServiceException("绿植编码已经存在！");
//        }
//        PmgBatIrrigateGreenPlants pmgBatIrrigateGreenPlants = Func.copy(dto, PmgBatIrrigateGreenPlants.class);
//        pmgBatIrrigateGreenPlants.setCreateOrgUser(AuthUtil.getUser().getOrgUserId());
//       return this.save(pmgBatIrrigateGreenPlants);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Boolean update(GreenPlantsUpdateDTO dto) {
//        PmgBatIrrigateGreenPlants batIrrigateGreenPlants = this.getById(dto.getId());
//        if (Func.isEmpty(batIrrigateGreenPlants)){
//            throw new ServiceException("数据不存在！");
//        }
//        int count = this.count(new LambdaQueryWrapper<PmgBatIrrigateGreenPlants>()
//                .eq(PmgBatIrrigateGreenPlants::getParkId, dto.getParkId())
//                .eq(PmgBatIrrigateGreenPlants::getGreenPlantsCode, dto.getGreenPlantsCode()).ne(PmgBatIrrigateGreenPlants::getId,dto
//                .getId()));
//        if (count>0){
//            throw new ServiceException("区域编码已经存在！");
//        }
//        Func.copy(dto, batIrrigateGreenPlants);
//       return this.updateById(batIrrigateGreenPlants);
//    }
//
//    @Override
//    public List<GreenPlantsPageVO> getList(Long parkId) {
//        LambdaQueryWrapper<PmgBatIrrigateGreenPlants> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(PmgBatIrrigateGreenPlants::getParkId,parkId);
//        List<PmgBatIrrigateGreenPlants> list = this.list(queryWrapper);
//        if (Func.isEmpty(list)){
//            return new ArrayList<>();
//        }
//        // 组织用户列表
//        List<Long> orgUserIdList = list.stream().map(PmgBatIrrigateGreenPlants::getCreateOrgUser).collect(Collectors.toList());
//        List<Long> parkIdList =list.stream().map(PmgBatIrrigateGreenPlants::getParkId).collect(Collectors.toList());
//        List<NestOrgUser> orgUserList = Optional.ofNullable(iOrgUserClient.findOrgUserListByIds(orgUserIdList).getData()).orElse(new ArrayList<>());
//        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));
//        return list.stream().map(e -> {
//            GreenPlantsPageVO greenPlantsPageVO = Func.copy(e, GreenPlantsPageVO.class);
//            greenPlantsPageVO.setCreateOrgUserName(orgUserMap.get(e.getCreateOrgUser()));
//            greenPlantsPageVO.setParkName(parkMap.get(e.getParkId()));
//            return greenPlantsPageVO;
//        }).collect(Collectors.toList());
//    }
//}
