package com.crm.web.service.impl;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.web.information.WebInformationSearchDto;
import com.crm.rely.backend.model.entity.web.information.WebInformationAddEntity;
import com.crm.rely.backend.model.entity.web.information.WebInformationDeleteEntity;
import com.crm.rely.backend.model.entity.web.information.WebInformationSearchListEntity;
import com.crm.rely.backend.model.entity.web.information.WebInformationUpdateEntity;
import com.crm.rely.backend.model.pojo.table.WebInformationTable;
import com.crm.web.dao.mapper.WebInformationMapper;
import com.crm.web.dao.repository.WebInformationRepository;
import com.crm.web.service.WebInformationService;
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;

@Service
public class WebInformationServiceImpl implements WebInformationService {

    @Autowired
    private WebInformationRepository webInformationRepository;

    @Autowired
    private WebInformationMapper webInformationMapper;

    @Override
    public WebInformationTable getById(Long id) throws ServiceException {

        WebInformationTable table = webInformationRepository.getFirstById(id);
        if (table == null) {
            throw ServiceException.notFoundException();
        }
        return table;
    }

    @Override
    public BaseResultDto<WebInformationSearchDto> searchSingle(Long id) throws ServiceException {
        WebInformationTable webInformationTable = getById(id);

        if (webInformationTable == null) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        WebInformationSearchDto webInformationSearchDto = new WebInformationSearchDto();
        BeanUtils.copyProperties(webInformationTable, webInformationSearchDto);

        return BaseResultDto.success(webInformationSearchDto);
    }

    @Override
    public ResultWithPagerDto<WebInformationSearchDto> pageSearchList(WebInformationSearchListEntity entity) throws ServiceException {
        Integer count = webInformationMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<WebInformationTable> webInformationTables = webInformationMapper.pageList(entity);

        if (webInformationTables == null || webInformationTables.size() <= 0) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<WebInformationSearchDto> webInformationSearchDtos = new ArrayList<>(webInformationTables.size());

        for (WebInformationTable webInformationTable : webInformationTables) {
            WebInformationSearchDto webInformationSearchDto = new WebInformationSearchDto();
            BeanUtils.copyProperties(webInformationTable, webInformationSearchDto);
            webInformationSearchDtos.add(webInformationSearchDto);

        }
        return ResultWithPagerDto.success(pageDto, webInformationSearchDtos);
    }

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

        WebInformationTable table = new WebInformationTable();
        BeanUtils.copyProperties(entity, table);
        webInformationRepository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WebInformationUpdateEntity entity) throws ServiceException {
        WebInformationTable webInformationTable = webInformationRepository.findFirstById(entity.getId());
        if (webInformationTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        BeanUtils.copyProperties(entity, webInformationTable);

        webInformationRepository.save(webInformationTable);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(WebInformationDeleteEntity entity) throws ServiceException {
        List<WebInformationTable> webInformationRepositoryAllByIdIn = webInformationRepository.findAllByIdIn(entity.getIds());
        if (webInformationRepositoryAllByIdIn == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        webInformationRepository.delete(webInformationRepositoryAllByIdIn);
    }
}
