package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.AccountInvestorPasswordChangeMapper;
import com.crm.manager.dao.repository.AccountInvestorPasswordChangeRepository;
import com.crm.manager.service.AccountInvestorPasswordChangeService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.account.investor.AccountInvestorPasswordChangeDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeApproveEntity;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeApproveManagerEntity;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeDeleteEntity;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeSearchEntity;
import com.crm.rely.backend.core.pojo.table.AccountInvestorPasswordChangeTable;
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;

/**
 * @description:
 * @author: houn
 * @create: 2020-07-07 14:45
 **/
@Service
public class AccountInvestorPasswordChangeServiceImpl implements AccountInvestorPasswordChangeService {

    @Autowired
    private AccountInvestorPasswordChangeRepository accountInvestorPasswordChangeRepository;

    @Autowired
    private AccountInvestorPasswordChangeMapper accountInvestorPasswordChangeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(AccountInvestorPasswordChangeApproveEntity entity) throws ServiceException {
        AccountInvestorPasswordChangeTable investorPasswordChangeTable = accountInvestorPasswordChangeRepository.findFirstById(entity.getId());
        if (investorPasswordChangeTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        if (!Constants.APPLY_STATUS.equals(investorPasswordChangeTable.getStatus())) {
            throw ServiceException.exception(Constants.CANNOT_APPROVE);
        }

        BeanUtils.copyProperties(entity, investorPasswordChangeTable);

        accountInvestorPasswordChangeRepository.save(investorPasswordChangeTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(AccountInvestorPasswordChangeApproveManagerEntity entity) throws ServiceException {
        AccountInvestorPasswordChangeTable investorPasswordChangeTable = accountInvestorPasswordChangeRepository.findFirstById(entity.getId());
        if (investorPasswordChangeTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        if (!Constants.PASSED_STATUS.equals(investorPasswordChangeTable.getStatus())||!Constants.MANAGER_PROCESSING_STATUS.equals(investorPasswordChangeTable.getInvestorStatus())) {
            throw ServiceException.exception(Constants.CANNOT_APPROVE);
        }

        BeanUtils.copyProperties(entity, investorPasswordChangeTable);

        accountInvestorPasswordChangeRepository.save(investorPasswordChangeTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(AccountInvestorPasswordChangeDeleteEntity entity) throws ServiceException {
        List<AccountInvestorPasswordChangeTable> accountInvestorPasswordChangeTables = accountInvestorPasswordChangeRepository.findAllByIdIn(entity.getIds());
        if (accountInvestorPasswordChangeTables == null || accountInvestorPasswordChangeTables.size() <= 0) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        accountInvestorPasswordChangeRepository.delete(accountInvestorPasswordChangeTables);
    }

    @Override
    public ResultWithPagerDto<AccountInvestorPasswordChangeDto> searchList(AccountInvestorPasswordChangeSearchEntity entity) throws ServiceException {
        Integer count = accountInvestorPasswordChangeMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<AccountInvestorPasswordChangeTable> accountInvestorPasswordChangeTables = accountInvestorPasswordChangeMapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<AccountInvestorPasswordChangeDto> accountInvestorPasswordChangeDtos = new ArrayList<>(accountInvestorPasswordChangeTables.size());

        for (AccountInvestorPasswordChangeTable accountInvestorPasswordChangeTable : accountInvestorPasswordChangeTables) {
            AccountInvestorPasswordChangeDto transform = transform(accountInvestorPasswordChangeTable);
            accountInvestorPasswordChangeDtos.add(transform);
        }

        return ResultWithPagerDto.success(pageDto, accountInvestorPasswordChangeDtos);
    }

    @Override
    public AccountInvestorPasswordChangeDto searchSingle(Long id) throws ServiceException {
        AccountInvestorPasswordChangeTable accountInvestorPasswordChangeTable = accountInvestorPasswordChangeRepository.getFirstById(id);
        if (accountInvestorPasswordChangeTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return transform(accountInvestorPasswordChangeTable);
    }

    private AccountInvestorPasswordChangeDto transform(AccountInvestorPasswordChangeTable accountInvestorPasswordChangeTable) {
        AccountInvestorPasswordChangeDto accountInvestorPasswordChangeDto = new AccountInvestorPasswordChangeDto();
        BeanUtils.copyProperties(accountInvestorPasswordChangeTable, accountInvestorPasswordChangeDto);
        return accountInvestorPasswordChangeDto;
    }
}
