package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomLoginDao;
import com.crm.manager.service.CustomLoginService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.entity.custom.login.CustomLoginStatusUpdateBySalesIdEntity;
import com.crm.rely.backend.core.pojo.table.CustomLoginTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by max on 2021/1/22.
 */
@Service
public class CustomLoginServiceImpl implements CustomLoginService {

    @Autowired
    private CustomLoginDao customLoginDao;

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

        try {
            List<CustomLoginTable> tables = customLoginDao.findAllLoginBySalesId(entity.getSalesId());
            if (CollectionUtils.isEmpty(tables)) {
                return;
            }
            for (CustomLoginTable table : tables) {
                table.setLoginStatusNumber(0);
                table.setLoginStatus(Constants.MANAGER_PENDING_STATUS);
                table.setModifyUser(entity.getModifyUser());
                table.setModifyIp(entity.getModifyIp());
                table.setModifyTime(entity.getModifyTime());
            }

            customLoginDao.batchUpdateLoginStatusByAgentId(tables);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
