package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomAllocationRosterRecordMapper;
import com.crm.manager.dao.repository.CustomAllocationRosterRecordRepository;
import com.crm.manager.service.CustomAllocationRosterRecordService;
import com.crm.rely.backend.core.dto.custom.allocation.roster.record.CustomAllocationRosterRecordDto;
import com.crm.rely.backend.core.entity.custom.allocation.roster.CustomAllocationRosterClickByCountrysEntity;
import com.crm.rely.backend.core.entity.custom.allocation.roster.CustomAllocationRosterClickEntity;
import com.crm.rely.backend.core.entity.custom.allocation.roster.record.CustomAllocationRosterRecordSearchEntity;
import com.crm.rely.backend.core.pojo.table.CustomAllocationRosterRecordTable;
import com.crm.rely.backend.core.pojo.table.CustomAllocationRosterTable;
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.Date;
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 CustomAllocationRosterRecordServiceImpl implements CustomAllocationRosterRecordService {
    @Autowired
    private CustomAllocationRosterRecordMapper dao;

    @Autowired
    private CustomAllocationRosterRecordRepository repository;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(List<CustomAllocationRosterTable> tables) throws ServiceException {
        if (tables == null) {
            return;
        }
        List<CustomAllocationRosterRecordTable> rosterRecordTables = new ArrayList<>(tables.size());
        for (CustomAllocationRosterTable table : tables) {
            CustomAllocationRosterRecordTable rosterRecordTable = new CustomAllocationRosterRecordTable();
            BeanUtils.copyProperties(table, rosterRecordTable);
            rosterRecordTable.setId(null);
            rosterRecordTable.setAllocationRosterId(table.getId());
            rosterRecordTable.setAddTime(new Date());
            rosterRecordTables.add(rosterRecordTable);
        }
        repository.save(rosterRecordTables);


    }

    @Override
    public List<CustomAllocationRosterRecordDto> selectList(CustomAllocationRosterRecordSearchEntity
                                                                    searchPageList) throws ServiceException {

        List<CustomAllocationRosterRecordDto> dtos = getDto(searchPageList);

        return dtos;
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clickAllocationRoster(CustomAllocationRosterClickEntity entity) {
        dao.clickAllocationRoster(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clickAllocationRosterByCountrys(CustomAllocationRosterClickByCountrysEntity entity) {
        dao.clickAllocationRosterByCountrys(entity);
    }

    private List<CustomAllocationRosterRecordDto> getDto(CustomAllocationRosterRecordSearchEntity search) {
        List<CustomAllocationRosterRecordTable> records = dao.selectList(search);
        if (records == null || records.size() <= 0) {
            return null;
        }
        List<CustomAllocationRosterRecordDto> dtos = new ArrayList<>(records.size());
        for (CustomAllocationRosterRecordTable record : records) {
            CustomAllocationRosterRecordDto dto = transform(record);
            dtos.add(dto);
        }
        return dtos;
    }

    private CustomAllocationRosterRecordDto transform(CustomAllocationRosterRecordTable record) {
        CustomAllocationRosterRecordDto dto = new CustomAllocationRosterRecordDto();
        BeanUtils.copyProperties(record, dto);
        return dto;
    }
}
