package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.MarketAgentLinkMapper;
import com.crm.manager.dao.repository.MarketAgentLinkRepository;
import com.crm.manager.service.MarketAgentLinkService;
import com.crm.manager.service.impl.base.BaseUploadServiceImpl;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.CustomConstant;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.market.agnet.link.MarketAgentLinkDto;
import com.crm.rely.backend.core.entity.market.agent.link.MarketAgentLinkAddEntity;
import com.crm.rely.backend.core.entity.market.agent.link.MarketAgentLinkDeleteEntity;
import com.crm.rely.backend.core.entity.market.agent.link.MarketAgentLinkSearchPageEntity;
import com.crm.rely.backend.core.entity.market.agent.link.MarketAgentLinkUpdateEntity;
import com.crm.rely.backend.core.pojo.table.MarketAgentLinkTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

@Service
public class MarketAgentLinkServiceImpl extends BaseUploadServiceImpl implements MarketAgentLinkService {

    @Autowired
    private MarketAgentLinkRepository repository;

    @Autowired
    private MarketAgentLinkMapper mapper;

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

        MarketAgentLinkTable table = repository.getFirstById(id);
        if (table == null) {
            throw new ServiceException(CustomConstant.ACTIVITY_NOT_EXIST_ERROR);
        }
        return table;
    }

    @Override
    public MarketAgentLinkDto searchSingle(Long id) throws ServiceException {
        MarketAgentLinkTable MarketAgentLinkTable = getById(id);

        if (MarketAgentLinkTable == null) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        MarketAgentLinkDto marketExtensionDto = new MarketAgentLinkDto();
        BeanUtils.copyProperties(MarketAgentLinkTable, marketExtensionDto);

        return marketExtensionDto;
    }

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

        if (dtos == null || dtos.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public List<MarketAgentLinkDto> searchList(MarketAgentLinkSearchPageEntity entity) throws ServiceException {

        List<MarketAgentLinkTable> tables = mapper.pageList(entity);

        if (tables == null || tables.size() <= 0) {
            return null;
        }
        List<MarketAgentLinkDto> dtos = new ArrayList<>(tables.size());
        List<Long> ids = new ArrayList<>(tables.size());
        for (MarketAgentLinkTable table : tables) {
            MarketAgentLinkDto MarketAgentLinkDto = new MarketAgentLinkDto();
            BeanUtils.copyProperties(table, MarketAgentLinkDto);
            dtos.add(MarketAgentLinkDto);
            ids.add(table.getId());
        }

        return dtos;
    }

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

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

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MarketAgentLinkUpdateEntity entity) throws ServiceException {
        MarketAgentLinkTable 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(MarketAgentLinkDeleteEntity entity) throws ServiceException {
        List<MarketAgentLinkTable> MarketAgentLinkRepositoryAllByIdIn = repository.findAllByIdIn(entity.getIds());
        if (MarketAgentLinkRepositoryAllByIdIn == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        repository.delete(MarketAgentLinkRepositoryAllByIdIn);

    }

    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        String middlePath = "/market/agent/link";
        String path = saveFile(middlePath, file);

        return path;
    }
}
