package com.dgtly.order.service.impl;

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

import com.dgtly.common.utils.StringUtils;
import com.dgtly.order.domain.CustomerAuthModel;
import com.dgtly.order.domain.OrderCustomers;
import org.springframework.stereotype.Service;
import com.dgtly.order.mapper.CustomersStartMapper;
import com.dgtly.order.domain.CustomersStart;
import com.dgtly.order.service.ICustomersStartService;
import com.dgtly.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 经销商启用Service业务层处理
 * 
 * @author nsy
 * @date 2021-01-18
 */
@Service
public class CustomersStartServiceImpl implements ICustomersStartService 
{
    @Resource
    private CustomersStartMapper customersStartMapper;

    /**
     * 查询经销商启用
     * 
     * @param customersCode 经销商启用ID
     * @return 经销商启用
     */
    @Override
    public CustomersStart selectCustomersStartById(String customersCode)
    {
        return customersStartMapper.selectCustomersStartByCustomersCode(customersCode);
    }

    /**
     * 查询经销商启用列表
     * 
     * @param customersStart 经销商启用
     * @return 经销商启用
     */
    @Override
    public List<CustomersStart> selectCustomersStartList(CustomersStart customersStart) {
        List<CustomersStart> list = customersStartMapper.selectCustomersStartList(customersStart);
        for (CustomersStart start : list) {
            start.setCustomerTypeFormat();
        }
        return list;
    }

    /**
     * 新增经销商启用
     * 
     * @param customersStart 经销商启用
     * @return 结果
     */
    @Override
    public int insertCustomersStart(CustomersStart customersStart)
    {
        CustomersStart customersS = customersStartMapper.selectCustomersStartByCustomersCode(customersStart.getCustomersCode());
        if(customersS != null) return 1;
        return customersStartMapper.insertCustomersStart(customersStart);
    }

    /**
     * 修改经销商启用
     * 
     * @param customersStart 经销商启用
     * @return 结果
     */
    @Override
    public int updateCustomersStart(CustomersStart customersStart)
    {
        return customersStartMapper.updateCustomersStart(customersStart);
    }

    /**
     * 删除经销商启用对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCustomersStartByIds(String ids)
    {
        return customersStartMapper.deleteCustomersStartByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除经销商启用信息
     * 
     * @param customersCode 经销商启用ID
     * @return 结果
     */
    @Override
    public int deleteCustomersStartById(String customersCode)
    {
        return customersStartMapper.deleteCustomersStartById(customersCode);
    }

    /**
     * 返回经销商列表，根据进销商编号去重
     * @param customers
     * @return
     */
    @Override
    public List<OrderCustomers> selectcustomersList(OrderCustomers customers) {
        try {
            StringUtils.beanAttributeValueTrim(customers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customersStartMapper.selectcustomersList(customers);
    }

    /**
     * 新增保存经销商启用
     * @param ids
     * @return
     */
    @Override
    public int addCustomersStart(String ids) {
        int state = 0;
        String[] indexIds = Convert.toStrArray(ids);
        for (String id: indexIds) {
            OrderCustomers customers = customersStartMapper.selectCustomersById(Long.valueOf(id));
            if(customers != null){
                String chainsCode = customers.getChainsCode();
                String chainsName = customers.getChainsName();
                CustomersStart customersStart = customersStartMapper.selectCustomersStartByCustomersCode(chainsCode);
                if(customersStart == null){
                    customersStart = new CustomersStart();
                    customersStart.setCustomersCode(chainsCode);
                    customersStart.setCustomersName(chainsName);
                    customersStart.setOnlineTime(new Date());
                    state = customersStartMapper.insertCustomersStart(customersStart);
                }
                continue;
            }
        }
        return state;
    }

    @Override
    public List<CustomerAuthModel> selectCustomerAuthList(String yearMonth) {
        List<CustomerAuthModel> customerExt=customersStartMapper.selectCustomerExt();
        List<CustomerAuthModel> list=customersStartMapper.selectCustomerAuthList(yearMonth);
        List<String> customertuc = list.stream().map(CustomerAuthModel::getCustomerCode).collect(Collectors.toList());
        List<String> customerext = customerExt.stream().map(CustomerAuthModel::getCustomerCode).collect(Collectors.toList());
        List<String> collect = customerext.stream().filter(item -> !customertuc.contains(item)).collect(Collectors.toList());
        if(collect.size()>0){
        for (String id:collect
             ) {
            CustomerAuthModel model=new CustomerAuthModel();
            CustomerAuthModel auth=customersStartMapper.selectCustomerExtByid(id);
            String name=customersStartMapper.selectCustomerNameByCustomerId(auth.getCustomerCode());
            model.setCustomerCode(auth.getCustomerCode());
            model.setCustomerName(name);
            model.setAuthTime(auth.getAuthTime());
            list.add(model);
        }
        }
        return list;
    }
}
