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.custom.analysis.template.WebCustomAnalysisTemplateSearchDto;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateAddEntity;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateDeleteEntity;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateSearchListEntity;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateUpdateEntity;
import com.crm.rely.backend.model.pojo.table.WebCustomAnalysisTemplateTable;
import com.crm.web.dao.mapper.WebCustomAnalysisTemplateMapper;
import com.crm.web.dao.repository.WebCustomAnalysisTemplateRepository;
import com.crm.web.service.WebCustomAnalysisTemplateService;
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 WebCustomAnalysisTemplateServiceImpl implements WebCustomAnalysisTemplateService {

    @Autowired
    private WebCustomAnalysisTemplateRepository repository;

    @Autowired
    private WebCustomAnalysisTemplateMapper mapper;

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

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

    @Override
    public BaseResultDto<WebCustomAnalysisTemplateSearchDto> searchSingle(Long id) throws ServiceException {
        WebCustomAnalysisTemplateTable table = getById(id);

        if (table == null) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        WebCustomAnalysisTemplateSearchDto dto = new WebCustomAnalysisTemplateSearchDto();
        BeanUtils.copyProperties(table, dto);

        return BaseResultDto.success(dto);
    }

    @Override
    public ResultWithPagerDto<WebCustomAnalysisTemplateSearchDto> pageSearchList(WebCustomAnalysisTemplateSearchListEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        List<WebCustomAnalysisTemplateSearchDto> dtos = searchList(entity);
        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public List<WebCustomAnalysisTemplateSearchDto> searchList(WebCustomAnalysisTemplateSearchListEntity entity) throws ServiceException {

        List<WebCustomAnalysisTemplateTable> tables = mapper.pageList(entity);
        if (entity.getPage() != null) {
            if (tables == null || tables.size() <= 0) {

                throw new ServiceException(Constants.SYSTEM_ERROR);
            }
        }

        List<WebCustomAnalysisTemplateSearchDto> dtos = new ArrayList<>(tables.size());

        for (WebCustomAnalysisTemplateTable table : tables) {
            WebCustomAnalysisTemplateSearchDto dto = new WebCustomAnalysisTemplateSearchDto();
            BeanUtils.copyProperties(table, dto);
            dtos.add(dto);

        }
        return dtos;
    }

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

        WebCustomAnalysisTemplateTable table = new WebCustomAnalysisTemplateTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WebCustomAnalysisTemplateUpdateEntity entity) throws ServiceException {
        WebCustomAnalysisTemplateTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        BeanUtils.copyProperties(entity, table);

        repository.save(table);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(WebCustomAnalysisTemplateDeleteEntity entity) throws ServiceException {
        List<WebCustomAnalysisTemplateTable> tables = repository.findAllByIdIn(entity.getIds());
        if (tables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        repository.delete(tables);
    }
}
