/*
*  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.PointItem;
import com.youchain.utils.FileUtil;
import com.youchain.utils.PageUtil;
import com.youchain.utils.QueryHelp;
import com.youchain.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.PointItemRepository;
import com.youchain.basicdata.service.PointItemService;
import com.youchain.basicdata.service.dto.PointItemDto;
import com.youchain.basicdata.service.dto.PointItemQueryCriteria;
import com.youchain.basicdata.service.mapstruct.PointItemMapper;
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.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author admin
* @date 2025-04-11
**/
@Service
@RequiredArgsConstructor
public class PointItemServiceImpl implements PointItemService {

    private final PointItemRepository pointItemRepository;
    private final PointItemMapper pointItemMapper;

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

    @Override
    public List<PointItemDto> queryAll(PointItemQueryCriteria criteria){
        return pointItemMapper.toDto(pointItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PointItemDto findById(Long id) {
        PointItem pointItem = pointItemRepository.findById(id).orElseGet(PointItem::new);
        ValidationUtil.isNull(pointItem.getId(),"PointItem","id",id);
        return pointItemMapper.toDto(pointItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PointItemDto create(PointItem resources) {
        resources.setCode(resources.getPoint().getCode()+":"+resources.getItem().getCode());
        resources.setEnabled(true);
        return pointItemMapper.toDto(pointItemRepository.save(resources));
    }

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

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

    @Override
    public void download(List<PointItemDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PointItemDto pointItem : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编号", pointItem.getCode());
            map.put("名称", pointItem.getName());
            map.put("物料ID", pointItem.getItem().getCode());
            map.put("点位ID", pointItem.getPoint().getCode());
            map.put("状态：1启用、0禁用", pointItem.getEnabled());
            map.put("工单号", pointItem.getOrderNumber());
            map.put("创建人", pointItem.getCreateBy());
            map.put("修改人", pointItem.getUpdateBy());
            map.put("创建时间", pointItem.getCreateTime());
            map.put("修改时间", pointItem.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}