package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CooperativePartnerMapper;
import com.crm.manager.dao.repository.CooperativePartnerRepository;
import com.crm.manager.service.CooperativePartnerService;
import com.crm.rely.backend.core.constant.Constants;
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.cooperative.partner.agent.CooperativePartnerAgentDto;
import com.crm.rely.backend.core.dto.cooperative.partner.subset.CooperativePartnerSubsetDto;
import com.crm.rely.backend.core.dto.cooperative.partner.us.CooperativePartnerUsDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.cooperative.partner.agent.*;
import com.crm.rely.backend.core.entity.cooperative.partner.subset.*;
import com.crm.rely.backend.core.entity.cooperative.partner.us.*;
import com.crm.rely.backend.core.pojo.table.CooperativePartnerTable;
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 java.util.ArrayList;
import java.util.List;

@Service
public class CooperativePartnerServiceImpl implements CooperativePartnerService {

    @Autowired
    private CooperativePartnerRepository repository;

    @Autowired
    private CooperativePartnerMapper mapper;

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

        CooperativePartnerTable table = new CooperativePartnerTable();
        BeanUtils.copyProperties(entity, table);
        table.setStatus(0);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAgent(CooperativePartnerAgentUpdateEntity entity) throws ServiceException {
        CooperativePartnerTable 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 deleteAgent(CooperativePartnerAgentDeleteEntity entity) throws ServiceException {
        List<CooperativePartnerTable> tables = repository.findAllByIdIn(entity.getIds());

        if (tables == null || tables.size() <= 0) {
            return;
        }
        repository.delete(tables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approveAgent(CooperativePartnerAgentApproveEntity entity) throws ServiceException {
        CooperativePartnerTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);

        repository.save(table);
    }

    @Override
    public ResultWithPagerDto<CooperativePartnerAgentDto> searchAgentPageList(CooperativePartnerAgentSearchEntity entity) throws ServiceException, IllegalAccessException, InstantiationException {

        Integer count = mapper.countAgentList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        List<CooperativePartnerAgentDto> dtos = getCooperativePartnerAgentDto(entity);
        if (dtos == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public CooperativePartnerAgentDto searchAgentSingle(SingleLongEntity entity) throws ServiceException, InstantiationException, IllegalAccessException {
        CooperativePartnerTable table = repository.getFirstById(entity.getId());

        CooperativePartnerAgentDto dto = table.transferToDto(CooperativePartnerAgentDto.class);

        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSubset(CooperativePartnerSubsetAddEntity entity) throws ServiceException {
        CooperativePartnerTable table = new CooperativePartnerTable();
        BeanUtils.copyProperties(entity, table);
        table.setStatus(0);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSubset(CooperativePartnerSubsetUpdateEntity entity) throws ServiceException {
        CooperativePartnerTable 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 deleteSubset(CooperativePartnerSubsetDeleteEntity entity) throws ServiceException {
        List<CooperativePartnerTable> tables = repository.findAllByIdIn(entity.getIds());

        if (tables == null || tables.size() <= 0) {
            return;
        }
        repository.delete(tables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approveSubset(CooperativePartnerSubsetApproveEntity entity) throws ServiceException {
        CooperativePartnerTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);

        repository.save(table);
    }

    @Override
    public ResultWithPagerDto<CooperativePartnerSubsetDto> searchSubsetPageList(CooperativePartnerSubsetSearchEntity entity) throws ServiceException, IllegalAccessException, InstantiationException {
        Integer count = mapper.countSubsetList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        List<CooperativePartnerSubsetDto> dtos = getCooperativePartnerSubsetDto(entity);
        if (dtos == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public CooperativePartnerSubsetDto searchSubsetSingle(SingleLongEntity entity) throws ServiceException, InstantiationException, IllegalAccessException {
        CooperativePartnerTable table = repository.getFirstById(entity.getId());

        CooperativePartnerSubsetDto dto = table.transferToDto(CooperativePartnerSubsetDto.class);

        return dto;
    }

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

        CooperativePartnerTable table = new CooperativePartnerTable();
        BeanUtils.copyProperties(entity, table);
        table.setStatus(0);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUs(CooperativePartnerUsUpdateEntity entity) throws ServiceException {
        CooperativePartnerTable 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 deleteUs(CooperativePartnerUsDeleteEntity entity) throws ServiceException {
        List<CooperativePartnerTable> tables = repository.findAllByIdIn(entity.getIds());

        if (tables == null || tables.size() <= 0) {
            return;
        }
        repository.delete(tables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approveUs(CooperativePartnerUsApproveEntity entity) throws ServiceException {
        CooperativePartnerTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);

        repository.save(table);
    }

    @Override
    public ResultWithPagerDto<CooperativePartnerUsDto> searchUsPageList(CooperativePartnerUsSearchEntity entity) throws ServiceException, IllegalAccessException, InstantiationException {

        Integer count = mapper.countAgentList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        List<CooperativePartnerUsDto> dtos = getCooperativePartnerUsDto(entity);
        if (dtos == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public CooperativePartnerUsDto searchUsSingle(SingleLongEntity entity) throws ServiceException, InstantiationException, IllegalAccessException {
        CooperativePartnerTable table = repository.getFirstById(entity.getId());

        CooperativePartnerUsDto dto = table.transferToDto(CooperativePartnerUsDto.class);

        return dto;
    }

    private List<CooperativePartnerAgentDto> getCooperativePartnerAgentDto(CooperativePartnerAgentSearchEntity entity) throws ServiceException, InstantiationException, IllegalAccessException {
        List<CooperativePartnerTable> tables = mapper.pageAgentList(entity);

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

            return null;
        }
        List<CooperativePartnerAgentDto> dtos = new ArrayList<>(tables.size());

        for (CooperativePartnerTable table : tables) {
            CooperativePartnerAgentDto dto = table.transferToDto(CooperativePartnerAgentDto.class);
            dtos.add(dto);
        }
        return dtos;
    }

    private List<CooperativePartnerSubsetDto> getCooperativePartnerSubsetDto(CooperativePartnerSubsetSearchEntity entity) throws ServiceException, InstantiationException, IllegalAccessException {
        List<CooperativePartnerTable> tables = mapper.pageAgentList(entity);

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

            return null;
        }
        List<CooperativePartnerSubsetDto> dtos = new ArrayList<>(tables.size());

        for (CooperativePartnerTable table : tables) {
            CooperativePartnerSubsetDto dto = table.transferToDto(CooperativePartnerSubsetDto.class);
            dtos.add(dto);
        }
        return dtos;
    }

    private List<CooperativePartnerUsDto> getCooperativePartnerUsDto(CooperativePartnerUsSearchEntity entity) throws ServiceException, InstantiationException, IllegalAccessException {
        List<CooperativePartnerTable> tables = mapper.pageUsList(entity);

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

            return null;
        }
        List<CooperativePartnerUsDto> dtos = new ArrayList<>(tables.size());

        for (CooperativePartnerTable table : tables) {
            CooperativePartnerUsDto dto = table.transferToDto(CooperativePartnerUsDto.class);
            dtos.add(dto);
        }
        return dtos;
    }
}
