/*
 *  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 com.youchain.basicdata.service.impl;

import com.youchain.basicdata.domain.Point;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.PointRepository;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.dto.PointDto;
import com.youchain.basicdata.service.dto.PointQueryCriteria;
import com.youchain.basicdata.service.mapstruct.PointMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liuxue
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-07-26
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class PointServiceImpl implements PointService {

    private final PointRepository pointRepository;
    private final PointMapper pointMapper;

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

    @Override
    public List<PointDto> queryAll(PointQueryCriteria criteria) {
        return pointMapper.toDto(pointRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }


    @Override
    @Transactional
    public PointDto findById(Long id) {
        Point point = pointRepository.findById(id).orElseGet(Point::new);
        ValidationUtil.isNull(point.getId(), "Point", "id", id);
        return pointMapper.toDto(point);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PointDto create(Point resources) {
        return pointMapper.toDto(pointRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Point resources) {
        Point point = pointRepository.findById(resources.getId()).orElseGet(Point::new);
        ValidationUtil.isNull(point.getId(), "Point", "id", resources.getId());
        point.copy(resources);
        pointRepository.save(point);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            pointRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PointDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PointDto point : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("RCS代码", point.getCode());
            map.put("名称", point.getName());
            map.put("状态", point.getStatus());
            map.put("描述", point.getDescription());
            map.put("区域编码", point.getBeatCode());
            map.put("物料编码", point.getItemCode());
            map.put("库区名称", point.getArea().getName());
            map.put("是否启用", point.getEnabled());
            map.put("仓库名称", point.getDept().getName());
            map.put("坐标X", point.getPosX());
            map.put("坐标Y", point.getPosY());
            map.put("坐标Z", point.getPosZ());
            map.put("类型", point.getType());
            map.put("热度", point.getHeat());
            map.put("创建人", point.getCreateBy());
            map.put("修改人", point.getUpdateBy());
            map.put("创建时间", point.getCreateTime());
            map.put("修改时间", point.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Point toEntity(PointDto pointDto) {
        return pointMapper.toEntity(pointDto);
    }


    @Override
    public List<Point> findByTypeAndAreaCode(String type, String areaCode) {
        return pointRepository.findByTypeAndAreaCode(type, areaCode);
    }

    @Override
    public Page<Point> findByTypeAndAreaCode(String type, String areaCode, int page, int pageSize) {
        Pageable pageable = PageRequest.of(page - 1, pageSize);
        return pointRepository.findByTypeAndAreaCode(type, areaCode, pageable);
    }

    @Override
    public Point findByCode(String code, String status, String type, String areaCode, String goodType, Double itemHeight) {
        List<Point> pointList=pointRepository.findByCode(code, status, type, areaCode, goodType, itemHeight);
        if(pointList.isEmpty()){
            return null;
        }else{
            return pointList.get(0);
        }
    }

    @Override
    public Map<String, Point> findByCodes(Set pointCodes) {
        List<Point> pointList = pointRepository.findByCodes(pointCodes);
        Map<String, Point> pointMap = new HashMap<>();
        for (Point point : pointList) {
            pointMap.put(point.getCode(), point);
        }
        return pointMap;
    }

    @Override
    public List<Point> findByByBeatCode(String beatCode) {
        return pointRepository.findByByBeatCode(beatCode);
    }
}
