package com.ruoyi.project.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.business.domain.TjElevator;
import com.ruoyi.project.business.domain.TjElevatorFloorPara;
import com.ruoyi.project.business.domain.TjElevatorParaDetail;
import com.ruoyi.project.business.domain.vo.Civil.TjElevatorVo;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.mapper.TjElevatorMapper;
import com.ruoyi.project.business.mapper.TjElevatorParaDetailMapper;
import com.ruoyi.project.business.service.ITjElevatorFloorParaService;
import com.ruoyi.project.business.service.ITjElevatorService;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-20
 */
@Service
public class TjElevatorServiceImpl implements ITjElevatorService {

    @Resource
    private TjElevatorMapper tjElevatorMapper;

    @Resource
    private TjElevatorParaDetailMapper tjElevatorParaDetailMapper;


    @Resource
    private ITjElevatorFloorParaService tjElevatorFloorParaService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TjElevatorVo selectTjElevatorById(Long id) {

        // 查询土建出图关联电梯
        List<TjElevatorVo> tjElevators = tjElevatorMapper.selectElevatorListByCivilEngineeringId(null, id);
        if (CollUtil.isNotEmpty(tjElevators)) {
            List<Long> elevatorIds = tjElevators.stream().map(TjElevatorVo::getId).collect(Collectors.toList());
            // 查询土建出图关联电梯的所有参数
            List<TjElevatorParaDetail> detailList = tjElevatorParaDetailMapper.selectByElevatorIds(elevatorIds);
            // 查询关联的楼层参数
            List<TjElevatorFloorPara> floorParaList = tjElevatorFloorParaService.list(
                Wrappers.<TjElevatorFloorPara>lambdaQuery()
                    .in(TjElevatorFloorPara::getElevatorId, elevatorIds)
                    .eq(TjElevatorFloorPara::getDelFlag, DelFlagEnum.NORMAL.getValue())
            );
            // 根据电梯id分组参数
            tjElevators.forEach(elevator -> {
                elevator.setParamList(
                    detailList.stream()
                        .filter(detail -> detail.getElevatorId().equals(elevator.getId()))
                        .collect(Collectors.toList())
                );
                elevator.setFloorParamList(
                    floorParaList.stream()
                        .filter(detail -> detail.getElevatorId().equals(elevator.getId()))
                        .collect(Collectors.toList())
                );
            });
            return tjElevators.get(0);
        }
        return null;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tjElevator 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TjElevator> selectTjElevatorList(TjElevator tjElevator) {
        return tjElevatorMapper.selectTjElevatorList(tjElevator);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tjElevator 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTjElevator(TjElevator tjElevator) {
        tjElevator.setCreateTime(DateUtils.getNowDate());
        return tjElevatorMapper.insertTjElevator(tjElevator);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tjElevator 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTjElevator(TjElevator tjElevator) {
        tjElevator.setUpdateTime(DateUtils.getNowDate());
        return tjElevatorMapper.updateTjElevator(tjElevator);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTjElevatorByIds(Long[] ids) {
        return tjElevatorMapper.deleteTjElevatorByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTjElevatorById(Long id) {
        return tjElevatorMapper.deleteTjElevatorById(id);
    }
}
