/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.dmLane.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.dm.company.domain.DmCompany;
import me.zhengjie.modules.dm.dmLane.domain.DmLane;
import me.zhengjie.modules.dm.dmLane.repository.DmLaneRepository;
import me.zhengjie.modules.dm.dmLane.service.DmLaneService;
import me.zhengjie.modules.dm.dmLane.service.dto.DmLaneDto;
import me.zhengjie.modules.dm.dmLane.service.dto.DmLaneQueryCriteria;
import me.zhengjie.modules.dm.dmLane.service.mapstruct.DmLaneMapper;
import me.zhengjie.modules.dmuser.domain.DmUser;
import me.zhengjie.modules.dmuser.service.DmUserService;
import me.zhengjie.modules.dmuser.service.dto.DmUserDto;
import me.zhengjie.modules.dmuser.service.mapstruct.DmUserMapper;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhirong
* @date 2021-11-10
**/
@Service
@RequiredArgsConstructor
public class DmLaneServiceImpl implements DmLaneService {

    private final DmLaneRepository dmLaneRepository;
    private final DmLaneMapper dmLaneMapper;

    @Override
    public Map<String,Object> queryAll(DmLaneQueryCriteria criteria, Pageable pageable){
        Page<DmLane> page = dmLaneRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dmLaneMapper::toDto));
    }

    @Override
    public List<DmLaneDto> queryAll(DmLaneQueryCriteria criteria){
        return dmLaneMapper.toDto(dmLaneRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmLaneDto create(DmLane resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId((int) snowflake.nextId());
        return dmLaneMapper.toDto(dmLaneRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmLane resources) throws IOException {
        DmLane dmCompany = dmLaneRepository.findById(resources.getId()).orElseGet(DmLane::new);
        ValidationUtil.isNull( dmCompany.getId(),"DmLane","id",resources.getId());
        dmCompany.copy(resources);
        dmLaneRepository.save(dmCompany);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer userName : ids) {
            dmLaneRepository.deleteById(userName);
        }
    }
}