package com.inspur.service.impl;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.inspur.common.utils.DateUtils;
import com.inspur.domain.ElevatorInfo;
import com.inspur.domain.ElevatorPersonInfo;
import com.inspur.mapper.ElevatorInfoMapper;
import com.inspur.mapper.ElevatorPersonInfoMapper;
import com.inspur.service.IElevatorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 电梯信息Service业务层处理
 *
 * @author inspur
 * @date 2024-10-12
 */
@Service
public class ElevatorInfoServiceImpl implements IElevatorInfoService
{
    @Autowired
    private ElevatorInfoMapper elevatorInfoMapper;

    @Autowired
    private ElevatorPersonInfoMapper elevatorPersonInfoMapper;

    /**
     * 查询电梯信息
     *
     * @param id 电梯信息主键
     * @return 电梯信息
     */
    @Override
    public ElevatorInfo selectElevatorInfoById(Long id)
    {
        ElevatorInfo elevatorInfo = elevatorInfoMapper.selectElevatorInfoById(id);
        if (null == elevatorInfo) {
            return elevatorInfo;
        }
        // 增加查询人员信息
        String elevatorId = elevatorInfo.getDeviceId();
        List<ElevatorPersonInfo> personInfos = elevatorPersonInfoMapper.selectElevatorPersonInfoByElevatorId(elevatorId);
        elevatorInfo.setPersonInfos(personInfos);
        return elevatorInfo;
    }

    /**
     * 查询电梯信息列表
     *
     * @param elevatorInfo 电梯信息
     * @return 电梯信息
     */
    @Override
    public List<ElevatorInfo> selectElevatorInfoList(ElevatorInfo elevatorInfo)
    {
        return elevatorInfoMapper.selectElevatorInfoList(elevatorInfo);
    }

    /**
     * 新增电梯信息
     *
     * @param elevatorInfo 电梯信息
     * @return 结果
     */
    @Override
    public int insertElevatorInfo(ElevatorInfo elevatorInfo)
    {
        elevatorInfo.setCreateTime(DateUtils.getNowDate());
        elevatorInfo.setDeviceId(UUID.randomUUID().toString().replace("-",""));
        int result = elevatorInfoMapper.insertElevatorInfo(elevatorInfo);
        List<ElevatorPersonInfo> personInfos = elevatorInfo.getPersonInfos();
        if (CollectionUtils.isEmpty(personInfos)) {
            return result;
        }
        for (ElevatorPersonInfo personInfo : personInfos) {
            personInfo.setElevatorId(elevatorInfo.getDeviceId());
            personInfo.setUnitName(elevatorInfo.getUnitName());
            personInfo.setSafeCode(elevatorInfo.getSafeCode());
            personInfo.setPersonType(2L);
            personInfo.setCreateTime(DateUtils.getNowDate());
            elevatorPersonInfoMapper.insertElevatorPersonInfo(personInfo);
        }
        return result;
    }

    /**
     * 修改电梯信息
     *
     * @param elevatorInfo 电梯信息
     * @return 结果
     */
    @Override
    public int updateElevatorInfo(ElevatorInfo elevatorInfo)
    {
        // 前端传递的personInfos
        List<ElevatorPersonInfo> personInfos = elevatorInfo.getPersonInfos();
        if (CollectionUtils.isEmpty(personInfos)) {
            elevatorPersonInfoMapper.deleteElevatorPersonInfoByDeviceId(elevatorInfo.getDeviceId());
        }else {
            // 如果person Id为空 为新增
            List<ElevatorPersonInfo> addPersons = personInfos.stream()
                    .filter(personInfo -> personInfo.getId() == null).collect(Collectors.toList());
            // 如果person Id不为空 为更新
            List<ElevatorPersonInfo> updatePersons = personInfos.stream()
                    .filter(personInfo -> personInfo.getId() != null).collect(Collectors.toList());
            // 新增人员信息
            if (!CollectionUtils.isEmpty(addPersons)) {
                for (ElevatorPersonInfo personInfo : addPersons) {
                    personInfo.setElevatorId(elevatorInfo.getDeviceId());
                    personInfo.setUnitName(elevatorInfo.getUnitName());
                    personInfo.setSafeCode(elevatorInfo.getSafeCode());
                    personInfo.setPersonType(2L);
                    personInfo.setCreateTime(DateUtils.getNowDate());
                    elevatorPersonInfoMapper.insertElevatorPersonInfo(personInfo);
                }
            }
            // 更新人员信息
            if (!CollectionUtils.isEmpty(updatePersons)) {
                for (ElevatorPersonInfo personInfo : updatePersons) {
                    elevatorPersonInfoMapper.updateElevatorPersonInfo(personInfo);
                }
            }
        }

        return elevatorInfoMapper.updateElevatorInfo(elevatorInfo);
    }

    /**
     * 批量删除电梯信息
     *
     * @param ids 需要删除的电梯信息主键
     * @return 结果
     */
    @Override
    public int deleteElevatorInfoByIds(Long[] ids)
    {
        return elevatorInfoMapper.deleteElevatorInfoByIds(ids);
    }

    /**
     * 删除电梯信息信息
     *
     * @param id 电梯信息主键
     * @return 结果
     */
    @Override
    public int deleteElevatorInfoById(Long id)
    {
        return elevatorInfoMapper.deleteElevatorInfoById(id);
    }
}
