package com.dgtly.system.service.impl;

import java.text.ParseException;
import java.util.Iterator;
import java.util.List;

import com.dgtly.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.system.mapper.AssRelcustomerinfoMapper;
import com.dgtly.system.domain.AssRelcustomerinfo;
import com.dgtly.system.service.IAssRelcustomerinfoService;
import com.dgtly.common.core.text.Convert;

/**
 * 多经销商关联Service业务层处理
 * 
 * @author dgtly
 * @date 2021-04-08
 */
@Service
public class AssRelcustomerinfoServiceImpl implements IAssRelcustomerinfoService 
{
    @Autowired
    private AssRelcustomerinfoMapper assRelcustomerinfoMapper;

    /**
     * 查询多经销商关联
     * 
     * @param id 多经销商关联ID
     * @return 多经销商关联
     */
    @Override
    public AssRelcustomerinfo selectAssRelcustomerinfoById(Long id)
    {
        return assRelcustomerinfoMapper.selectAssRelcustomerinfoById(id);
    }

    /**
     * 查询多经销商关联列表
     * 
     * @param assRelcustomerinfo 多经销商关联
     * @return 多经销商关联
     */
    @Override
    public List<AssRelcustomerinfo> selectAssRelcustomerinfoList(AssRelcustomerinfo assRelcustomerinfo)
    {
        return assRelcustomerinfoMapper.selectAssRelcustomerinfoList(assRelcustomerinfo);
    }

    /**
     * 新增多经销商关联
     * 
     * @param assRelcustomerinfo 多经销商关联
     * @return 结果
     */
    @Override
    public int insertAssRelcustomerinfo(AssRelcustomerinfo assRelcustomerinfo)
    {
        assRelcustomerinfo.setCreateTime(DateUtils.getNowDate());
        return assRelcustomerinfoMapper.insertAssRelcustomerinfo(assRelcustomerinfo);
    }

    /**
     * 修改多经销商关联
     * 
     * @param assRelcustomerinfo 多经销商关联
     * @return 结果
     */
    @Override
    public int updateAssRelcustomerinfo(AssRelcustomerinfo assRelcustomerinfo)
    {
        assRelcustomerinfo.setUpdateTime(DateUtils.getNowDate());
        return assRelcustomerinfoMapper.updateAssRelcustomerinfo(assRelcustomerinfo);
    }

    /**
     * 删除多经销商关联对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAssRelcustomerinfoByIds(String ids)
    {
        return assRelcustomerinfoMapper.deleteAssRelcustomerinfoByIds(Convert.toStrArray(ids));
    }

    @Override
    public int deleteAssRelcustomerinfoByGlfs(String glfs) {
        return assRelcustomerinfoMapper.deleteAssRelcustomerinfoByGlfs(glfs);
    }

    @Override
    public int deleteAssRelcustomerinfoByUserId(String userId,String customerCode) {
        return assRelcustomerinfoMapper.deleteAssRelcustomerinfoByUserId(userId,customerCode);
    }

    /**
     * 删除多经销商关联信息
     * 
     * @param id 多经销商关联ID
     * @return 结果
     */
    @Override
    public int deleteAssRelcustomerinfoById(Long id)
    {
        return assRelcustomerinfoMapper.deleteAssRelcustomerinfoById(id);
    }

    @Override
    public List<AssRelcustomerinfo> selectRelcustomerinfoListByorgCode(String orgCode,String userId) {
        return assRelcustomerinfoMapper.selectRelcustomerinfoListByorgCode(orgCode,userId);
    }

    @Override
    public List<AssRelcustomerinfo> selectRelcustomerinfoByKunnr(String kunnr,String userId) {
        return assRelcustomerinfoMapper.selectRelcustomerinfoByKunnr(kunnr,userId);
    }

