package com.crm.custom.service.impl;

import com.crm.custom.dao.repository.AccountApplyPasswordResetRepository;
import com.crm.custom.service.AccountApplyPasswordResetService;
import com.crm.rely.backend.core.constant.CustomConstant;
import com.crm.rely.backend.core.constant.StatusConstants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.entity.account.apply.password.reset.AccountApplyPasswordResetAddEntity;
import com.crm.rely.backend.model.pojo.table.AccountApplyPasswordResetTable;
import com.crm.rely.backend.util.GetRandom;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountApplyPasswordResetServiceImpl implements AccountApplyPasswordResetService {

    @Autowired
    private AccountApplyPasswordResetRepository repository;


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

        /*
            保存信息
         */
        AccountApplyPasswordResetTable table = new AccountApplyPasswordResetTable();
        BeanUtils.copyProperties(entity, table);
        table.setPassword(null);
        table.setPasswordStatus(StatusConstants.MANAGER_PENDING_STATUS);
        table.setSerial(getSerial());
        repository.save(table);
    }

    private String getSerial() {
        String orderNo = GetRandom.getOrderNo(CustomConstant.ACCOUNT_PASSWORD_MASTER_RESET_HEAD);
        return orderNo;
    }
}
