package com.crm.custom.service.impl;

import com.crm.custom.dao.mapper.CustomInfoDao;
import com.crm.custom.dao.mapper.CustomRecommendMapper;
import com.crm.custom.service.CustomRecommendService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.custom.CustomInfoLoginDto;
import com.crm.rely.backend.model.dto.custom.CustomInfoRecommendDto;
import com.crm.rely.backend.model.entity.custom.CustomInfoListEntity;
import com.crm.rely.backend.model.entity.custom.CustomInfoLoginListEntity;
import com.crm.rely.backend.model.pojo.view.CustomInfoLoginView;
import com.crm.rely.backend.model.pojo.view.CustomInfoView;
import com.crm.rely.backend.util.RexUtil;
import com.google.common.base.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class CustomRecommendServiceImpl implements CustomRecommendService {

    @Autowired
    private CustomRecommendMapper mapper;

    @Autowired
    private CustomInfoDao customInfoDao;

    @Override
    public ResultWithPagerDto<CustomInfoLoginDto> searchPageList(CustomInfoLoginListEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<CustomInfoLoginDto> dtos = getDtos(entity);
        if (dtos == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public ResultWithPagerDto<CustomInfoRecommendDto> searchPageList(CustomInfoListEntity entity) throws ServiceException {
        Integer count = customInfoDao.countRecommend(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<CustomInfoRecommendDto> dtos = getDtos(entity);
        if (dtos == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    private List<CustomInfoLoginDto> getDtos(CustomInfoLoginListEntity entity) throws ServiceException {
        List<CustomInfoLoginView> views = mapper.pageList(entity);

        if (views == null || views.size() <= 0) {
            return null;
        }
        List<CustomInfoLoginDto> dtos = new ArrayList<>(views.size());
        for (CustomInfoLoginView view : views) {
            CustomInfoLoginDto dto = transform(view);

            dtos.add(dto);
        }


        return dtos;
    }

    private CustomInfoLoginDto transform(CustomInfoLoginView table) {
        CustomInfoLoginDto dto = new CustomInfoLoginDto();
        BeanUtils.copyProperties(table, dto);

        if (!Strings.isNullOrEmpty(dto.getPhone())) {
            int length = dto.getPhone().length();
            if (length > 3) {
                if (length < 7) {
                    dto.setPhone(RexUtil.getMaxkstr(dto.getPhone(), 3, length, "*"));
                } else {
                    dto.setPhone(RexUtil.getMaxkstr(dto.getPhone(), 3, 7, "*"));
                }
            }
        }

        if (!Strings.isNullOrEmpty(dto.getEmail())) {
            dto.setEmail(RexUtil.getMaxkstr(dto.getEmail(), 2, dto.getEmail().indexOf("@"), "*"));
        }

        return dto;
    }

    private List<CustomInfoRecommendDto> getDtos(CustomInfoListEntity entity) throws ServiceException {
        List<CustomInfoView> views = customInfoDao.getPageRecommend(entity);

        if (views == null || views.size() <= 0) {
            return null;
        }
        List<CustomInfoRecommendDto> dtos = new ArrayList<>(views.size());
        for (CustomInfoView view : views) {
            CustomInfoRecommendDto dto = transform(view);

            dtos.add(dto);
        }


        return dtos;
    }

    private CustomInfoRecommendDto transform(CustomInfoView customInfoView) {
        CustomInfoRecommendDto dto = new CustomInfoRecommendDto();
        BeanUtils.copyProperties(customInfoView, dto);

        if (!Strings.isNullOrEmpty(dto.getPhone())) {
            int length = dto.getPhone().length();
            if (length > 3) {
                if (length < 7) {
                    dto.setPhone(RexUtil.getMaxkstr(dto.getPhone(), 3, length, "*"));
                } else {
                    dto.setPhone(RexUtil.getMaxkstr(dto.getPhone(), 3, 7, "*"));
                }
            }
        }

        if (!Strings.isNullOrEmpty(dto.getEmail())) {
            dto.setEmail(RexUtil.getMaxkstr(dto.getEmail(), 2, dto.getEmail().indexOf("@"), "*"));
        }

        return dto;
    }
}
