package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.RiskLeverageLoginMapper;
import com.crm.manager.dao.repository.RiskLeverageLoginRepository;
import com.crm.manager.service.RiskLeverageLoginRuleService;
import com.crm.manager.service.RiskLeverageLoginService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.ManagerConstant;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.risk.leverage.login.RiskLeverageLoginDto;
import com.crm.rely.backend.core.dto.risk.leverage.login.rule.RiskLeverageLoginRuleDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginAddEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginDeleteEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginSearchPageEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginUpdateEntity;
import com.crm.rely.backend.core.pojo.table.RiskLeverageLoginTable;
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;

@Service
public class RiskLeverageLoginServiceImpl implements RiskLeverageLoginService {

    @Autowired
    private RiskLeverageLoginRepository riskLeverageLoginRepository;

    @Autowired
    private RiskLeverageLoginMapper RiskLeverageLoginMapper;

    @Autowired
    private RiskLeverageLoginRuleService riskLeverageLoginRuleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(RiskLeverageLoginAddEntity entity) throws ServiceException {
        existsByLogin(entity.getLogin());

        RiskLeverageLoginTable riskLeverageLoginTable = new RiskLeverageLoginTable();
        BeanUtils.copyProperties(entity, riskLeverageLoginTable);

        riskLeverageLoginRepository.save(riskLeverageLoginTable);

        riskLeverageLoginRuleService.save(riskLeverageLoginTable.getId(), entity.getRules());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RiskLeverageLoginUpdateEntity entity) throws ServiceException {
        RiskLeverageLoginTable riskLeverageLoginTable = riskLeverageLoginRepository.findFirstById(entity.getId());
        if (riskLeverageLoginTable == null) {
            throw ServiceException.notFoundException();
        }

        if (!entity.getLogin().equals(riskLeverageLoginTable.getLogin())) {
            existsByLogin(entity.getLogin());
        }

        BeanUtils.copyProperties(entity, riskLeverageLoginTable);

        riskLeverageLoginRepository.save(riskLeverageLoginTable);

        riskLeverageLoginRuleService.save(riskLeverageLoginTable.getId(), entity.getRules());

    }

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

        List<RiskLeverageLoginTable> leverageLoginTables = riskLeverageLoginRepository.findAllByIdIn(entity.getIds());
        if (leverageLoginTables == null || leverageLoginTables.size() <= 0) {
            return;
        }
        riskLeverageLoginRepository.delete(leverageLoginTables);

        riskLeverageLoginRuleService.delete(entity.getIds());
    }

    @Override
    public ResultWithPagerDto<RiskLeverageLoginDto> searchPageList(RiskLeverageLoginSearchPageEntity entity) throws ServiceException {
        Integer count = RiskLeverageLoginMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        List<RiskLeverageLoginDto> riskLeverageLoginDto = getRiskLeverageLoginDto(entity);
        if (riskLeverageLoginDto == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        return ResultWithPagerDto.success(pageDto, riskLeverageLoginDto);
    }

    @Override
    public RiskLeverageLoginDto searchSingle(SingleLongEntity entity) throws ServiceException {
        RiskLeverageLoginTable riskLeverageLoginTable = riskLeverageLoginRepository.getFirstById(entity.getId());

        RiskLeverageLoginDto riskLeverageLoginDto = transform(riskLeverageLoginTable);

        List<RiskLeverageLoginRuleDto> rules = riskLeverageLoginRuleService.getRuleDto(riskLeverageLoginTable.getId());
        riskLeverageLoginDto.setRules(rules);

        return riskLeverageLoginDto;
    }

    private void existsByLogin(Long login) throws ServiceException {
        boolean exist = riskLeverageLoginRepository.existsByLogin(login);

        if (exist) {
            throw ServiceException.exception(ManagerConstant.LOGIN_EXIST_ERROR);
        }
    }

    private List<RiskLeverageLoginDto> getRiskLeverageLoginDto(RiskLeverageLoginSearchPageEntity entity) throws ServiceException {
        List<RiskLeverageLoginTable> RiskLeverageLoginTables = RiskLeverageLoginMapper.pageList(entity);

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

            return null;
        }
        List<RiskLeverageLoginDto> RiskLeverageLoginDtos = new ArrayList<>(RiskLeverageLoginTables.size());

        for (RiskLeverageLoginTable RiskLeverageLoginTable : RiskLeverageLoginTables) {
            RiskLeverageLoginDto RiskLeverageLoginDto = transform(RiskLeverageLoginTable);
            RiskLeverageLoginDtos.add(RiskLeverageLoginDto);
        }
        return RiskLeverageLoginDtos;
    }

    private RiskLeverageLoginDto transform(RiskLeverageLoginTable RiskLeverageLoginTable) {
        RiskLeverageLoginDto RiskLeverageLoginDto = new RiskLeverageLoginDto();
        BeanUtils.copyProperties(RiskLeverageLoginTable, RiskLeverageLoginDto);
        return RiskLeverageLoginDto;
    }
}
