package com.crm.custom.service.impl;

import com.crm.custom.dao.repository.AccountApplyDemoRepository;
import com.crm.custom.service.AccountApplyDemoService;
import com.crm.custom.service.CustomInfoService;
import com.crm.custom.service.CustomLoginDemoService;
import com.crm.rely.backend.core.constant.Constants;
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.constant.CustomConstants;
import com.crm.rely.backend.model.entity.account.apply.demo.AccountApplyDemoAddEntity;
import com.crm.rely.backend.model.pojo.table.AccountApplyDemoTable;
import com.crm.rely.backend.service.RedisService;
import com.crm.rely.backend.util.GetRandom;
import com.crm.rely.backend.util.UUIDUtil;
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 AccountApplyDemoServiceImpl implements AccountApplyDemoService {

    @Autowired
    private AccountApplyDemoRepository accountApplyDemoRepository;
    @Autowired
    private CustomLoginDemoService customLoginDemoService;

    @Autowired
    private CustomInfoService customInfoService;

    @Autowired
    private RedisService redisService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAccountApplyDemo(AccountApplyDemoAddEntity entity) throws Exception {


        /*
            验证是否有未完成的申请
         */
        boolean exists = accountApplyDemoRepository.existsByCustomIdAndStatus(entity.getCustomId(), StatusConstants.APPLY_STATUS);
        if (exists) {
            throw new ServiceException(Constants.APPLY_EXISTS);
        }
        exists = customLoginDemoService.existDemoStatus(entity.getCustomId());
        if (exists) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }
        String uuid = UUIDUtil.getUUID();
        String key=this.getClass().toString() + "addAccountApplyDemo" + entity.getCustomId();
        try {
            if (!redisService.tryLock(key, uuid
                    , 10*60 * 1000)) {
                throw ServiceException.exception("no_repeat_submit_error");
            }
        /*
            保存信息
         */
            AccountApplyDemoTable table = new AccountApplyDemoTable();
            BeanUtils.copyProperties(entity, table);
            table.setMasterPassword(null);
            table.setStatus(StatusConstants.APPLY_STATUS);
            table.setSerial(getSerial());
            table.setAccountStatus(null);
            table.setBalanceStatus(null);
            table.setExpirationTime(null);
            table.setGroupCode(null);
            table.setLogin(null);
            accountApplyDemoRepository.save(table);

            customInfoService.updatePosition(table.getCustomId(), CustomConstants.POSITION2);
        } finally {
            redisService.unlock(key, uuid);
        }

    }

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