package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.AccountDealPasswordChangeMapper;
import com.crm.manager.dao.repository.AccountDealPasswordChangeRepository;
import com.crm.manager.service.AccountDealPasswordChangeService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.account.deal.change.AccountDealPasswordChangeDto;
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.change.AccountDealPasswordChangeApproveEntity;
import com.crm.rely.backend.core.entity.account.deal.change.AccountDealPasswordChangeApproveManagerEntity;
import com.crm.rely.backend.core.entity.account.deal.change.AccountDealPasswordChangeDeleteEntity;
import com.crm.rely.backend.core.entity.account.deal.change.AccountDealPasswordChangeSearchEntity;
import com.crm.rely.backend.core.pojo.table.AccountDealPasswordChangeTable;
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 AccountDealPasswordChangeServiceImpl implements AccountDealPasswordChangeService {

    @Autowired
    private AccountDealPasswordChangeRepository accountDealPasswordChangeRepository;

    @Autowired
    private AccountDealPasswordChangeMapper accountDealPasswordChangeMapper;

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

        BeanUtils.copyProperties(entity, accountDealPasswordChangeTable);

        accountDealPasswordChangeRepository.save(accountDealPasswordChangeTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(AccountDealPasswordChangeApproveManagerEntity entity) throws ServiceException {
        AccountDealPasswordChangeTable accountDealPasswordChangeTable = accountDealPasswordChangeRepository.findFirstById(entity.getId());
        if (accountDealPasswordChangeTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

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

        BeanUtils.copyProperties(entity, accountDealPasswordChangeTable);

        accountDealPasswordChangeRepository.save(accountDealPasswordChangeTable);
    }

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

        accountDealPasswordChangeRepository.delete(accountDealPasswordChangeTables);
    }

    @Override
    public ResultWithPagerDto<AccountDealPasswordChangeDto> searchList(AccountDealPasswordChangeSearchEntity entity) throws ServiceException {
        Integer count = accountDealPasswordChangeMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<AccountDealPasswordChangeTable> accountDealPasswordChangeTables = accountDealPasswordChangeMapper.pageList(entity);

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

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<AccountDealPasswordChangeDto> accountDealPasswordChangeDtos = new ArrayList<>(accountDealPasswordChangeTables.size());

        for (AccountDealPasswordChangeTable accountDealPasswordChangeTable : accountDealPasswordChangeTables) {
            AccountDealPasswordChangeDto activityApplySearchDto = transform(accountDealPasswordChangeTable);
            accountDealPasswordChangeDtos.add(activityApplySearchDto);
        }

        return ResultWithPagerDto.success(pageDto, accountDealPasswordChangeDtos);
    }

    @Override
    public AccountDealPasswordChangeDto searchSingle(Long id) throws ServiceException {
        AccountDealPasswordChangeTable dealPasswordChangeTable = accountDealPasswordChangeRepository.getFirstById(id);
        if (dealPasswordChangeTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return transform(dealPasswordChangeTable);
    }

    private AccountDealPasswordChangeDto transform(AccountDealPasswordChangeTable accountDealPasswordChangeTable) {
        AccountDealPasswordChangeDto accountDealPasswordChangeDto = new AccountDealPasswordChangeDto();
        BeanUtils.copyProperties(accountDealPasswordChangeTable, accountDealPasswordChangeDto);
        return accountDealPasswordChangeDto;
    }
}