    @Override
    public void relationCustomerOnlineSync() throws ParseException {
        List<AssRelcustomerinfo> assRelList= assRelcustomerinfoMapper.selectAssRelCustomer();
       if(assRelList !=null && assRelList.size()>0){
        for (AssRelcustomerinfo ass:assRelList
             ) {
            if(ass.getCustomerType()!=null && ass.getCustomerType() !="") {
                List<String> typesMain = assRelcustomerinfoMapper.selectCustomerStart(ass.getMainkunnr(), Integer.valueOf(ass.getCustomerType()),0);
                List<String> typekuur = assRelcustomerinfoMapper.selectCustomerStart(ass.getKunnr(), Integer.valueOf(ass.getCustomerType()),0);
                if (typesMain.size() > 0 && typekuur.size() == 0) {
                    List<String> isyncList=assRelcustomerinfoMapper.selectCustomerByCustomerCode(ass.getKunnr());
                    if(isyncList.size()>0){
                        AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                        assinfo.setMainkunnr(ass.getKunnr());
                        assinfo.setMainname1(ass.getName1());
                        assinfo.setUpdateTime(DateUtils.getYesterDate());
                        assinfo.setCustomerType("3");
                        assRelcustomerinfoMapper.deleteCustomerStartByCustomerCode(ass.getKunnr());
                        assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }else{
                    AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                    assinfo.setMainkunnr(ass.getKunnr());
                    assinfo.setMainname1(ass.getName1());
                    assinfo.setUpdateTime(DateUtils.getYesterDate());
                    assinfo.setCustomerType(ass.getCustomerType());
                    assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }
                } else if (typesMain.size() == 0 && typekuur.size() > 0) {
                    List<String> isyncList=assRelcustomerinfoMapper.selectCustomerByCustomerCode(ass.getMainkunnr());
                    if(isyncList.size()>0){
                        AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                        assinfo.setMainkunnr(ass.getMainkunnr());
                        assinfo.setMainname1(ass.getMainname1());
                        assinfo.setUpdateTime(DateUtils.getYesterDate());
                        assinfo.setCustomerType("3");
                        assRelcustomerinfoMapper.deleteCustomerStartByCustomerCode(ass.getMainkunnr());
                        assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }else{
                    AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                    assinfo.setMainkunnr(ass.getMainkunnr());
                    assinfo.setMainname1(ass.getMainname1());
                    assinfo.setUpdateTime(DateUtils.getYesterDate());
                    assinfo.setCustomerType(ass.getCustomerType());
                    assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }
                }
                List<String> typesMain3 = assRelcustomerinfoMapper.selectCustomerStart(ass.getMainkunnr(), Integer.valueOf(ass.getCustomerType()),3);
                List<String> typekuur3 = assRelcustomerinfoMapper.selectCustomerStart(ass.getKunnr(), Integer.valueOf(ass.getCustomerType()),3);
                if (typesMain3.size() > 0 && typekuur3.size() == 0) {
                    List<String> isyncList=assRelcustomerinfoMapper.selectCustomerByCustomerCode(ass.getKunnr());
                    if(isyncList.size()>0){
                        AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                        assinfo.setMainkunnr(ass.getKunnr());
                        assinfo.setMainname1(ass.getName1());
                        assinfo.setUpdateTime(DateUtils.getYesterDate());
                        assinfo.setCustomerType("3");
                        assRelcustomerinfoMapper.deleteCustomerStartByCustomerCode(ass.getKunnr());
                        assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }else{
                    AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                    assinfo.setMainkunnr(ass.getKunnr());
                    assinfo.setMainname1(ass.getName1());
                    assinfo.setUpdateTime(DateUtils.getYesterDate());
                    assinfo.setCustomerType(ass.getCustomerType());
                    assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }
                } else if (typesMain3.size() == 0 && typekuur3.size() > 0) {
                    List<String> isyncList=assRelcustomerinfoMapper.selectCustomerByCustomerCode(ass.getMainkunnr());
                    if(isyncList.size()>0){
                        AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                        assinfo.setMainkunnr(ass.getMainkunnr());
                        assinfo.setMainname1(ass.getMainname1());
                        assinfo.setUpdateTime(DateUtils.getYesterDate());
                        assinfo.setCustomerType("3");
                        assRelcustomerinfoMapper.deleteCustomerStartByCustomerCode(ass.getMainkunnr());
                        assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }else{
                    AssRelcustomerinfo assinfo = new AssRelcustomerinfo();
                    assinfo.setMainkunnr(ass.getMainkunnr());
                    assinfo.setMainname1(ass.getMainname1());
                    assinfo.setUpdateTime(DateUtils.getYesterDate());
                    assinfo.setCustomerType(ass.getCustomerType());
                    assRelcustomerinfoMapper.relationCustomerOnlineSync(assinfo);
                    }
                }
            }
        }
       }
        //assRelcustomerinfoMapper.relationCustomerOnlineSync();
    }

    @Override
    public String selectCustomerName(String customerId) {
        return assRelcustomerinfoMapper.selectCustomerName(customerId);
    }
}
