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.deviceProduct.DeviceProductModel;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductParam;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductSearch;
import com.zhongshu.card.client.model.org.OrganizationSimpleModel;
import com.zhongshu.card.server.core.dao.devices.DeviceProductBindDeviceDao;
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.org.Organization;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.org.OrganizationServiceImpl;
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;

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

    @Autowired
    private DeviceProductDao deviceProductDao;

    @Autowired
    private DeviceProductBindDeviceDao deviceProductBindDeviceDao;

    @Autowired
    private DeviceProductProvideServeDao deviceProductProvideServeDao;

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private OrganizationServiceImpl organizationService;

    public ResultContent saveInfo(DeviceProductParam param) {
        if (StringUtils.isEmpty(param.getProjectOid())) {
            return ResultContent.buildFail("projectOid不能为空");
        }

        // 产品绑定的机构信息
        Organization organization = null;
        if (StringUtils.isNotEmpty(param.getBeLongOid())) {
            organization = organizationDao.findTopByOid(param.getBeLongOid());
            if (ObjectUtils.isEmpty(organization)) {
                return ResultContent.buildFail("所属机构不存在");
            }
        }

        DeviceProduct product = null;
        // 是否有同名的产品
        DeviceProduct nameTemp = deviceProductDao.findTopByNameAndProjectOid(param.getName(), param.getProjectOid());
        if (StringUtils.isNotEmpty(param.getId())) {
            product = deviceProductDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(product)) {
                return ResultContent.buildFail("数据不存在");
            }
            if (ObjectUtils.isNotEmpty(nameTemp) && !nameTemp.getId().equals(product.getId())) {
                return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIST, product.getName()));
            }
            initUpdateEntity(product);
        } else {
            if (ObjectUtils.isNotEmpty(nameTemp)) {
                return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIST, product.getName()));
            }
            product = new DeviceProduct();
            initEntityNoCheckOid(product);
        }
        BeanUtils.copyProperties(param, product);
        product.setOid(param.getProjectOid());

        product.setOrganization(organization);
        product.setBeLongOid(organization.getOid());
        product.setBeLongName(organization.getName());
        deviceProductDao.save(product);
        return ResultContent.buildSuccess();
    }

    public ResultContent<Page<DeviceProductModel>> page(DeviceProductSearch param, Pageable pageable) {
        // 完善所属项目
        String projectOid = param.getProjectOid();
        if (StringUtils.isNotEmpty(projectOid)) {
            Organization organization = organizationDao.findTopByOid(projectOid);
            if (ObjectUtils.isEmpty(organization)) {
                return ResultContent.buildFail("projectOid不存在");
            }
        }
        Page<DeviceProduct> page = deviceProductDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    public ResultContent<DeviceProductModel> getInfo(String id) {
        DeviceProduct entity = deviceProductDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        DeviceProductModel model = toModel(entity);
        return ResultContent.buildSuccess(model);
    }

    public ResultContent deleteInfo(String id) {
        DeviceProduct entity = deviceProductDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        deviceProductDao.delete(entity);
        deviceProductBindDeviceDao.deleteByDeviceProductId(id);
        deviceProductProvideServeDao.deleteByDeviceProductId(id);
        return ResultContent.buildSuccess();
    }

    /**
     * 更新数量
     *
     * @param id
     * @return
     */
    public ResultContent updateNumber(String id) {
        if (StringUtils.isNotEmpty(id)) {
            DeviceProduct entity = deviceProductDao.findTopById(id);
            updateNumber(entity);
        }
        return ResultContent.buildSuccess();
    }

    public ResultContent updateNumber(DeviceProduct entity) {
        if (ObjectUtils.isNotEmpty(entity)) {
            String id = entity.getId();
            Long number = deviceProductBindDeviceDao.countByDeviceProductId(id);
            Long serviceNumber = deviceProductProvideServeDao.countByDeviceProductId(id);
            entity.setNumber(number);
            entity.setServiceNumber(serviceNumber);
            deviceProductDao.save(entity);
        }
        return ResultContent.buildSuccess();
    }

    public DeviceProductModel toModel(DeviceProduct entity) {
        DeviceProductModel model = new DeviceProductModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);

            OrganizationSimpleModel simpleModel = organizationService.toSimpleModel(entity.getOrganization());
            if (ObjectUtils.isNotEmpty(simpleModel)) {
                model.setBeLongOid(simpleModel.getOid());
                model.setBeLongName(simpleModel.getName());
            }
            model.setOrganization(simpleModel);
        }
        return model;
    }
}
