package com.trade.admin.enterprise.customer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.admin.enterprise.customer.domain.CustomerAssign;
import com.trade.service.enterprise.customer.dto.EnterpriseDictVO;
import com.trade.admin.enterprise.customer.service.EnterpriseService;
import com.trade.admin.enterprise.customer.service.CustomerAssignService;
import com.trade.admin.enterprise.customer.dto.EnterpriseDetailDto;
import com.trade.admin.system.security.utils.SecurityUtils;
import com.trade.common.utils.bean.BeanCopyUtils;
import com.trade.service.enterprise.customer.dto.EnterpriseBaseInfoDto;
import com.trade.service.enterprise.customer.dto.EnterpriseSearchDto;
import com.trade.common.core.page.GridPage;
import com.trade.common.utils.LoggerUtil;
import com.trade.service.enterprise.customer.domain.Enterprise;
import com.trade.service.enterprise.customer.dto.EnterpriseDto;
import com.trade.service.enterprise.customer.service.EnterpriseBaseService;
import com.trade.service.enterprise.customer.service.impl.EnterpriseDataConverter;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业信息服务实现类
 *
 * @author hlb
 * @since 2024-05-29 15:02:09
 */
@Service("enterpriseService")
public class EnterpriseServiceImpl implements EnterpriseService {

    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);
    @Resource
    private EnterpriseBaseService enterpriseBaseService;
    @Resource
    private EnterpriseDataConverter enterpriseDataConverter;
    @Resource
    private CustomerAssignService customerAssignService;

    @Override
    public GridPage<EnterpriseBaseInfoDto> getPages(EnterpriseSearchDto search) {
        Long userId = SecurityUtils.getUserId();
        Page<EnterpriseBaseInfoDto> page = PageHelper.startPage(search.getPage(), search.getRows());
        List<EnterpriseBaseInfoDto> result = enterpriseBaseService.findBySearch(search);
        return new GridPage<>(search.getRows(), search.getPage(), page.getTotal(), result);
    }

    @Override
    public EnterpriseDetailDto queryDetailById(Long id) {
        Enterprise enterprise = enterpriseBaseService.getById(id);
        if (enterprise == null) {
            return null;
        }
        EnterpriseDto enterpriseDto = enterpriseDataConverter.EnterpriseDomainToDto(enterprise);
        EnterpriseDetailDto enterpriseDetailDto = BeanCopyUtils.copy(enterpriseDto, EnterpriseDetailDto.class);
        if (enterpriseDetailDto == null) {
            return null;
        }
        CustomerAssign customerAssign = customerAssignService.findByCustomerUserId(enterpriseDto.getCustomerId());
        if (customerAssign == null) {
            return enterpriseDetailDto;
        }
        enterpriseDetailDto.setRiskAssignWay(customerAssign.getRcAssignWay());
        enterpriseDetailDto.setRiskManager(customerAssign.getRcUserName());
        enterpriseDetailDto.setRiskManagerId(customerAssign.getRcUserId());
        enterpriseDetailDto.setRiskTransferTime(customerAssign.getRiskTime());
        enterpriseDetailDto.setBdAssignWay(customerAssign.getBdAssignWay());
        enterpriseDetailDto.setBusinessManager(customerAssign.getBdUserName());
        enterpriseDetailDto.setBusinessManagerId(customerAssign.getBdUserId());
        enterpriseDetailDto.setBdTransferTime(customerAssign.getBdTime());
        return enterpriseDetailDto;
    }

    @Override
    public List<EnterpriseBaseInfoDto> findByCustomerInfo(String customerInfo) {
        return enterpriseBaseService.findByCustomerInfo(customerInfo);
    }

    @Override
    public List<EnterpriseDictVO> queryAllEnterprise4Dict() {
        return enterpriseBaseService.queryAllEnterprise4Dict();
    }
}
