package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysBankMapper;
import com.crm.manager.dao.repository.SysBankRepository;
import com.crm.manager.service.SysBankService;
import com.crm.manager.service.impl.base.BaseUploadServiceImpl;
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.system.bank.SysBankListDto;
import com.crm.rely.backend.core.dto.system.bank.SysBankSearchListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.bank.SysBankAddEntity;
import com.crm.rely.backend.core.entity.system.bank.SysBankDeleteEntitys;
import com.crm.rely.backend.core.entity.system.bank.SysBankSearchListEntity;
import com.crm.rely.backend.core.entity.system.bank.SysBankUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysBankTable;
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;

/**
 * @Author: houn
 * @Date: 2020/10/18 21:38
 * @Description:
 */
@Service
public class SysBankServiceImpl extends BaseUploadServiceImpl implements SysBankService {

    @Autowired
    private SysBankRepository sysBankRepository;

    @Autowired
    private SysBankMapper sysBankMapper;

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

        SysBankTable table = new SysBankTable();
        BeanUtils.copyProperties(entity, table);
        sysBankRepository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysBankUpdateEntity entity) throws ServiceException {
        SysBankTable table = sysBankRepository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        sysBankRepository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysBankDeleteEntitys entitys) throws ServiceException {
        List<SysBankTable> tables = sysBankRepository.findAllByIdIn(entitys.getIds());
        if (tables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        sysBankRepository.delete(tables);
    }

    @Override
    public ResultWithPagerDto<SysBankSearchListDto> searchPageList(SysBankSearchListEntity entity) throws ServiceException {
        Integer count = sysBankMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<SysBankTable> sysBankTables = sysBankMapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysBankSearchListDto> sysBankSearchListDtos = new ArrayList<>(sysBankTables.size());

        for (SysBankTable sysBankTable : sysBankTables) {
            SysBankSearchListDto sysConfigSearchDto = transformSearchListDto(sysBankTable);
            sysBankSearchListDtos.add(sysConfigSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, sysBankSearchListDtos);
    }

    @Override
    public SysBankSearchListDto searchSingle(SingleLongEntity entity) throws ServiceException {
        SysBankTable table = sysBankRepository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        SysBankSearchListDto sysBankSearchListDto = transformSearchListDto(table);
        return sysBankSearchListDto;
    }

    @Override
    public List<SysBankListDto> getList() throws ServiceException {
        List<SysBankTable> sysBankTables = sysBankRepository.getAllByValidOrderBySubIndexAsc(1);
        if (sysBankTables == null) {
            return null;
        }

        List<SysBankListDto> sysBankListDtos = new ArrayList<>(sysBankTables.size());

        for (SysBankTable sysBankTable : sysBankTables) {
            SysBankListDto sysBankListDto = transformListDto(sysBankTable);
            sysBankListDtos.add(sysBankListDto);
        }

        return sysBankListDtos;
    }

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

        return path;
    }

    private SysBankSearchListDto transformSearchListDto(SysBankTable sysBankTable) {
        SysBankSearchListDto sysBankSearchListDto = new SysBankSearchListDto();
        BeanUtils.copyProperties(sysBankTable, sysBankSearchListDto);
        return sysBankSearchListDto;
    }

    private SysBankListDto transformListDto(SysBankTable sysBankTable) {
        SysBankListDto sysBankListDto = new SysBankListDto();
        BeanUtils.copyProperties(sysBankTable, sysBankListDto);
        return sysBankListDto;
    }
}
