package com.cf.imes.module.system.api.organ;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.system.api.organ.dto.OrganizationDTO;
import com.cf.imes.module.system.controller.admin.funds.delay.vo.PreviouProductDelayRespVO;
import com.cf.imes.module.system.controller.admin.funds.delay.vo.ProductDelayRespVO;
import com.cf.imes.module.system.dal.dataobject.funds.products.ProductsDO;
import com.cf.imes.module.system.dal.dataobject.funds.purchase.PurchaseRecordDO;
import com.cf.imes.module.system.service.funds.delay.ProductDelayService;
import com.cf.imes.module.system.service.funds.products.ProductsService;
import com.cf.imes.module.system.service.organ.OrganService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.ORG_PRODUCT_EXPIRED;
import static com.cf.imes.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class OrganApiImpl implements OrganApi {

    @Resource
    private OrganService organService;

    @Resource
    private ProductDelayService productDelayService;

    @Resource
    private ProductsService productsService;

    @Override
    public CommonResult<List<Long>> getOrganIdList() {
        return success(organService.getOrganIdList());
    }

    @Override
    public CommonResult<Boolean> validOrgan(Long id) {
        organService.validOrgan(id);
        return success(true);
    }

    @Override
    public CommonResult<OrganizationDTO> getOrganDetails(Long organId) {

        return success(BeanUtils.toBean(organService.validOrgan(organId),OrganizationDTO.class));
    }


    @Override
    public CommonResult<List<OrganizationDTO>> getOrganDetailsByName(String organName) {

        return null;
    }

    @Override
    public CommonResult<Boolean> validOrganProduct(Long organId, Long productId) {
        // 校验产品有效性
        ProductsDO productsDO = productsService.get(productId);

        // 查询组织产品下有效的延期记录
        PreviouProductDelayRespVO recordsByProductId = productDelayService.getActiveRecordsByProductId(productsDO.getId(), organId);
        PurchaseRecordDO previouPurchaseRecord = recordsByProductId.getPreviouPurchaseRecord();
        List<ProductDelayRespVO> delayRespVOList = recordsByProductId.getDelayRespVOList();
        // 当前产品购买记录有效期是否超过当前时间
        boolean productMatch = false;
        if (ObjectUtil.isNotNull(previouPurchaseRecord)) {
            LocalDate endTime = previouPurchaseRecord.getEndTime();
            productMatch = endTime.isAfter(LocalDate.now()) || endTime.isEqual(LocalDate.now());
        }
        // 延期记录中是否有超过当前时间的记录
        boolean delayMatch = false;
        if (CollUtil.isNotEmpty(delayRespVOList)) {
            delayMatch = delayRespVOList.stream()
                    .anyMatch(item -> item.getDelayTime().isAfter(LocalDate.now())
                            || item.getDelayTime().isEqual(LocalDate.now()));
        }
        // 产品过期且没有有效的延期记录时，提示产品已过期
        if (!productMatch && !delayMatch) {
            throw new ServiceException(ORG_PRODUCT_EXPIRED);
        }
        return success(true);
    }
}
