package com.fjhx.oa.service.contacts.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.customer.entity.customer.po.Customer;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.oa.entity.contacts.po.Contacts;
import com.fjhx.oa.mapper.contacts.ContactsMapper;
import com.fjhx.oa.service.contacts.ContactsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.supply.entity.supplier.po.SupplierInfo;
import com.fjhx.supply.service.supplier.SupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.oa.entity.contacts.vo.ContactsVo;
import com.fjhx.oa.entity.contacts.dto.ContactsSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.oa.entity.contacts.dto.ContactsDto;
import cn.hutool.core.bean.BeanUtil;

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


/**
 * <p>
 * 外部通讯录联系人 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-06
 */
//@DS(SourceConstant.OA)
@Service
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts> implements ContactsService {
    @Autowired
    CustomerService customerService;
    @Autowired
    SupplierInfoService supplierInfoService;

    @Override
    public Page<ContactsVo> getPage(ContactsSelectDto dto) {
        IWrapper<Contacts> wrapper = getWrapper();
        wrapper.orderByDesc("c", Contacts::getId);
        wrapper.eq("c",Contacts::getEnterpriseType,dto.getEnterpriseType());
        if(ObjectUtil.isNotEmpty(dto.getKeyword())) {
            wrapper.and(q->q.like(Contacts::getContactName, dto.getKeyword())
                    .or()
                    .like(Contacts::getPhoneNumber, dto.getKeyword())
                    .or()
                    .like(Contacts::getEnterpriseName, dto.getKeyword()));
        }
        Page<ContactsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<ContactsVo> records = page.getRecords();
        //给客户赋值企业名称
        List<ContactsVo> customers = records.stream().filter(item -> item.getEnterpriseType() == 1).collect(Collectors.toList());
//        DynamicDataSourceContextHolder.push(SourceConstant.CUSTOMER);
        customerService.attributeAssign(customers, ContactsVo::getEnterpriseId, (item, customer) -> {
            item.setEnterpriseName(customer.getName());
        });
//        DynamicDataSourceContextHolder.poll();
        //给供应商赋值企业名称
        List<ContactsVo> supplierInfos = records.stream().filter(item -> item.getEnterpriseType() == 2).collect(Collectors.toList());
//        DynamicDataSourceContextHolder.push(SourceConstant.SUPPLY);
        supplierInfoService.attributeAssign(supplierInfos, ContactsVo::getEnterpriseId, (item, supplier) -> {
            item.setEnterpriseName(supplier.getName());
        });
//        DynamicDataSourceContextHolder.poll();
        return page;
    }

    @Override
    public ContactsVo detail(Long id) {
        Contacts Contacts = this.getById(id);
        ContactsVo result = BeanUtil.toBean(Contacts, ContactsVo.class);
        return result;
    }

    @Override
    public void add(ContactsDto contactsDto) {
        //企业id 用在创建自定义企业的时候
        Long enterpriseId = IdWorker.getId();

        List<Contacts> contactsList = contactsDto.getContactsList();
        for (Contacts contacts: contactsList){
            if(ObjectUtil.isNotEmpty(contactsDto.getEnterpriseId())) {
                contacts.setEnterpriseId(contactsDto.getEnterpriseId());
                contacts.setEnterpriseType(contactsDto.getEnterpriseType());
            }else{
                contacts.setEnterpriseId(enterpriseId);
                contacts.setEnterpriseName(contactsDto.getEnterpriseName());
                contacts.setEnterpriseType(3);
            }
        }
        saveBatch(contactsList);
    }

    @Override
    public void edit(ContactsDto contactsDto) {
        this.updateById(contactsDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    @Override
    public List<Contacts> getList() {
        //获取自定义企业列表
        IWrapper<Contacts> wrapper = getWrapper();
        wrapper.select("DISTINCT enterprise_id,enterprise_type,enterprise_name");
        wrapper.eq(Contacts::getEnterpriseType,3);
        List<Contacts> contactsList = list(wrapper);
        //获取客户列表
//        DynamicDataSourceContextHolder.push(SourceConstant.CUSTOMER);
        List<Customer> customerList = customerService.list();
        for (Customer customer:customerList){
            Contacts contacts = new Contacts();
            contacts.setEnterpriseId(customer.getId());
            contacts.setEnterpriseName(customer.getName());
            contacts.setEnterpriseType(1);
            contactsList.add(contacts);
        }
//        DynamicDataSourceContextHolder.poll();
        //获取供应商列表
//        DynamicDataSourceContextHolder.push(SourceConstant.SUPPLY);
        List<SupplierInfo> supplierInfoList = supplierInfoService.list();
        for (SupplierInfo supplierInfo:supplierInfoList){
            Contacts contacts = new Contacts();
            contacts.setEnterpriseId(supplierInfo.getId());
            contacts.setEnterpriseName(supplierInfo.getName());
            contacts.setEnterpriseType(2);
            contactsList.add(contacts);
        }
//        DynamicDataSourceContextHolder.poll();
        return contactsList;
    }

}
