package com.jwipc.sys.logic.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.DictClient;
import com.jwipc.sys.client.dto.DictDTO;
import com.jwipc.sys.convert.AttachmentTypeConfigConvert;
import com.jwipc.sys.dto.AttachmentTypeConfigDTO;
import com.jwipc.sys.entity.AttachmentTypeConfig;
import com.jwipc.sys.entity.BusinessAttachment;
import com.jwipc.sys.logic.AttachmentTypeConfigLogic;
import com.jwipc.sys.service.IAttachmentTypeConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shenyang.ou
 * @date 2025/10/27 11:34
 * @desc
 */
@Service
@RequiredArgsConstructor
public class AttachmentTypeConfigLogicImpl implements AttachmentTypeConfigLogic {
    private final DictClient dictClient;
    private final IAttachmentTypeConfigService attachmentTypeConfigService;
    private final AttachmentTypeConfigConvert attachmentTypeConfigConvert;
    @Override
    public List<AttachmentTypeConfigDTO> list() {
        List<DictDTO> dictDTOS = dictClient.getDicByType("businessFileType");
        List<AttachmentTypeConfig> attachmentTypeConfigs = attachmentTypeConfigService.list();
        if (CollUtil.isNotEmpty(dictDTOS)){
            List<AttachmentTypeConfigDTO> attachmentTypeConfigDTOS = new ArrayList<>();
            dictDTOS.forEach(dictDTO -> {
                boolean ext = false;
                for (AttachmentTypeConfig attachmentTypeConfig : attachmentTypeConfigs){
                    if (dictDTO.getCode().equals(attachmentTypeConfig.getCode())){
                        ext = true;
                        AttachmentTypeConfigDTO attachmentTypeConfigDTO = attachmentTypeConfigConvert.entity2DTO(attachmentTypeConfig);
                        attachmentTypeConfigDTO.setName(dictDTO.getName());
                        attachmentTypeConfigDTOS.add(attachmentTypeConfigDTO);
                    }
                }
                if (!ext){
                    AttachmentTypeConfigDTO attachmentTypeConfigDTO = new AttachmentTypeConfigDTO();
                    attachmentTypeConfigDTO.setCode(dictDTO.getCode());
                    attachmentTypeConfigDTO.setName(dictDTO.getName());
                    attachmentTypeConfigDTOS.add(attachmentTypeConfigDTO);
                }
            });
            return attachmentTypeConfigDTOS;
        }
        return List.of();
    }

    @Override
    public List<AttachmentTypeConfigDTO> listByBusinessTypeCode(String businessTypeCode) {
//        List<AttachmentTypeConfig> attachmentTypeConfigs = attachmentTypeConfigService.list(new QueryWrapper<AttachmentTypeConfig>().eq(businessTypeCode+"_flag", 1));
        List<AttachmentTypeConfig> attachmentTypeConfigs = attachmentTypeConfigService.listByBusinessTypeCode(businessTypeCode+"_flag", 1);
        List<AttachmentTypeConfigDTO> attachmentTypeConfigDTOs = attachmentTypeConfigConvert.entity2DTOList(attachmentTypeConfigs);
        attachmentTypeConfigDTOs.forEach(attachmentTypeConfigDTO -> {
            attachmentTypeConfigDTO.setName(dictClient.getDictByTypeAndCode("businessFileType", attachmentTypeConfigDTO.getCode()).getName());
        });
        return attachmentTypeConfigDTOs;
    }

    @Override
    public List<AttachmentTypeConfigDTO> listNeedCheckByBusinessTypeCode(String businessTypeCode, String checkCode) {
        List<AttachmentTypeConfig> attachmentTypeConfigs = attachmentTypeConfigService.listNeedCheckByBusinessTypeCode(
                businessTypeCode+"_flag", 1,
                checkCode + "_flag", 1);
        List<AttachmentTypeConfigDTO> attachmentTypeConfigDTOs = attachmentTypeConfigConvert.entity2DTOList(attachmentTypeConfigs);
        attachmentTypeConfigDTOs.forEach(attachmentTypeConfigDTO -> {
            attachmentTypeConfigDTO.setName(dictClient.getDictByTypeAndCode("businessFileType", attachmentTypeConfigDTO.getCode()).getName());
        });
        return attachmentTypeConfigDTOs;
    }

    @Override
    public void saveOrUpdate(List<AttachmentTypeConfig> saveList) {
        List<AttachmentTypeConfig> attachmentTypeConfigs = attachmentTypeConfigService.list();
        saveList.forEach(dictDTO -> {
            boolean ext = false;
            if (CollUtil.isNotEmpty(attachmentTypeConfigs)){
                for (AttachmentTypeConfig attachmentTypeConfig : attachmentTypeConfigs){
                    if (dictDTO.getCode().equals(attachmentTypeConfig.getCode())){
                        ext = true;
                        dictDTO.setId(attachmentTypeConfig.getId());
                        dictDTO.setUpdateTime(LocalDateTime.now());
                        attachmentTypeConfigService.updateById(dictDTO);
                    }
                }
            }
            if (!ext){
                dictDTO.setCreateTime(LocalDateTime.now());
                dictDTO.setCreateUser(LoginUtil.getUserId());
                attachmentTypeConfigService.save(dictDTO);
            }
        });
    }
}
