package com.inspect.metadata.patrolpointmnt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.inspect.metadata.alg.domain.BasedataAlgSubtype;
import com.inspect.metadata.alg.mapper.BasedataAlgSubtypeMapper;
import com.inspect.metadata.patrolpointmnt.domain.BasedataStdPatrolPoint;
import com.inspect.metadata.patrolpointmnt.domain.model.InsertStdModel;
import com.inspect.metadata.patrolpointmnt.mapper.BasedataStdPatrolPointMapper;
import com.inspect.metadata.patrolpointmnt.service.IBasedataStdPatrolPointService;
import com.inspect.metadata.prodevmnt.domain.BasedataDeviceType;
import com.inspect.metadata.prodevmnt.mapper.BasedataDeviceTypeMapper;
import com.inspect.base.core.utils.DateUtils;
import com.inspect.base.core.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

@Service
public class BasedataStdPatrolPointServiceImpl implements IBasedataStdPatrolPointService {
    private final BasedataStdPatrolPointMapper basedataStdpatrolPointMapper;
    final BasedataAlgSubtypeMapper basedataAlgSubtypeMapper;
    final BasedataDeviceTypeMapper basedataDevicetypeMapper;

    public BasedataStdPatrolPointServiceImpl(BasedataStdPatrolPointMapper basedataStdpatrolPointMapper, BasedataAlgSubtypeMapper basedataAlgSubtypeMapper, BasedataDeviceTypeMapper basedataDevicetypeMapper) {
        this.basedataStdpatrolPointMapper = basedataStdpatrolPointMapper;
        this.basedataAlgSubtypeMapper = basedataAlgSubtypeMapper;
        this.basedataDevicetypeMapper = basedataDevicetypeMapper;
    }

    public BasedataStdPatrolPoint selectBasedataStdPatrolPointByStdPatrolPointId(Long stdpatrolPointId) {
        return this.basedataStdpatrolPointMapper.selectBasedataStdPatrolPointByStdPatrolPointId(stdpatrolPointId);
    }

    public List<BasedataStdPatrolPoint> selectBasedataStdpPatrolPointList(BasedataStdPatrolPoint basedataStdpatrolPoint) {
        List<BasedataStdPatrolPoint> list = this.basedataStdpatrolPointMapper.selectBasedataStdPatrolPointList(basedataStdpatrolPoint);
        if (!list.isEmpty()) {
            for (BasedataStdPatrolPoint stdPatrolPoint : list) {
                Long componentDeviceTypeId = stdPatrolPoint.getComponentDeviceTypeId();
                if (!StringUtils.isEmpty(stdPatrolPoint.getAlarmThreshold())) {
                    List<BasedataAlgSubtype> defectTypes = this.basedataAlgSubtypeMapper.selectBasedataAlgSubtypeList(new BasedataAlgSubtype());
                    Map<String, String> map = new HashMap<>();
                    defectTypes.forEach((item) -> map.put(item.getAlgSubtypeCode(), item.getAlgSubtypeName()));
                    List<JSONObject> jsonArray = JSONArray.parseArray(stdPatrolPoint.getAlarmThreshold(), JSONObject.class);
                    String algSubtypeNames = jsonArray.stream().map((item) -> map.get(item.getString("code"))).collect(Collectors.joining(","));
                    stdPatrolPoint.setAlgSubtypeName(algSubtypeNames);
                }

                if (null != componentDeviceTypeId) {
                    StringBuilder builder = new StringBuilder();
                    BasedataDeviceType basedataDevicetype = this.basedataDevicetypeMapper.selectBasedataDeviceTypeByDeviceTypeId(componentDeviceTypeId);
                    if (null != basedataDevicetype) {
                        builder.append(basedataDevicetype.getDeviceTypeName());
                        Long parentId = basedataDevicetype.getParentId();
                        if (null != parentId) {
                            this.recursionGetAllPathNameForComp(parentId, builder);
                        }

                        stdPatrolPoint.setComponentDeviceTypeName(builder.toString());
                    }
                }
            }
        }

        return list;
    }

    public int insertBasedataStdPatrolPoint(BasedataStdPatrolPoint basedataStdpatrolPoint) {
        List<BasedataStdPatrolPoint> basedata = this.basedataStdpatrolPointMapper.selectBasedataStdPatrolPointList(basedataStdpatrolPoint);
        if (!basedata.isEmpty()) {
            return 1;
        } else {
            basedataStdpatrolPoint.setCreateTime(DateUtils.getNowDate());
            return this.basedataStdpatrolPointMapper.insertBasedataStdPatrolPoint(basedataStdpatrolPoint);
        }
    }

    public int updateBasedataStdPatrolPoint(BasedataStdPatrolPoint basedataStdpatrolPoint) {
        basedataStdpatrolPoint.setUpdateTime(DateUtils.getNowDate());
        return this.basedataStdpatrolPointMapper.updateBasedataStdPatrolPoint(basedataStdpatrolPoint);
    }

    public int deleteBasedataStdPatrolPointByStdPatrolPointIds(Long[] stdPatrolPointIds) {
        return this.basedataStdpatrolPointMapper.deleteBasedataStdPatrolPointByStdPatrolPointIds(stdPatrolPointIds);
    }

    public int deleteBasedataStdPatrolPointByStdPatrolPointId(Long stdPatrolPointId) {
        return this.basedataStdpatrolPointMapper.deleteBasedataStdPatrolPointByStdPatrolPointId(stdPatrolPointId);
    }

    public int addBasedataStdPatrolPointForList(List<InsertStdModel> list) {
        if (null != list && !list.isEmpty()) {
            List<BasedataStdPatrolPoint> basedataStdPatrolPoints = JSONArray.parseArray(JSONArray.toJSONString(list), BasedataStdPatrolPoint.class);

            for (BasedataStdPatrolPoint stdPatrolPoint : basedataStdPatrolPoints) {
                Long stdPatrolPointId = stdPatrolPoint.getStdPatrolPointId();
                if (null == stdPatrolPointId) {
                    stdPatrolPoint.setUpdateTime(DateUtils.getNowDate());
                    this.basedataStdpatrolPointMapper.insertBasedataStdPatrolPoint(stdPatrolPoint);
                }

                if (null != stdPatrolPointId) {
                    BasedataStdPatrolPoint basedataStdpatrolPoint = this.basedataStdpatrolPointMapper.selectBasedataStdPatrolPointByStdPatrolPointId(stdPatrolPointId);
                    if (null != basedataStdpatrolPoint) {
                        stdPatrolPoint.setUpdateTime(DateUtils.getNowDate());
                        this.basedataStdpatrolPointMapper.updateBasedataStdPatrolPoint(stdPatrolPoint);
                    }
                }
            }

            return 1;
        } else {
            return 0;
        }
    }

    private void recursionGetAllPathNameForComp(Long parentId, StringBuilder builder) {
        BasedataDeviceType basedataDevice = this.basedataDevicetypeMapper.selectBasedataDeviceTypeByDeviceTypeId(parentId);
        if (null != basedataDevice && null != basedataDevice.getPowerMainEquFlag()) {
            if ("Yes".equals(basedataDevice.getPowerMainEquFlag())) {
                return;
            }

            builder.insert(0, basedataDevice.getDeviceTypeName() + StringUtils.SLASH);
            if (null != basedataDevice.getParentId()) {
                parentId = basedataDevice.getParentId();
                this.recursionGetAllPathNameForComp(parentId, builder);
            }
        }

    }
}
