package com.zhongshu.card.server.core.service.devices;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.models.device.GateWaySyncParam;
import com.zhongshu.card.client.model.devices.GateWayInfoModel;
import com.zhongshu.card.client.model.devices.GateWayInfoParam;
import com.zhongshu.card.client.model.devices.GateWayInfoSearch;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.service.school.GateWayInfoService;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.school.AreaDao;
import com.zhongshu.card.server.core.dao.devices.GateWayInfoDao;
import com.zhongshu.card.server.core.domain.school.Area;
import com.zhongshu.card.server.core.domain.devices.GateWayInfo;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.org.OrganizationServiceImpl;
import com.zhongshu.card.server.core.service.school.AreaServiceImpl;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

/**
 * @author TRX
 * @date 2024/7/2
 */
@Slf4j
@Service
public class GateWayInfoServiceImpl extends SuperService implements GateWayInfoService {

    @Autowired
    GateWayInfoDao gateWayInfoDao;

    @Autowired
    AreaDao areaDao;

    @Autowired
    AreaServiceImpl areaService;

    @Autowired
    private OrganizationServiceImpl organizationServiceImpl;

    @Autowired
    OrganizationDao organizationDao;

    /**
     * 同步网关信息
     *
     * @param param
     * @return
     */
    @Override
    public ResultContent syncGateWayInfo(GateWaySyncParam.GateWaySyncInfo param) {
        Assert.hasText(param.getGateWayId(), "gateWayId不能为空");
        // 设备号
        String gateWayId = param.getGateWayId();
        GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(gateWayId);
        if (ObjectUtils.isNotEmpty(gateWayInfo)) {
        } else {
            gateWayInfo = new GateWayInfo();
            initEntityNoCheckOid(gateWayInfo);
        }
        BeanUtils.copyProperties(param, gateWayInfo, "id");
        gateWayInfo.setProjectInfoName(organizationServiceImpl.getOrgNameByCode(param.getProjectInfoCode()));
        gateWayInfoDao.save(gateWayInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * 修改网管的区域
     *
     * @param param
     * @return
     */
    @Override
    public ResultContent updateGateArea(GateWayInfoParam param) {
        GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(param.getGateWayId());
        if (ObjectUtils.isEmpty(gateWayInfo)) {
            return ResultContent.buildFail(String.format("网关不存在：%s", param.getGateWayId()));
        }
        Area area = areaDao.findTopById(param.getAreaId());
        gateWayInfo.setArea(area);
        gateWayInfoDao.save(gateWayInfo);
        return ResultContent.buildSuccess();
    }

    @Override
    public ResultContent<Page<GateWayInfoModel>> page(GateWayInfoSearch param, Pageable pageable) {
        initOidSearchParamNoCheckOid(param);
        Page<GateWayInfo> page = gateWayInfoDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    @Override
    public ResultContent<GateWayInfoModel> getGateWayInfo(String gateWayId) {
        GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(gateWayId);
        if (ObjectUtils.isEmpty(gateWayInfo)) {
            return ResultContent.buildFail(String.format("网关不存在：%s", gateWayId));
        }
        return ResultContent.buildSuccess(toModel(gateWayInfo));
    }

    @Override
    public ResultContent<List<GateWayInfoModel>> getAllGateWayInfo() {
        List<GateWayInfo> gateWayInfos = gateWayInfoDao.findAll();
        List<GateWayInfoModel> models = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(gateWayInfos)) {
            models = gateWayInfos.stream().map(this::toModel).collect(Collectors.toList());
        }
        return ResultContent.buildSuccess(models);
    }

    public GateWayInfoModel toModel(GateWayInfo entity) {
        GateWayInfoModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new GateWayInfoModel();
            BeanUtils.copyProperties(entity, model);
            // 区域
            model.setArea(areaService.toModel(entity.getArea()));
        }
        return model;
    }

}
