package com.ruoyi.web.modules.trading.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.trading.domain.BizIsolationPatrol;
import com.ruoyi.web.modules.trading.domain.vo.AppDistributorEntryIsolationVo;
import com.ruoyi.web.modules.trading.domain.vo.AppDistributorYakBasicVo;
import com.ruoyi.web.modules.trading.mapper.AppDistributorYakMapper;
import com.ruoyi.web.modules.trading.service.IAppDistributorYakService;
import com.ruoyi.web.modules.trading.support.AppDistributorOrderRules;
import com.ruoyi.web.modules.trading.support.AppDistributorSessionSupport;
import com.ruoyi.web.modules.trading.support.EarTagCodec;

/**
 * 承销商小程序牦牛基本信息。
 */
@Service
public class AppDistributorYakServiceImpl implements IAppDistributorYakService
{
    private static final ZoneId ZONE = ZoneId.systemDefault();

    @Autowired
    private AppDistributorSessionSupport appDistributorSessionSupport;

    @Autowired
    private AppDistributorYakMapper appDistributorYakMapper;

    @Override
    public AppDistributorYakBasicVo selectYakBasic(Long supplierId, String earTag)
    {
        appDistributorSessionSupport.requireDistributorId();
        if (supplierId == null)
        {
            throw new ServiceException(AppDistributorOrderRules.MSG_SUPPLIER_REQUIRED);
        }
        if (StringUtils.isEmpty(earTag))
        {
            throw new ServiceException(AppDistributorOrderRules.MSG_EAR_TAG_REQUIRED);
        }
        List<String> tags = EarTagCodec.parse(earTag.trim());
        if (tags.isEmpty())
        {
            throw new ServiceException(AppDistributorOrderRules.MSG_EAR_TAG_REQUIRED);
        }
        String normalizedTag = tags.get(0);

        AppDistributorEntryIsolationVo entry = appDistributorYakMapper.selectEntryBySupplierAndEarTag(
                supplierId, normalizedTag);
        if (entry == null)
        {
            throw new ServiceException(AppDistributorOrderRules.MSG_YAK_NOT_FOUND);
        }

        List<BizIsolationPatrol> patrols = Collections.emptyList();
        if (entry.getEntryTime() != null && entry.getExpectedReleaseDate() != null)
        {
            Date releaseEnd = toReleaseEnd(entry.getExpectedReleaseDate());
            patrols = appDistributorYakMapper.selectPatrolsDuringEntryIsolation(
                    entry.getId(), entry.getEntryTime(), releaseEnd);
            if (patrols == null)
            {
                patrols = Collections.emptyList();
            }
        }

        AppDistributorYakBasicVo vo = new AppDistributorYakBasicVo();
        vo.setEarTag(normalizedTag);
        vo.setEntryIsolation(entry);
        vo.setPatrolList(patrols);
        return vo;
    }

    private static Date toReleaseEnd(Date expectedReleaseDate)
    {
        LocalDate day = expectedReleaseDate.toInstant().atZone(ZONE).toLocalDate();
        return Date.from(day.atTime(23, 59, 59).atZone(ZONE).toInstant());
    }
}
