package com.yc.ship.module.resource.service.channel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.channel.vo.ScenicChannelPageReqVO;
import com.yc.ship.module.resource.controller.admin.channel.vo.ScenicChannelSaveReqVO;
import com.yc.ship.module.resource.controller.admin.vo.TreeNodeVO;
import com.yc.ship.module.resource.dal.dataobject.channel.ScenicChannelDO;
import com.yc.ship.module.resource.dal.mysql.channel.ScenicChannelMapper;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 通道 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ScenicChannelServiceImpl implements ScenicChannelService {

    @Resource
    private ScenicChannelMapper scenicChannelMapper;

    @Override
    public Long createScenicChannel(ScenicChannelSaveReqVO createReqVO) {
        // 插入
        ScenicChannelDO scenicChannel = BeanUtils.toBean(createReqVO, ScenicChannelDO.class);
        Long id = IdWorker.getId(scenicChannel);
        scenicChannel.setId(id);
        scenicChannel.setDeleted(false);
        scenicChannelMapper.insert(scenicChannel);
        // 返回
        return scenicChannel.getId();
    }

    @Override
    public void updateScenicChannel(ScenicChannelSaveReqVO updateReqVO) {
        // 校验存在
        validateScenicChannelExists(updateReqVO.getId());
        // 更新
        ScenicChannelDO updateObj = BeanUtils.toBean(updateReqVO, ScenicChannelDO.class);
        scenicChannelMapper.updateById(updateObj);
    }

    @Override
    public void deleteScenicChannel(Long id) {
        // 校验存在
        validateScenicChannelExists(id);
        // 删除
        scenicChannelMapper.deleteById(id);
    }

    private void validateScenicChannelExists(Long id) {
        if (scenicChannelMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.SCENIC_CHANNEL_NOT_EXISTS);
        }
    }

    @Override
    public ScenicChannelDO getScenicChannel(Long id) {
        return scenicChannelMapper.selectById(id);
    }

    @Override
    public PageResult<ScenicChannelDO> getScenicChannelPage(ScenicChannelPageReqVO pageReqVO) {
        return scenicChannelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ScenicChannelDO> getChannelListByStatus(String siteId) {
        if (StringUtils.isBlank(siteId)) {
            return scenicChannelMapper.selectList(ScenicChannelDO::getIsUse, 1);
        } else {
            return scenicChannelMapper.selectList(ScenicChannelDO::getSiteId, siteId, ScenicChannelDO::getIsUse, 1);
        }
    }

    @Override
    public List<ScenicChannelDO> getChannelList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return scenicChannelMapper.selectBatchIds(ids);
    }

    /**
     * 获得用户 Map
     *
     * @param ids 站点编号数组
     * @return 站点 Map
     */
    @Override
    public Map<Long, ScenicChannelDO> getChannelMap(Collection<Long> ids) {
        List<ScenicChannelDO> users = getChannelList(ids);
        return CollectionUtils.convertMap(users, ScenicChannelDO::getId);
    }

    @Override
    public List<TreeNodeVO> getCheckChannelList() {
        return scenicChannelMapper.queryCheckChannelList();
    }

    @Override
    public List<Tree<String>> getPdaCheckChannelList() {
        List<TreeNodeVO> dataList = this.getCheckChannelList();

        List<TreeNode<String>> treeNodes=  dataList.stream().map(treeNodeVO -> {
            TreeNode<String> node = new TreeNode<>();
            //下面四个属性是树型结构必有的属性
            node.setId(treeNodeVO.getId().toString());
            node.setName(treeNodeVO.getName());
            node.setParentId(treeNodeVO.getParentId().toString());
            return node;
        }).collect(Collectors.toList());
        return TreeUtil.build(treeNodes,"0");
    }
}
