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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.guidegroup.vo.GuideGroupPageReqVO;
import com.yc.ship.module.resource.controller.admin.guidegroup.vo.GuideGroupSaveReqVO;
import com.yc.ship.module.resource.controller.admin.vo.SimpleListReqVO;
import com.yc.ship.module.resource.dal.dataobject.guidegroup.GuideGroupDO;
import com.yc.ship.module.resource.dal.mysql.guidegroup.GuideGroupMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.*;


/**
 * 导游分组 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class GuideGroupServiceImpl implements GuideGroupService {

    @Resource
    private GuideGroupMapper guideGroupMapper;

    @Override
    public String createGuideGroup(GuideGroupSaveReqVO createReqVO) {
        // 插入
        GuideGroupDO guideGroup = BeanUtils.toBean(createReqVO, GuideGroupDO.class);
        guideGroup.setId(IdWorker.getIdStr(guideGroup));
        guideGroup.setDeleted(false);
        guideGroupMapper.insert(guideGroup);
        // 返回
        return guideGroup.getId();
    }

    @Override
    public void updateGuideGroup(GuideGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateGuideGroupExists(updateReqVO.getId());
        // 更新
        GuideGroupDO updateObj = BeanUtils.toBean(updateReqVO, GuideGroupDO.class);
        guideGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteGuideGroup(String id) {
        // 校验存在
        validateGuideGroupExists(id);
        // 校验是否有子分组
        if (guideGroupMapper.selectCountByParentId(id) > 0) {
            throw exception(GUIDE_GROUP_EXITS_CHILDREN);
        }
        // 删除
        guideGroupMapper.deleteById(id);
    }

    private void validateGuideGroupExists(String id) {
        if (guideGroupMapper.selectById(id) == null) {
            throw exception(GUIDE_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public GuideGroupDO getGuideGroup(String id) {
        return guideGroupMapper.selectById(id);
    }

    @Override
    public PageResult<GuideGroupDO> getGuideGroupPage(GuideGroupPageReqVO pageReqVO) {
        return guideGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GuideGroupDO> getGroupList(SimpleListReqVO listReqVO) {
        List<GuideGroupDO> list = guideGroupMapper.selectList(listReqVO);
//        list.sort(Comparator.comparing(GuideGroupDO::getSort));
        return list;
    }

    @Override
    public List<String> getGroupAndAllChildren(String id) {
        List<GuideGroupDO> list = guideGroupMapper.selectList(SimpleListReqVO.builder().build());
        List<String> idList = getAllChildren(list, id);
        idList.add(id);
        return idList;
    }

    public static List<String> getAllChildren(List<GuideGroupDO> items, String parentId) {
        List<String> children = new ArrayList<>();
        for (GuideGroupDO item : items) {
            if (parentId.equals(item.getParentId())) {
                children.add(item.getId());
                List<String> grandChildren = getAllChildren(items, item.getId());
                children.addAll(grandChildren);
            }
        }
        return children;
    }


}
