package com.crm.custom.service.impl;

import com.crm.custom.dao.repository.AccountApplyRepository;
import com.crm.custom.service.AccountApplyService;
import com.crm.custom.service.CustomInfoService;
import com.crm.custom.service.CustomLoginService;
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.entity.account.apply.AccountApplyAddEntity;
import com.crm.rely.backend.model.pojo.table.AccountApplyTable;
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;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccountApplyServiceImpl implements AccountApplyService {

    @Autowired
    private AccountApplyRepository repository;
    @Autowired
    private CustomLoginService customLoginService;

    @Autowired
    private CustomInfoService customInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAccountApply(AccountApplyAddEntity entity) throws Exception {
        existAccountApply(entity.getCustomId());


        validLoginNumber(entity.getCustomId());
        /*
            保存信息
         */
        AccountApplyTable table = new AccountApplyTable();
        BeanUtils.copyProperties(entity, table);

        table.setStatus(StatusConstants.APPLY_STATUS);
        table.setSerial(getSerial());
        table.setAccountStatus(null);
        table.setGroupCode(null);
        table.setLogin(null);
        repository.save(table);
    }

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

    private void existAccountApply(Long customId) throws ServiceException {
        List<Integer> st = new ArrayList<>();
        st.add(StatusConstants.APPLY_STATUS);
        st.add(StatusConstants.PASSED_STATUS);

        List<Integer> accountSt = new ArrayList<>();
        accountSt.add(StatusConstants.MANAGER_COMPLETED_STATUS);
        accountSt.add(StatusConstants.MANAGER_FAIL_STATUS);
        /*
            验证是否有未完成的申请
         */
        Integer pending = repository.countByCustomIdAndAccountStatusNotInAndStatusNot(customId, accountSt,
                StatusConstants.REJECTED_STATUS);
        if (pending != null && pending >= 1) {
            throw new ServiceException("apply_exists");
        }
//        Integer pending = accountApplyRepository.countByCustomIdAndAccountStatusNotIn(customId, accountSt);
//        if (pending != null && pending >= 1) {
//            throw new ServiceException("apply_exists");
//        }

        pending = repository.countByCustomIdAndStatusInAndAccountStatusIsNull(customId, st);
        if (pending != null && pending >= 1) {
            throw new ServiceException("apply_exists");
        }
    }

    private void validLoginNumber(Long customId) throws ServiceException {

        Integer countLogin = customLoginService.countByCustomId(customId);
        if (countLogin != null && countLogin.compareTo(0) > 0) {
            Integer loginNumber = 5;

            if (countLogin.compareTo(loginNumber) >= 0) {
                throw ServiceException.exception(Constants.LOGIN_SUPERIOR_LIMIT);
            }
        }

    }
}
