package com.crm.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.crm.manager.dao.mapper.TenantMapper;
import com.crm.manager.dao.repository.TenantRepository;
import com.crm.manager.service.TenantService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
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.tenant.TenantSearchDto;
import com.crm.rely.backend.core.entity.tenant.info.*;
import com.crm.rely.backend.core.pojo.table.MerchantTable;
import com.crm.rely.backend.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TenantServiceImpl implements TenantService {

    @Autowired
    private TenantRepository tenantRepository;

    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public String add(TenantAddEntity entity) {
        log.info("添加商户：{}", JSON.toJSONString(entity));
        MerchantTable merchantTable = new MerchantTable();
        BeanUtils.copyProperties(entity, merchantTable);
        tenantRepository.save(merchantTable);
        return "添加成功";
    }

    @Override
    public TenantRespEntity findSingle(Long id) {
        log.info("查询商户：{}", id);
        MerchantTable merchantTable = tenantRepository.findOne(id);
        TenantRespEntity respEntity = new TenantRespEntity();
        BeanUtils.copyProperties(merchantTable, respEntity);
        return respEntity;
    }

    @Override
    public String edit(TenantEditEntity entity) {
        log.info("修改商户：{}", JSON.toJSONString(entity));
        MerchantTable merchantTable = new MerchantTable();
        BeanUtils.copyProperties(entity, merchantTable);
        tenantRepository.save(merchantTable);
        return "修改成功";
    }

    @Override
    public String delete(Long id) {
        log.info("删除商户：{}", id);
        tenantRepository.delete(id);
        return "删除成功";
    }

    @Override
    public BaseResultWithPagerDto<List<TenantSearchDto>> searchPage(TenantSearchEntity entity) throws ServiceException {
        PageDto pageDto = new PageDto();
        Integer count = tenantMapper.searchByPageCount(entity);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        pageDto = PageDto.format(entity, count);
        List<MerchantTable> merchantTables = tenantMapper.searchByPageList(entity);
        if (merchantTables == null || merchantTables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<TenantSearchDto> userInfoSearchDtos = merchantTables.stream().map(table -> {
            TenantSearchDto dto = new TenantSearchDto();
            BeanUtils.copyProperties(table, dto);
            return dto;
        }).collect(Collectors.toList());


        return new BaseResultWithPagerDto(pageDto, userInfoSearchDtos);
    }

    @Override
    public List<TenantEntity> getAllMerchant() {
        List<MerchantTable> merchantTables = tenantRepository.findAll();
        if (merchantTables == null || merchantTables.size() <= 0) {
            return Collections.emptyList();
        }
        return merchantTables.stream().map(table -> {
            TenantEntity entity = new TenantEntity();
            BeanUtils.copyProperties(table, entity);
            entity.setId(table.getId().intValue());
            return entity;
        }).collect(Collectors.toList());
    }
}
