package com.jwipc.sys.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.sys.convert.DigitalSealBusinessDetailConvert;
import com.jwipc.sys.dto.DigitalSealBusinessDetailDTO;
import com.jwipc.sys.entity.DigitalSealBusinessDetail;
import com.jwipc.sys.logic.DigitalSealBusinessDetailLogic;
import com.jwipc.sys.service.IDigitalSealBusinessDetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author shenyang.ou
 * @date 2024/5/10 15:54
 * @desc
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DigitalSealBusinessDetailLogicImpl implements DigitalSealBusinessDetailLogic {
    private final IDigitalSealBusinessDetailService digitalSealBusinessDetailService;
    private final DigitalSealBusinessDetailConvert digitalSealBusinessDetailConvert;
    @Override
    public Integer getLimitSignStatus(Long businessId) {
        DigitalSealBusinessDetail detail = digitalSealBusinessDetailService.getOne(Wrappers.lambdaQuery(DigitalSealBusinessDetail.class).eq(DigitalSealBusinessDetail::getBusinessId,businessId).orderByAsc(DigitalSealBusinessDetail::getSignStatus).last(" limit 1"));
        return ObjectUtil.isNotNull(detail)?detail.getSignStatus():null;
    }

    @Override
    public void save(DigitalSealBusinessDetailDTO paramClientDTO) {
        DigitalSealBusinessDetail detail = digitalSealBusinessDetailConvert.DTO2Entity2(paramClientDTO);
        digitalSealBusinessDetailService.save(detail);
    }

    @Override
    public boolean checkExist(Long businessId, String businessTypeCodes) {
        List<String> businessTypeCodeList =  StrUtil.split(businessTypeCodes, ",");
        DigitalSealBusinessDetail detail = digitalSealBusinessDetailService.getOne(Wrappers.lambdaQuery(DigitalSealBusinessDetail.class).eq(DigitalSealBusinessDetail::getBusinessId,businessId).in(DigitalSealBusinessDetail::getBusinessTypeCode,businessTypeCodeList).ne(DigitalSealBusinessDetail::getSignStatus,7).last(" limit 1"));
        return ObjectUtil.isNotNull(detail);
    }
}
