package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomAllocationRosterCommentMapper;
import com.crm.manager.dao.repository.CustomAllocationRosterCommentRepository;
import com.crm.manager.service.CustomAllocationRosterCommentService;
import com.crm.manager.service.CustomAllocationRosterService;
import com.crm.rely.backend.core.dto.custom.allocation.roster.comment.CustomAllocationRosterCommentDto;
import com.crm.rely.backend.core.entity.custom.allocation.roster.comment.CustomAllocationRosterCommentAddEntity;
import com.crm.rely.backend.core.entity.custom.allocation.roster.comment.CustomAllocationRosterCommentSearchEntity;
import com.crm.rely.backend.core.pojo.table.CustomAllocationRosterCommentTable;
import com.crm.rely.backend.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author houn
 * @Date 2023/2/20 12:36
 * @PackageName:com.crm.manager.service.impl
 * @ClassName: CustomAllocationRosterServiceImpl
 * @Description: TODO
 */
@Slf4j
@Service
class CustomAllocationRosterCommentServiceImpl implements CustomAllocationRosterCommentService {
    @Autowired
    private CustomAllocationRosterCommentMapper dao;

    @Autowired
    private CustomAllocationRosterCommentRepository repository;

    @Autowired
    private CustomAllocationRosterService customAllocationRosterService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CustomAllocationRosterCommentAddEntity entity) throws ServiceException {

        CustomAllocationRosterCommentTable rosterCommentTable = new CustomAllocationRosterCommentTable();
        BeanUtils.copyProperties(entity, rosterCommentTable);
        rosterCommentTable.setId(null);

        repository.save(rosterCommentTable);

        customAllocationRosterService.updateFollowTagById(rosterCommentTable.getAllocationRosterId());

    }

    @Override
    public List<CustomAllocationRosterCommentDto> selectList(CustomAllocationRosterCommentSearchEntity
                                                                     searchPageList) throws ServiceException {

        List<CustomAllocationRosterCommentDto> dtos = getDto(searchPageList);

        return dtos;
    }

    @Override
    public int selectCount(CustomAllocationRosterCommentSearchEntity searchCount) {
        return dao.selectCount(searchCount);
    }

    private List<CustomAllocationRosterCommentDto> getDto(CustomAllocationRosterCommentSearchEntity search) {
        List<CustomAllocationRosterCommentTable> comments = dao.selectList(search);
        if (comments == null || comments.size() <= 0) {
            return null;
        }
        List<CustomAllocationRosterCommentDto> dtos = new ArrayList<>(comments.size());
        for (CustomAllocationRosterCommentTable comment : comments) {
            CustomAllocationRosterCommentDto dto = transform(comment);
            dtos.add(dto);
        }
        return dtos;
    }

    private CustomAllocationRosterCommentDto transform(CustomAllocationRosterCommentTable Comment) {
        CustomAllocationRosterCommentDto dto = new CustomAllocationRosterCommentDto();
        BeanUtils.copyProperties(Comment, dto);
        return dto;
    }
}
