package com.crm.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.crm.manager.dao.mapper.RiskRevokeCreditRuleLoginMapper;
import com.crm.manager.dao.repository.RiskRevokeCreditRuleLoginRepository;
import com.crm.manager.service.RiskRevokeCreditRuleLoginService;
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.revoke.credit.rule.login.RiskRevokeCreditRuleLoginDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.risk.revoke.credit.rule.login.*;
import com.crm.rely.backend.core.pojo.table.RiskRevokeCreditRuleLoginTable;
import com.crm.rely.backend.exception.ServiceException;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Log4j
@Service
public class RiskRevokeCreditRuleLoginServiceImpl implements RiskRevokeCreditRuleLoginService {

    @Autowired
    private RiskRevokeCreditRuleLoginRepository riskRevokeCreditRuleLoginRepository;

    @Autowired
    private RiskRevokeCreditRuleLoginMapper riskRevokeCreditRuleLoginMapper;


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

        RiskRevokeCreditRuleLoginTable RiskRevokeCreditRuleLoginTable = new RiskRevokeCreditRuleLoginTable();
        BeanUtils.copyProperties(entity, RiskRevokeCreditRuleLoginTable);

        riskRevokeCreditRuleLoginRepository.save(RiskRevokeCreditRuleLoginTable);
    }

    @JmsListener(destination = Constants.RISK_REVOKE_CREDIT_RULE_LOGIN)
    @Transactional(rollbackFor = Exception.class)
    public void add(String content) throws ServiceException {
        log.info(String.format("RISK_REVOKE_CREDIT_RULE_LOGIN," +
                "content:%s", content));
        RiskRevokeCreditRuleLoginAddEntity entity = JSON.parseObject(content,
                RiskRevokeCreditRuleLoginAddEntity.class);
        if (entity == null) {
            log.error("RISK_REVOKE_CREDIT_RULE_LOGIN," +
                    "entity is null");
            return;
        }

        if (entity.getLogin() == null) {
            return;
        }

        save(entity);
    }

    private void save(RiskRevokeCreditRuleLoginAddEntity entity) {
        RiskRevokeCreditRuleLoginTable table = riskRevokeCreditRuleLoginRepository.findFirstByLogin(entity.getLogin());

        if (table == null) {
            table = new RiskRevokeCreditRuleLoginTable();
        }
        entity.setAddTime(new Date());
        BeanUtils.copyProperties(entity, table);

        riskRevokeCreditRuleLoginRepository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RiskRevokeCreditRuleLoginUpdateEntity entity) throws ServiceException {
        RiskRevokeCreditRuleLoginTable RiskRevokeCreditRuleLoginTable =
                riskRevokeCreditRuleLoginRepository.findFirstById(entity.getId());
        if (RiskRevokeCreditRuleLoginTable == null) {
            throw ServiceException.notFoundException();
        }

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

        BeanUtils.copyProperties(entity, RiskRevokeCreditRuleLoginTable);

        riskRevokeCreditRuleLoginRepository.save(RiskRevokeCreditRuleLoginTable);

    }

    @JmsListener(destination = Constants.RISK_REVOKE_CREDIT_RULE_LOGIN_DELETE)
    @Transactional(rollbackFor = Exception.class)
    public void delete(String content) throws ServiceException {
        log.info(String.format("RISK_REVOKE_CREDIT_RULE_LOGIN_DELETE," +
                "content:%s", content));
        RiskRevokeCreditRuleLoginMqDeleteEntity entity = JSON.parseObject(content,
                RiskRevokeCreditRuleLoginMqDeleteEntity.class);
        if (entity == null) {
            log.error("RISK_REVOKE_CREDIT_RULE_LOGIN_DELETE," +
                    "entity is null");
            return;
        }

        if (entity.getLogin() == null) {
            return;
        }

        RiskRevokeCreditRuleLoginTable table = riskRevokeCreditRuleLoginRepository.findFirstByLogin(entity.getLogin());

        if (table == null) {
            return;
        }

        riskRevokeCreditRuleLoginRepository.delete(table);
    }

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

        List<RiskRevokeCreditRuleLoginTable> riskRevokeCreditRuleLoginTables =
                riskRevokeCreditRuleLoginRepository.findAllByIdIn(entity.getIds());
        if (riskRevokeCreditRuleLoginTables == null || riskRevokeCreditRuleLoginTables.size() <= 0) {
            return;
        }
        riskRevokeCreditRuleLoginRepository.delete(riskRevokeCreditRuleLoginTables);
    }

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

        List<RiskRevokeCreditRuleLoginDto> riskRevokeCreditRuleLoginDto = getRiskRevokeCreditRuleLoginDto(entity);
        if (riskRevokeCreditRuleLoginDto == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        return ResultWithPagerDto.success(pageDto, riskRevokeCreditRuleLoginDto);
    }

    @Override
    public RiskRevokeCreditRuleLoginDto searchSingle(SingleLongEntity entity) throws ServiceException {
        RiskRevokeCreditRuleLoginTable riskRevokeCreditRuleLoginTable =
                riskRevokeCreditRuleLoginRepository.getFirstById(entity.getId());

        RiskRevokeCreditRuleLoginDto riskRevokeCreditRuleLoginDto = transform(riskRevokeCreditRuleLoginTable);

        return riskRevokeCreditRuleLoginDto;
    }

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

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

    private List<RiskRevokeCreditRuleLoginDto> getRiskRevokeCreditRuleLoginDto(RiskRevokeCreditRuleLoginSearchPageEntity entity) throws ServiceException {
        List<RiskRevokeCreditRuleLoginTable> RiskRevokeCreditRuleLoginTables =
                riskRevokeCreditRuleLoginMapper.pageList(entity);

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

            return null;
        }
        List<RiskRevokeCreditRuleLoginDto> RiskRevokeCreditRuleLoginDtos =
                new ArrayList<>(RiskRevokeCreditRuleLoginTables.size());

        for (RiskRevokeCreditRuleLoginTable RiskRevokeCreditRuleLoginTable : RiskRevokeCreditRuleLoginTables) {
            RiskRevokeCreditRuleLoginDto RiskRevokeCreditRuleLoginDto = transform(RiskRevokeCreditRuleLoginTable);
            RiskRevokeCreditRuleLoginDtos.add(RiskRevokeCreditRuleLoginDto);
        }
        return RiskRevokeCreditRuleLoginDtos;
    }

    private RiskRevokeCreditRuleLoginDto transform(RiskRevokeCreditRuleLoginTable RiskRevokeCreditRuleLoginTable) {
        RiskRevokeCreditRuleLoginDto RiskRevokeCreditRuleLoginDto = new RiskRevokeCreditRuleLoginDto();
        BeanUtils.copyProperties(RiskRevokeCreditRuleLoginTable, RiskRevokeCreditRuleLoginDto);
        return RiskRevokeCreditRuleLoginDto;
    }
}
