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

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.model.devices.deviceGroup.DeviceGroupModel;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductProvideServeModel;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductProvideServeParam;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductProvideServeSearch;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.dao.devices.DeviceProductDao;
import com.zhongshu.card.server.core.dao.devices.DeviceProductProvideServeDao;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.domain.devices.DeviceProduct;
import com.zhongshu.card.server.core.domain.devices.DeviceProductProvideServe;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * @author TRX
 * @date 2025/1/8
 */
@Slf4j
@Service
public class DeviceProductProvideServeService extends SuperService {

    @Autowired
    private DeviceProductDao deviceProductDao;

    @Autowired
    private DeviceProductProvideServeDao deviceProductProvideServeDao;

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private DeviceProductService deviceProductService;

    public ResultContent saveInfo(DeviceProductProvideServeParam param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        DeviceProduct deviceProduct = deviceProductDao.findTopById(param.getDeviceProductId());
        if (ObjectUtils.isEmpty(deviceProduct)) {
            return ResultContent.buildFail("产品不存在");
        }

        DeviceProductProvideServe entity = null;
        if (StringUtils.isNotEmpty(param.getId())) {
            entity = deviceProductProvideServeDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(entity)) {
                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
            }
            initUpdateEntity(entity);
        } else {
            entity = new DeviceProductProvideServe();
            initEntityNoCheckOid(entity);
        }
        BeanUtils.copyProperties(param, entity);
        entity.setDeviceProduct(deviceProduct);
        entity.setDeviceProductId(deviceProduct.getId());
        deviceProductProvideServeDao.save(entity);
        deviceProductService.updateNumber(deviceProduct);
        return ResultContent.buildSuccess();
    }

    public ResultContent<Page<DeviceProductProvideServeModel>> page(DeviceProductProvideServeSearch param, Pageable pageable) {
        String projectOid = param.getProjectOid();
        Organization organization = organizationDao.findTopByOid(projectOid);
        if (ObjectUtils.isEmpty(organization)) {
            return ResultContent.buildFail("projectOid不存在");
        }
        Page<DeviceProductProvideServe> page = deviceProductProvideServeDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    /**
     * 删除服务
     *
     * @param id
     * @return
     */
    public ResultContent deleteInfo(String id) {
        DeviceProductProvideServe entity = deviceProductProvideServeDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        deviceProductProvideServeDao.delete(entity);
        deviceProductService.updateNumber(entity.getDeviceProduct());
        return ResultContent.buildSuccess();
    }

    public ResultContent<DeviceGroupModel> getDetailInfo(String id) {
        DeviceProductProvideServe entity = deviceProductProvideServeDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        return ResultContent.buildSuccess(toModel(entity));
    }

    public ResultContent changeState(String id, DataState state) {
        DeviceProductProvideServe entity = deviceProductProvideServeDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        entity.setState(state);
        deviceProductProvideServeDao.save(entity);
        return ResultContent.buildSuccess();
    }

    public DeviceProductProvideServeModel toModel(DeviceProductProvideServe entity) {
        DeviceProductProvideServeModel model = new DeviceProductProvideServeModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }
}
