package org.qinan.safetyeval.app.executor;

import org.qinan.safetyeval.client.api.OrgEquipmentApi;
import org.qinan.safetyeval.client.dto.PageResponse;
import org.qinan.safetyeval.client.dto.SingleResponse;
import org.qinan.safetyeval.client.co.OrgEquipmentCO;
import org.qinan.safetyeval.client.dto.OrgEquipmentAddCmd;
import org.qinan.safetyeval.client.dto.OrgEquipmentModifyCmd;
import org.qinan.safetyeval.client.dto.OrgEquipmentPageQuery;
import org.qinan.safetyeval.domain.entity.OrgEquipmentEntity;
import org.qinan.safetyeval.domain.query.OrgEquipmentQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.domain.service.OrgEquipmentDomainService;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 装备信息执行器（App层）
 *
 * @author safety-eval
 */
@Service
public class OrgEquipmentExecutor implements OrgEquipmentApi {

    @Lazy
    @Autowired(required = false)
    private OrgEquipmentDomainService orgEquipmentDomainService;

    @Override
    public SingleResponse<OrgEquipmentCO> add(OrgEquipmentAddCmd cmd) {
        OrgEquipmentEntity entity = new OrgEquipmentEntity();
        entity.setDeviceName(cmd.getDeviceName());
        entity.setDeviceModel(cmd.getDeviceModel());
        entity.setInstrumentTypeCode(cmd.getInstrumentTypeCode());
        entity.setInstrumentTypeName(cmd.getInstrumentTypeName());
        entity.setDeviceTypeCode(cmd.getDeviceTypeCode());
        entity.setDeviceTypeName(cmd.getDeviceTypeName());
        entity.setManufacturer(cmd.getManufacturer());
        entity.setFlowDesc(cmd.getFlowDesc());
        entity.setMinFlow(cmd.getMinFlow());
        entity.setMaxFlow(cmd.getMaxFlow());
        entity.setCalibrationUnit(cmd.getCalibrationUnit());
        entity.setCalibrationInitValue(cmd.getCalibrationInitValue());
        entity.setFieldCalibrationTypeCode(cmd.getFieldCalibrationTypeCode());
        entity.setFieldCalibrationTypeName(cmd.getFieldCalibrationTypeName());
        entity.setDualChannelFlag(cmd.getDualChannelFlag());
        entity.setEnableFlag(cmd.getEnableFlag());

        OrgEquipmentEntity result = orgEquipmentDomainService.add(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<OrgEquipmentCO> get(Long id) {
        OrgEquipmentEntity entity = orgEquipmentDomainService.get(id);
        return SingleResponse.success(toCO(entity));
    }

    @Override
    public SingleResponse<OrgEquipmentCO> modify(OrgEquipmentModifyCmd cmd) {
        OrgEquipmentEntity entity = new OrgEquipmentEntity();
        entity.setId(cmd.getId());
        entity.setDeviceName(cmd.getDeviceName());
        entity.setDeviceModel(cmd.getDeviceModel());
        entity.setInstrumentTypeCode(cmd.getInstrumentTypeCode());
        entity.setInstrumentTypeName(cmd.getInstrumentTypeName());
        entity.setDeviceTypeCode(cmd.getDeviceTypeCode());
        entity.setDeviceTypeName(cmd.getDeviceTypeName());
        entity.setManufacturer(cmd.getManufacturer());
        entity.setFlowDesc(cmd.getFlowDesc());
        entity.setMinFlow(cmd.getMinFlow());
        entity.setMaxFlow(cmd.getMaxFlow());
        entity.setCalibrationUnit(cmd.getCalibrationUnit());
        entity.setCalibrationInitValue(cmd.getCalibrationInitValue());
        entity.setFieldCalibrationTypeCode(cmd.getFieldCalibrationTypeCode());
        entity.setFieldCalibrationTypeName(cmd.getFieldCalibrationTypeName());
        entity.setDualChannelFlag(cmd.getDualChannelFlag());
        entity.setEnableFlag(cmd.getEnableFlag());

        OrgEquipmentEntity result = orgEquipmentDomainService.modify(entity);
        return SingleResponse.success(toCO(result));
    }

    @Override
    public SingleResponse<Void> delete(Long id) {
        orgEquipmentDomainService.delete(id);
        return SingleResponse.success();
    }

    @Override
    public PageResponse<OrgEquipmentCO> page(OrgEquipmentPageQuery query) {
        OrgEquipmentQuery domainQuery = new OrgEquipmentQuery();
        domainQuery.setPageNum(query.getCurrent());
        domainQuery.setPageSize(query.getSize());
        domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
        domainQuery.setOrgId(query.getOrgId());
        domainQuery.setDeviceName(query.getDeviceName());
        domainQuery.setInstrumentType(query.getInstrumentType());
        domainQuery.setDeviceType(query.getDeviceType());
        domainQuery.setEnableFlag(query.getEnableFlag());

        PageResult<OrgEquipmentEntity> pageResult = orgEquipmentDomainService.page(domainQuery);

        return PageResponse.of(
                pageResult.getRecords().stream()
                        .map(this::toCO)
                        .collect(java.util.stream.Collectors.toList()),
                pageResult.getTotal());
    }

    private OrgEquipmentCO toCO(OrgEquipmentEntity entity) {
        if (entity == null) {
            return null;
        }
        OrgEquipmentCO co = new OrgEquipmentCO();
        co.setId(entity.getId());
        co.setDeviceName(entity.getDeviceName());
        co.setDeviceModel(entity.getDeviceModel());
        co.setInstrumentTypeCode(entity.getInstrumentTypeCode());
        co.setInstrumentTypeName(entity.getInstrumentTypeName());
        co.setDeviceTypeCode(entity.getDeviceTypeCode());
        co.setDeviceTypeName(entity.getDeviceTypeName());
        co.setManufacturer(entity.getManufacturer());
        co.setFlowDesc(entity.getFlowDesc());
        co.setMinFlow(entity.getMinFlow());
        co.setMaxFlow(entity.getMaxFlow());
        co.setCalibrationUnit(entity.getCalibrationUnit());
        co.setCalibrationInitValue(entity.getCalibrationInitValue());
        co.setFieldCalibrationTypeCode(entity.getFieldCalibrationTypeCode());
        co.setFieldCalibrationTypeName(entity.getFieldCalibrationTypeName());
        co.setDualChannelFlag(entity.getDualChannelFlag());
        co.setEnableFlag(entity.getEnableFlag());
        co.setTenantId(entity.getTenantId());
        return co;
    }
}
