package com.zd.airbottle.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.zd.airbottle.domain.QpAirGoodsConfig;
import com.zd.airbottle.domain.vo.QpAirGoodsConfigRelationVo;
import com.zd.airbottle.domain.vo.QpAirGoodsConfigVo;
import com.zd.airbottle.mapper.QpAirGoodsConfigMapper;
import com.zd.airbottle.service.IQpAirGoodsConfigService;
import com.zd.common.core.utils.bean.TransferUtils;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.RemoteDeptService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 货物配置Service业务层处理
 */
@Service
public class QpAirGoodsConfigServiceImpl implements IQpAirGoodsConfigService {
    @Autowired
    private QpAirGoodsConfigMapper qpAirGoodsConfigMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RemoteDeptService remoteDeptService;

    /**
     * 查询货物配置
     *
     * @param id 货物配置主键
     * @return 货物配置
     */
    @Override
    public QpAirGoodsConfig selectQpAirGoodsConfigById(Long id) {
        return qpAirGoodsConfigMapper.selectQpAirGoodsConfigById(id);
    }

    /**
     * 查询货物配置列表
     *
     * @param qpAirGoodsConfig 货物配置
     * @return 货物配置
     */
    @Override
    public List<QpAirGoodsConfig> selectQpAirGoodsConfigList(QpAirGoodsConfig qpAirGoodsConfig) {
        return qpAirGoodsConfigMapper.selectQpAirGoodsConfigList(qpAirGoodsConfig);
    }

    /**
     * 新增货物配置
     *
     * @param qpAirGoodsConfig 货物配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertQpAirGoodsConfig(QpAirGoodsConfig qpAirGoodsConfig) {
        return qpAirGoodsConfigMapper.insertQpAirGoodsConfig(qpAirGoodsConfig);
    }

    /**
     * 修改货物配置
     *
     * @param qpAirGoodsConfig 货物配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQpAirGoodsConfig(QpAirGoodsConfig qpAirGoodsConfig) {
        return qpAirGoodsConfigMapper.updateQpAirGoodsConfig(qpAirGoodsConfig);
    }

    /**
     * 批量删除货物配置
     *
     * @param ids 需要删除的货物配置主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteQpAirGoodsConfigByIds(Long[] ids) {
        return qpAirGoodsConfigMapper.deleteQpAirGoodsConfigByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByGoogsIds(Long[] googsIds) {
        return qpAirGoodsConfigMapper.deleteByGoogsIds(googsIds);
    }

    @Override
    public List<QpAirGoodsConfig> getListByGoogsIds(List<Long> googsIds) {
        return qpAirGoodsConfigMapper.getListByGoogsIds(googsIds);
    }

    /**
     * 根据配置ID查找其所有上级及其本身，此处待优化，尚未想到合理的方式可以根据配置ID集合批量逆向查询其所有上级
     *
     * @param configIds 配置ID集合
     * @return 以配置ID为key的map集合
     */
    @Override
    public Map<Long, QpAirGoodsConfigVo> getInfoByConfigIds(List<Long> configIds) {
        if (CollUtil.isNotEmpty(configIds)) {
            List<QpAirGoodsConfig> goodsConfigs = qpAirGoodsConfigMapper.getByIds(configIds);
            List<QpAirGoodsConfigVo> configVos = TransferUtils.transferList(goodsConfigs, QpAirGoodsConfigVo.class);
            if (!configVos.isEmpty())
                return configVos.stream().collect(Collectors.toMap(QpAirGoodsConfigVo::getId, c -> c));

        }
        return Collections.emptyMap();
    }

    /***
     * 查询供应商配置的气瓶
     */
    @Override
    public List<QpAirGoodsConfigRelationVo> selectQpAirGoodsConfigRelationList(QpAirGoodsConfigRelationVo relationVo) {

        /* 此处注释 切勿删除
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        AjaxResult dept = remoteDeptService.getInfoById(sysUser.getDept().getDeptId());
        LinkedHashMap<String,String> linkedHashMap = (LinkedHashMap<String, String>) dept.get("data");
        String deptId = linkedHashMap.get("ancestors");
        //查询登录用户所属的院校的供应商
        if("0".equals(deptId)){
            relationVo.setDeptId(Long.valueOf(linkedHashMap.get("dept_id")));
        }else{
            relationVo.setDeptId( Long.valueOf(deptId.split(",")[1]));
        }*/
        return qpAirGoodsConfigMapper.selectQpAirGoodsConfigRelationList(relationVo);
    }

    @Override
    public List<QpAirGoodsConfigVo> getGoodsInfo(Long goodsId) {
        List<QpAirGoodsConfig> goodsConfigs = qpAirGoodsConfigMapper.selectQpAirGoodsConfigList(new QpAirGoodsConfig().setGoodsId(goodsId));
        return TransferUtils.transferList(goodsConfigs, QpAirGoodsConfigVo.class);
    }

    @Override
    public List<QpAirGoodsConfigVo> getByIds(List<Long> ids) {
        List<QpAirGoodsConfigVo> goodsConfigs = new ArrayList<>();
        if (ids.isEmpty()) {
            return goodsConfigs;
        }
        List<QpAirGoodsConfig> configs = qpAirGoodsConfigMapper.getByIds(ids);
        return TransferUtils.transferList(configs, QpAirGoodsConfigVo.class);
    }
}
