package com.gci.boot.modules.basedata.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.modules.basedata.entity.TbCustomerArchives;
import com.gci.boot.modules.basedata.mapper.TbCustomerArchivesMapper;
import com.gci.boot.modules.basedata.service.ITbCustomerArchivesService;
import com.gci.boot.modules.system.entity.SysDepart;
import com.gci.boot.modules.system.service.ISysDepartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TbCustomerArchivesServiceImpl extends ServiceImpl<TbCustomerArchivesMapper, TbCustomerArchives> implements ITbCustomerArchivesService {

    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private TbCustomerArchivesMapper tbCustomerArchivesMapper;

    @Override
    public boolean check(TbCustomerArchives archives) {
        LambdaQueryWrapper<TbCustomerArchives> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.eq(TbCustomerArchives::getName,archives.getName())
                .or().eq(TbCustomerArchives::getShortened,archives.getShortened()))
                .ne(StrUtil.isNotBlank(archives.getId()), TbCustomerArchives::getId,archives.getId());
        int count = count(queryWrapper);
        return count <= 0;
    }

    @Override
    public String getCustomerByUserId(String userId) {
        String result = null;

        List<SysDepart> departList = sysDepartService.getDepartByUserId(userId);
        //flag标识是否是客户公司身份 1_普通部门,2_维修厂,3_客户公司
        List<String> listDepart = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(departList)){
            for(SysDepart depart: departList){
                if(depart.getOrgCategory()==1){
                    return result;
                }else if(depart.getOrgCategory()==2){
                    return result;
                }else if(depart.getOrgCategory()==3){
                    listDepart.add(depart.getOrgCode());
                }
            }
        }
        if(CollectionUtils.isEmpty(listDepart)){
            return result;
        }
        List<TbCustomerArchives> listTbCustomerArchives = list(new QueryWrapper<TbCustomerArchives>().in("sys_org_code", listDepart));
        //返回第一个客户id：一个部门绑定多个客户的情况需要从业务上禁止
        return listTbCustomerArchives.get(0).getId();
    }

    @Override
    public boolean checkDuplicateDepart(String deparCode,String customerId) {
        if(StringUtils.isEmpty(deparCode)){
            return false;
        }
        if(StringUtils.isEmpty(customerId)){
            //新增场景
            List<TbCustomerArchives> listCustomerAdd = tbCustomerArchivesMapper.selectList(new QueryWrapper<TbCustomerArchives>().eq("sys_org_code", deparCode));
            if(CollectionUtils.isEmpty(listCustomerAdd)){
                return false;
            }else{
                return true;
            }
        }else{
            //更新场景
            List<TbCustomerArchives> listCustomerUpdate = tbCustomerArchivesMapper.selectList(new QueryWrapper<TbCustomerArchives>().eq("sys_org_code", deparCode).ne("id",customerId));
            if(CollectionUtils.isEmpty(listCustomerUpdate)){
                return false;
            }else{
                return true;
            }
        }
    }

    @Override
    public TbCustomerArchives getByProjectId(String projectId) {
        if(StringUtils.isEmpty(projectId)){
            return null;
        }
        LambdaQueryWrapper<TbCustomerArchives> queryWrapper = new LambdaQueryWrapper<>();
        String sql = "select 1 from tb_projects p where tb_customer_archives.id = p.customer_id and p.id='"+projectId+"'";
        queryWrapper.exists(sql);
        List<TbCustomerArchives> list = list(queryWrapper);
        return list.size()>0?list.get(0):null;
    }

}
