package cn.superdesk.uniorder.service.sys.impl;

import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.dao.entity.sys.SysOderUser;
import cn.superdesk.uniorder.dao.entity.sys.SysOderUserExample;
import cn.superdesk.uniorder.dao.mapper.sys.SysOderUserMapper;
import cn.superdesk.uniorder.dao.mapper.sys.SysOderUserMapperEx;
import cn.superdesk.uniorder.service.sys.SysOderUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 此大概为 服务之窗 同步过来的数据
 * @Author: linjinyu
 * @CreateDate: 2020/11/13 下午3:25
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020/11/13 下午3:25
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class SysOderUserServiceImpl implements SysOderUserService {

    @Autowired
    private SysOderUserMapper   sysOderUserMapper;
    @Autowired
    private SysOderUserMapperEx sysOderUserMapperEx;


    @Override
    public int insertSelective(SysOderUser t) {
        return sysOderUserMapper.insertSelective(t);

    }

    @Override
    public int updateByPrimaryKeySelective(SysOderUser t) {
        return sysOderUserMapper.updateByPrimaryKey(t);

    }

    @Override
    public void deleteObj(Long id) {


    }

    @Override
    public SysOderUser findById(Long id) {
        return sysOderUserMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<SysOderUser> findAll(Map<String, Object> queryMap) {
        return sysOderUserMapperEx.findAll(queryMap);
    }

    @Override
    public PageInfo<SysOderUser> findByPaging(Map<String, Object> params,
                                              Integer pageNo, Integer pageSize) {
        return null;
    }

    @Override
    public List<SysOderUser> findByUserId(Long orgId) {
        return sysOderUserMapperEx.findByUserId(orgId);
    }

    @Override
    public SysOderUser selectByUserId(Long userId) {

        return sysOderUserMapperEx.selectByUserId(userId);
    }

    @Override
    public List<SysOderUser> findByName(Long outOrganizationId, Long outEnterpriseId, String realName) {

        SysOderUserExample          example  = new SysOderUserExample();
        SysOderUserExample.Criteria criteria = example.createCriteria();
        criteria.andOrganizationIdEqualTo(outOrganizationId);
        criteria.andEnterpriseIdEqualTo(outEnterpriseId);

        if (StringUtils.isNotBlank(realName)) {
            criteria.andRealnameLike("%" + realName + "%");
        }

        return sysOderUserMapper.selectByExample(example);
    }


    public List<SysOderUser> findByOutUserIds(List<Long> outUserIds) {

        SysOderUserExample example = new SysOderUserExample();
        example.createCriteria()
                .andUserIdIn(outUserIds);
        List<SysOderUser> result = sysOderUserMapper.selectByExample(example);

        return result;

    }

    /****************************************** 工具方法 start ******************************************/

    public Map<Long, SysOderUser> convertSysOderUserIds2MapKeyValue(List<Long> outUserIds) {

        Map<Long, SysOderUser> resultMap = new HashMap<>();

        if (AnCollectionUtils.isNotEmpty(outUserIds)) {
            List<SysOderUser> resultList = findByOutUserIds(outUserIds);

            if (AnCollectionUtils.isNotEmpty(resultList)) {
                for (SysOderUser entity : resultList) {
                    resultMap.put(entity.getId(), entity);
                }
            }
        }


        return resultMap;

    }

    public List<String> convertSysOderUserIds2SysFullNames(List<Long> outUserIds) {

        List<String> result = new ArrayList<>();

        List<SysOderUser> sysOderUserList = findByOutUserIds(outUserIds);

        if (AnCollectionUtils.isNotEmpty(sysOderUserList)) {
            result = sysOderUserList.stream().filter(AnLamdaUtils.distinctByKey(o -> o.getId())).map(SysOderUser::getRealname).collect(Collectors.toList());
        }

        return result;

    }

    /****************************************** 工具方法   end ******************************************/

}
