package com.mortals.xhx.module.device.service.impl;

import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DevicePdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.feign.device.IDeviceFeign;
import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * DeviceService
 * 设备 service实现
 *
 * @author zxfei
 * @date 2022-03-09
 */
@Service("deviceService")
@Slf4j
public class DeviceServiceImpl extends AbstractCRUDServiceImpl<DeviceDao, DeviceEntity, Long> implements DeviceService {
    @Autowired
    private IDeviceFeign deviceFeign;


    @Override
    public Result<DeviceEntity> find(DeviceEntity entity, PageInfo pageInfo, Context context) throws AppException {
        Result<DeviceEntity> result = new Result();
        DevicePdu devicePdu = new DevicePdu();
        BeanUtils.copyProperties(entity, devicePdu, BeanUtil.getNullPropertyNames(entity));

        String productName = GlobalSysInfo.getParamValue(Constant.PARAMS_PRODUCT_NAME, "数字样表机");
        devicePdu.setProductName(productName);

        Rest<RespData<List<DevicePdu>>> rest = deviceFeign.list(devicePdu);
        if (rest.getCode().equals(YesNoEnum.YES.getValue())) {
            List<DeviceEntity> collect = rest.getData().getData().stream().map(item -> {
                DeviceEntity deviceEntity = new DeviceEntity();
                BeanUtils.copyProperties(item, deviceEntity, BeanUtil.getNullPropertyNames(item));
                return deviceEntity;
            }).collect(Collectors.toList());

            result.setList(collect);
            result.setDict(rest.getDict());
        }
        return result;

    }
}