package com.bonus.zlpt.equip.service.impl;

import java.util.*;
import java.util.stream.Collectors;


import com.bonus.zlpt.equip.api.domain.DevInfo;
import com.bonus.zlpt.equip.api.domain.SysFileInfo;
import com.bonus.zlpt.equip.api.domain.dto.InforMationDto;
import com.bonus.zlpt.equip.api.domain.vo.DevInfoVo;

import com.bonus.zlpt.common.core.domain.system.SysFile;
import com.bonus.zlpt.common.core.utils.DateUtils;
import org.springframework.beans.BeanUtils;

import com.bonus.zlpt.equip.mapper.SysFileInfoMapper;
import org.springframework.stereotype.Service;
import com.bonus.zlpt.equip.mapper.DevInfoMapper;
import com.bonus.zlpt.equip.service.IDevInfoService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 设备信息Service业务层处理
 * 
 * @author xsheng
 * @date 2023-12-02
 */
@Service
public class DevInfoServiceImpl implements IDevInfoService 
{
    private final Integer ASPECT_PICTURE = 20;
    private final Integer EXAMINATION_PDF = 28;
    private final Integer INSURANCE_PDF = 29;

    @Resource
    private DevInfoMapper devInfoMapper;


    @Resource
    private SysFileInfoMapper sysFileInfoMapper;

    /**
     * 查询设备信息
     * 
     * @param maId 设备信息主键
     * @return 设备信息
     */
    @Override
    public DevInfoVo selectDevInfoByMaId(Long maId)
    {
        DevInfoVo devInfoVo = devInfoMapper.selectDevInfoByMaId(maId);
        List<SysFile> files = devInfoMapper.getFilesByMaId(maId);
        if (!CollectionUtils.isEmpty(files) && files.get(0) != null) {
            List<String> picList = new ArrayList();
            for (SysFile file : files) {
                if (file.getDicId() == ASPECT_PICTURE) {
                    picList.add(file.getUrl());
                }
                if (file.getDicId() == EXAMINATION_PDF) {
                    devInfoVo.setExaminationPdf(file.getUrl());
                }
                if (file.getDicId() == INSURANCE_PDF) {
                    devInfoVo.setInsurancePdf(file.getUrl());
                }
            }
            String[] pictureArray = null;
            if (!CollectionUtils.isEmpty(picList)) {
                pictureArray = picList.toArray(new String[picList.size()]);
            }
            devInfoVo.setPictures(pictureArray);
        }
        Long companyUpNum = devInfoMapper.getCompanyUpNum(devInfoVo.getOwnCo());
        devInfoVo.setCompanyUpNum(companyUpNum);
        return devInfoVo;
    }

    /**
     * 查询设备信息列表
     * 
     * @param devInfo 设备信息
     * @return 设备信息
     */
    @Override
    public List<DevInfoVo> selectDevInfoList(DevInfoVo devInfo)
    {
        return devInfoMapper.selectDevInfoList(devInfo);
    }

    @Override
    public List<DevInfoVo> selectDevInfoHotList(DevInfoVo devInfo)
    {
        return devInfoMapper.selectDevInfoHotList(devInfo);
    }

    /**
     * 新增设备信息
     * 
     * @param devInfo 设备信息
     * @return 结果
     */
    @Override
    public int insertDevInfo(DevInfo devInfo)
    {
        devInfo.setCreateTime(DateUtils.getNowDate());
        //把文件保存到附件中
        List<SysFileInfo>  fileInfoList = devInfo.getFileList();
        if (fileInfoList.size()>0){
            for (SysFileInfo sysFileInfo: fileInfoList) {
                sysFileInfoMapper.insertFileInfo(sysFileInfo);
            }
        }
        return devInfoMapper.insertDevInfo(devInfo);
    }

    /**
     * 修改设备信息
     * 
     * @param devInfo 设备信息
     * @return 结果
     */
    @Override
    public int updateDevInfo(DevInfo devInfo)
    {
        devInfo.setUpdateTime(DateUtils.getNowDate());
        return devInfoMapper.updateDevInfo(devInfo);
    }

    /**
     * 批量删除设备信息
     * 
     * @param maIds 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDevInfoByMaIds(Long[] maIds)
    {
        return devInfoMapper.deleteDevInfoByMaIds(maIds);
    }

    /**
     * 删除设备信息信息
     * 
     * @param maId 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDevInfoByMaId(Long maId)
    {
        return devInfoMapper.deleteDevInfoByMaId(maId);
    }

    /**
     * 信息录入
     * @param inforMationDto
     * @return
     */
    @Override
    public int insertInforMationDto(InforMationDto inforMationDto) {
        DevInfo devInfo = new DevInfo();
        BeanUtils.copyProperties(inforMationDto, devInfo);
        devInfoMapper.insertDevInfo(devInfo);
        return devInfoMapper.insertLon(inforMationDto);
    }


    /**
     * 统计装备
     * @return
     */
    @Override
    public Map<String, Integer> sumType() {
        DevInfoVo devInfoVo = new DevInfoVo();
        Map<String, Integer> sumTypeMap = new IdentityHashMap<>();
        //获取所有的装备信息
        List<DevInfoVo> devInfoList = devInfoMapper.selectDevInfoList(devInfoVo);
        //获取每种状态列表
        Map<String, List<DevInfo>> groupedByMaStatus = devInfoList.stream()
                .collect(Collectors.groupingBy(DevInfo::getMaStatus));
        //获取所有的key
        Set<String> keys = groupedByMaStatus.keySet();
        //根据key计算每种状态的数量
        for (String key : keys) {
            List<DevInfo> DevInfoList =  groupedByMaStatus.get(key);
            sumTypeMap.put(key,DevInfoList.size());
        }
        return sumTypeMap;
    }
}
