//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.IrrigateAreaPageDTO;
//import com.yuchen.saas.park.api.dto.irrigate.IrrigateAreaSaveDTO;
//import com.yuchen.saas.park.api.dto.irrigate.IrrigateAreaUpdateDTO;
//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.PmgBatIrrigateGreenPlants;
//import com.yuchen.saas.park.api.vo.irrigate.IrrigateAreaPageVO;
//import com.yuchen.saas.park.service.mapper.irrigate.PmgBatIrrigateAreaMapper;
//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.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-04-16
// */
//@Service
//public class PmgBatIrrigateAreaServiceImpl extends BaseServiceImpl<PmgBatIrrigateAreaMapper, PmgBatIrrigateArea> implements IPmgBatIrrigateAreaService {
//    @Resource
//    private  IOrgUserClient iOrgUserClient;
//
//    @Resource
//    private PmgBatParkInfoService pmgBatParkInfoService;
//    @Resource
//    private  IPmgBatIrrigateGreenPlantsService irrigateGreenPlantsService;
//    @Override
//    public NestPage<IrrigateAreaPageVO> getPage(IrrigateAreaPageDTO dto) {
//        ValidatorUtils.validateEntity(dto);
//        LambdaQueryWrapper<PmgBatIrrigateArea> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(PmgBatIrrigateArea::getParkId,dto.getParkId());
//        queryWrapper.like(Func.isNotEmpty(dto.getIrrigateAreaName()),PmgBatIrrigateArea::getIrrigateAreaName,dto.getIrrigateAreaName());
//        queryWrapper.eq(Func.isNotEmpty(dto.getIrrigateAreaCode()),PmgBatIrrigateArea::getIrrigateAreaCode,dto.getIrrigateAreaCode());
//        IPage<PmgBatIrrigateArea> page = this.page(Condition.getPage(dto), queryWrapper);
//        NestPage<IrrigateAreaPageVO> 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(PmgBatIrrigateArea::getCreateOrgUser).collect(Collectors.toList());
//        List<Long> parkIdList = page.getRecords().stream().map(PmgBatIrrigateArea::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<IrrigateAreaPageVO> collect = page.getRecords().stream().map(e -> {
//            IrrigateAreaPageVO irrigateAreaPageVO = Func.copy(e, IrrigateAreaPageVO.class);
//            irrigateAreaPageVO.setCreateOrgUserName(orgUserMap.get(e.getCreateOrgUser()));
//            irrigateAreaPageVO.setParkName(parkMap.get(e.getParkId()));
//            if (Func.isNotEmpty(e.getGreenPlantsId())){
//                List<PmgBatIrrigateGreenPlants> pmgBatIrrigateGreenPlants = irrigateGreenPlantsService.listByIds(Func.toLongList(e.getGreenPlantsId()));
//                if (Func.isNotEmpty(pmgBatIrrigateGreenPlants)){
//                    irrigateAreaPageVO.setGreenPlantsName(pmgBatIrrigateGreenPlants.stream().map(PmgBatIrrigateGreenPlants::getGreenPlantsName).collect(Collectors.joining("、")));
//                }
//            }
//            return irrigateAreaPageVO;
//        }).collect(Collectors.toList());
//        resultPage.setRecords(collect);
//        return resultPage;
//    }
//
//    @Override
//    public IrrigateAreaPageVO info(Long id) {
//        PmgBatIrrigateArea pmgBatIrrigateArea = this.getById(id);
//        if (Func.isEmpty(pmgBatIrrigateArea)){
//            throw new ServiceException("数据不存在！");
//        }
//        IrrigateAreaPageVO irrigateAreaPageVO = Func.copy(pmgBatIrrigateArea, IrrigateAreaPageVO.class);
//        PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(irrigateAreaPageVO.getParkId());
//        if (Func.isNotEmpty(parkInfo)){
//            irrigateAreaPageVO.setParkName(parkInfo.getParkName());
//        }
//        R<NestOrgUser> nestOrgUserR = iOrgUserClient.detail(irrigateAreaPageVO.getCreateOrgUser());
//        if (R.isSuccess(nestOrgUserR)&&Func.isNotEmpty(nestOrgUserR.getData())){
//            irrigateAreaPageVO.setCreateOrgUserName(nestOrgUserR.getData().getName());
//        }
//        if (Func.isNotEmpty(irrigateAreaPageVO.getGreenPlantsId())){
//            List<PmgBatIrrigateGreenPlants> pmgBatIrrigateGreenPlants = irrigateGreenPlantsService.listByIds(Func.toLongList(irrigateAreaPageVO.getGreenPlantsId()));
//            if (Func.isNotEmpty(pmgBatIrrigateGreenPlants)){
//                irrigateAreaPageVO.setGreenPlantsName(pmgBatIrrigateGreenPlants.stream().map(PmgBatIrrigateGreenPlants::getGreenPlantsName).collect(Collectors.joining("、")));
//            }
//        }
//        return irrigateAreaPageVO;
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void save(IrrigateAreaSaveDTO dto) {
//        int count = this.count(new LambdaQueryWrapper<PmgBatIrrigateArea>().eq(PmgBatIrrigateArea::getParkId, dto.getParkId())
//                .eq(PmgBatIrrigateArea::getIrrigateAreaCode, dto.getIrrigateAreaCode()));
//        if (count>0){
//            throw new ServiceException("区域编码已经存在！");
//        }
//        PmgBatIrrigateArea pmgBatIrrigateArea = Func.copy(dto, PmgBatIrrigateArea.class);
//        pmgBatIrrigateArea.setCreateOrgUser(AuthUtil.getUser().getOrgUserId());
//        this.save(pmgBatIrrigateArea);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void update(IrrigateAreaUpdateDTO dto) {
//        PmgBatIrrigateArea batIrrigateArea = this.getById(dto.getId());
//        if (Func.isEmpty(batIrrigateArea)){
//            throw new ServiceException("数据不存在！");
//        }
//        int count = this.count(new LambdaQueryWrapper<PmgBatIrrigateArea>()
//                .eq(PmgBatIrrigateArea::getParkId, dto.getParkId())
//                .eq(PmgBatIrrigateArea::getIrrigateAreaCode, dto.getIrrigateAreaCode()).ne(PmgBatIrrigateArea::getId,dto
//                .getId()));
//        if (count>0){
//            throw new ServiceException("区域编码已经存在！");
//        }
//        Func.copy(dto, batIrrigateArea);
//        this.updateById(batIrrigateArea);
//    }
//
//    @Override
//    public List<IrrigateAreaPageVO> getList(Long parkId) {
//        LambdaQueryWrapper<PmgBatIrrigateArea> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(PmgBatIrrigateArea::getParkId,parkId);
//        List<PmgBatIrrigateArea> list = this.list(queryWrapper);
//        if (Func.isEmpty(list)){
//            return new ArrayList<>();
//        }
//        // 组织用户列表
//        List<Long> orgUserIdList = list.stream().map(PmgBatIrrigateArea::getCreateOrgUser).collect(Collectors.toList());
//        List<Long> parkIdList =list.stream().map(PmgBatIrrigateArea::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 -> {
//            IrrigateAreaPageVO irrigateAreaPageVO = Func.copy(e, IrrigateAreaPageVO.class);
//            irrigateAreaPageVO.setCreateOrgUserName(orgUserMap.get(e.getCreateOrgUser()));
//            irrigateAreaPageVO.setParkName(parkMap.get(e.getParkId()));
//            if (Func.isNotEmpty(irrigateAreaPageVO.getGreenPlantsId())){
//                List<PmgBatIrrigateGreenPlants> pmgBatIrrigateGreenPlants = irrigateGreenPlantsService.listByIds(Func.toLongList(e.getGreenPlantsId()));
//                if (Func.isNotEmpty(pmgBatIrrigateGreenPlants)){
//                    irrigateAreaPageVO.setGreenPlantsName(pmgBatIrrigateGreenPlants.stream().map(PmgBatIrrigateGreenPlants::getGreenPlantsName).collect(Collectors.joining("、")));
//                }
//            }
//            return irrigateAreaPageVO;
//        }).collect(Collectors.toList());
//    }
//}
