package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.AccountDealPasswordResetMapper;
import com.crm.manager.dao.repository.AccountDealPasswordResetRepository;
import com.crm.manager.service.AccountDealPasswordResetService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.account.deal.reset.AccountDealPasswordResetDto;
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.deal.reset.AccountDealPasswordResetApproveEntity;
import com.crm.rely.backend.core.entity.account.deal.reset.AccountDealPasswordResetApproveManagerEntity;
import com.crm.rely.backend.core.entity.account.deal.reset.AccountDealPasswordResetDeleteEntity;
import com.crm.rely.backend.core.entity.account.deal.reset.AccountDealPasswordResetSearchEntity;
import com.crm.rely.backend.core.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.core.pojo.table.AccountDealPasswordResetTable;
import com.crm.rely.backend.core.pojo.view.AccountDealPasswordResetView;
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 AccountDealPasswordResetServiceImpl implements AccountDealPasswordResetService {

    @Autowired
    private AccountDealPasswordResetRepository accountDealPasswordResetRepository;

    @Autowired
    private AccountDealPasswordResetMapper accountDealPasswordResetMapper;

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

        BeanUtils.copyProperties(entity, accountDealPasswordResetTable);

        accountDealPasswordResetRepository.save(accountDealPasswordResetTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(AccountDealPasswordResetApproveManagerEntity entity) throws ServiceException {
        AccountDealPasswordResetTable investorPasswordChangeTable = accountDealPasswordResetRepository.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.getDealResetStatus())) {
            throw ServiceException.exception(Constants.CANNOT_APPROVE);
        }

        BeanUtils.copyProperties(entity, investorPasswordChangeTable);

        accountDealPasswordResetRepository.save(investorPasswordChangeTable);
    }

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

        accountDealPasswordResetRepository.delete(accountDealPasswordResetTables);
    }

    @Override
    public ResultWithPagerDto<AccountDealPasswordResetDto> searchList(AccountDealPasswordResetSearchEntity entity) throws ServiceException {
        Integer count = accountDealPasswordResetMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<AccountDealPasswordResetView> accountDealPasswordResetTables = accountDealPasswordResetMapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<AccountDealPasswordResetDto> accountDealPasswordResetDtos = new ArrayList<>(accountDealPasswordResetTables.size());

        for (AccountDealPasswordResetView accountDealPasswordResetView : accountDealPasswordResetTables) {
            AccountDealPasswordResetDto activityApplySearchDto = transform(accountDealPasswordResetView);
            accountDealPasswordResetDtos.add(activityApplySearchDto);
        }

        return ResultWithPagerDto.success(pageDto, accountDealPasswordResetDtos);
    }

    @Override
    public AccountDealPasswordResetDto searchSingle(Long id) throws ServiceException {
        AccountDealPasswordResetView accountDealPasswordResetView = accountDealPasswordResetMapper.searchSingle(id);
        if (accountDealPasswordResetView == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return transform(accountDealPasswordResetView);
    }

    private AccountDealPasswordResetDto transform(AccountDealPasswordResetView accountDealPasswordResetView) {
        AccountDealPasswordResetDto accountDealPasswordResetDto = new AccountDealPasswordResetDto();
        BeanUtils.copyProperties(accountDealPasswordResetView, accountDealPasswordResetDto);

        return accountDealPasswordResetDto;
    }

//    public static String getName(AccountDealPasswordResetView accountDealPasswordResetView) {
//        StringBuffer nameSbuff = new StringBuffer();
//        nameSbuff.append(accountDealPasswordResetView.getFirstName());
//        if (accountDealPasswordResetView.getMiddle() != null) {
//            nameSbuff.append(" ");
//            nameSbuff.append(accountDealPasswordResetView.getMiddle());
//        }
//        nameSbuff.append(" ");
//        nameSbuff.append(accountDealPasswordResetView.getLastName());
//        return nameSbuff.toString();
//    }
}
