package com.izouma.uwip.service;

import cn.hutool.core.util.ObjectUtil;
import com.izouma.uwip.domain.Handle;
import com.izouma.uwip.domain.LogoPatent;
import com.izouma.uwip.domain.Partner;
import com.izouma.uwip.dto.PageQuery;
import com.izouma.uwip.enums.*;
import com.izouma.uwip.exception.BusinessException;
import com.izouma.uwip.repo.LogoPatentRepo;
import com.izouma.uwip.repo.PartnerRepo;
import com.izouma.uwip.utils.JpaUtils;
import com.izouma.uwip.utils.ObjUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
@AllArgsConstructor
public class LogoPatentService {

    private final LogoPatentRepo    logoPatentRepo;
    private final AttachmentService attachmentService;
    private final PartnerService    partnerService;

    public Page<LogoPatent> all(PageQuery pageQuery) {
        return logoPatentRepo.findAll(JpaUtils.toSpecification(pageQuery, LogoPatent.class), JpaUtils.toPageRequest(pageQuery));
    }

    public Handle saveHandle(LogoWorkflow workflow, Long userId) {
        return Handle.builder()
                .logoWorkflow(workflow)
                .userId(userId)
                .checkAt(LocalDateTime.now())
                .build();
    }

    public LogoPatent save(LogoPatent record, Long userId) {
        if (record.getId() != null) {
            LogoWorkflow workflow = record.getLogoWorkflow();
            if (ObjectUtil.isNull(workflow)) {
                throw new BusinessException("流程为空");
            }
            LogoPatent orig = logoPatentRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            // 保存附件
            if (CollectionUtils.isNotEmpty(record.getAttachments())) {
                attachmentService.batchSave(record.getAttachments(), userId, record.getId());
            }
            ObjUtils.merge(orig, record);
//            orig.getHandle().add(this.saveHandle(workflow, userId));
            if (ObjectUtil.isNull(record.getApplyStatus()) || !ApplyStatus.COMPLETED.equals(record.getApplyStatus())) {
                orig.setApplyStatus(this.getApplyStatus(workflow));
            }
            return logoPatentRepo.save(orig);
        }
        if (record.getSort() == 0) {
            record.setSort(logoPatentRepo.findMax());
        }
        record.setUwNo(partnerService.getUwNo(record.getClientPartnerId(), CaseType.LOGO, CaseStage.DOMESTIC, record.getSort()));
        record.setLogoWorkflow(LogoWorkflow.IS_CONTRACT);
        record.setApplyStatus(ApplyStatus.APPLY_STAGE);
        record = logoPatentRepo.save(record);
        // 保存附件
        if (CollectionUtils.isNotEmpty(record.getAttachments())) {
            attachmentService.batchSave(record.getAttachments(), userId, record.getId());
        }
        return record;
    }

    public ApplyStatus getApplyStatus(LogoWorkflow logoWorkflow) {
        switch (logoWorkflow) {
            case IS_CONTRACT://签约
            case TO_BE_MAINTAINED://供应商
            case PENDING://受理
            case PALINDROME_CORRECTION://回文
            case PENDING_PAYMENT://缴费
                return ApplyStatus.APPLY_STAGE;//申请阶段
            case PENDING_REVIEW://待审查
            case DISMISS://驳回
            case REVIEW_MATERIALS://复审材料
            case REVIEW_PAYMENT://复审缴费
            case REVIEW://复审是否通过
            case ANNOUNCEMENTS://公告初审
                return ApplyStatus.REVIEW_STAGE;//审查阶段
            case HANDLE://证件
                return ApplyStatus.GRANT_STAGE;//授权阶段
            default://不缴费/不复审
                return ApplyStatus.COMPLETED;
        }
    }
}
