package com.inkasso.factoring.system.suggestion.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.util.BeanUtilsProxy;
import com.inkasso.adapter.web.LoginUserHolder;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.system.suggestion.dao.SuggestionDAO;
import com.inkasso.factoring.system.suggestion.entity.SuggestionDO;
import com.inkasso.factoring.system.suggestion.model.dto.SuggestionDTO;
import com.inkasso.factoring.system.suggestion.model.request.AddSuggestionRequest;
import com.inkasso.factoring.system.suggestion.model.request.EditSuggestionRequest;
import com.inkasso.factoring.system.suggestion.service.SuggestionService;
import com.inkasso.adapter.exception.ServiceException;
import com.inkasso.adapter.exception.enums.ExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;

/**
 * @author 张超
 * @date 2023-08-31
 */
@Service
public class SuggestionServiceImpl extends ServiceImpl<SuggestionDAO, SuggestionDO> implements SuggestionService {

    @Autowired
    private FileService fileService;

    @Override
    @Transactional
    public void addSuggestion(AddSuggestionRequest addSuggestionRequest) {
        SuggestionDO suggestion = BeanUtilsProxy.copyPropertiesIgnoreNull(addSuggestionRequest, SuggestionDO.class);
        suggestion.setTenantId(LoginUserHolder.get().getTenantId());
        suggestion.setUserId(LoginUserHolder.get().getId());
        suggestion.setCreateTime(new Date());
        this.baseMapper.insert(suggestion);

        //处理附件信息
        if(!CollectionUtils.isEmpty(addSuggestionRequest.getFileIds())){
            fileService.updateBusinessId(addSuggestionRequest.getFileIds(),suggestion.getId());
        }
    }

    @Override
    public void editSuggestion(EditSuggestionRequest editSuggestionRequest) {

        SuggestionDO suggestion = super.getById(editSuggestionRequest.getId());
        if (suggestion == null) {
            throw new ServiceException(400, ExceptionEnum.SYSTEM_DATE_ERROR);
        }
        BeanUtilsProxy.copyPropertiesNotIgnoreNull(editSuggestionRequest, suggestion);
        this.updateById(suggestion);
    }

    @Override
    public SuggestionDTO getSuggestionDtoById(Long id) {
        SuggestionDO suggestion = super.getById(id);
        SuggestionDTO suggestionDto = BeanUtilsProxy.copyProperties(suggestion, SuggestionDTO.class);
        return suggestionDto;
    }

    @Override
    public void deleteSuggestion(Long id) {

        this.removeById(id);
    }
}

