package com.jkcredit.invoice.service.customer.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.mapper.customer.CustomerRecMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.service.customer.CustomerRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("customerRecService")
public class CustomerRecServiceImpl extends ServiceImpl<CustomerRecMapper,CustomerRec>   implements CustomerRecService{
    @Autowired
    CustomerRecMapper customerRecMapper;
    @Override
    public IPage findAllCustomerRec(Page page, CustomerRec customerRec) {
        return customerRecMapper.selectAllByPage(page,customerRec);
    }

    @Override
    public List<CustomerRec> findAllCustomerRecForExport(CustomerRec customerRec) {
        return customerRecMapper.selectAllByPageExport(customerRec);
    }

    @Override
    public synchronized CustomerRec selectByCustomerNameAndCompanyReference(CustomerRec record) {
        CustomerRec customerRec = customerRecMapper.selectByCustomerNameAndCompanyReference(record);

        if(customerRec == null){
            //根据税号查询企业，若业务类型一致自动挂到当前客户下
           List<CustomerRec> customerRecs = customerRecMapper.selectByCompanyReference(customerRec.getCompanyReferencenum());
           if(customerRecs!=null && customerRecs.size()>0){
               for(CustomerRec customerRec1:customerRecs){
                   if(customerRec1.getBussinessType().equals(customerRec.getBussinessType())){
                       customerRec1.setCustomerName(customerRec.getCustomerName());
                       customerRec = customerRec1;
                       customerRecMapper.insert(customerRec);
                       break;
                   }
               }
           }
        }
        return customerRec;
    }

    @Override
    public synchronized CustomerRec selectByCustomerNameAndCompanyNum(CustomerRec record) {
        CustomerRec customerRec = customerRecMapper.selectByCustomerNameAndCompanyNum(record);
        if(customerRec == null){
            //根据税号查询企业，若业务类型一致自动挂到当前客户下
            List<CustomerRec> customerRecs = customerRecMapper.selectByCompanyNum(customerRec);
            if(customerRecs!=null && customerRecs.size()>0){
                for(CustomerRec customerRec1:customerRecs){
                    if(customerRec1.getBussinessType().equals(customerRec.getBussinessType())){
                        customerRec1.setCustomerName(customerRec.getCustomerName());
                        customerRec = customerRec1;
                        customerRecMapper.insert(customerRec);
                        break;
                    }
                }
            }
        }
        if(customerRec == null || customerRec.getRecStatus()!=1){customerRec = null;}
        return customerRec;
    }

    @Override
    public  CustomerRec selectByCustomerNameAndCompanyConcat(CustomerRec record) {
        return customerRecMapper.selectByCustomerNameAndCompanyConcat(record);
    }
}
