/*
 *  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.businessdata.service.impl;

import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.repository.*;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.service.PickDetailService;
import com.youchain.businessdata.service.dto.PickDetailDto;
import com.youchain.businessdata.service.dto.PickDetailQueryCriteria;
import com.youchain.businessdata.service.mapstruct.PickDetailMapper;
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.servlet.http.HttpServletResponse;

/**
 * @author huojin
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-08-16
 **/
@Service
@RequiredArgsConstructor
public class PickDetailServiceImpl implements PickDetailService {
    private final PickDetailRepository pickDetailRepository;
    private final PickDetailMapper pickDetailMapper;

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

    @Override
    public List<PickDetailDto> queryAll(PickDetailQueryCriteria criteria) {
        return pickDetailMapper.toDto(pickDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public PickDetail findById(Long id) {
        return pickDetailRepository.findById(id).orElseGet(PickDetail::new);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PickDetailDto create(PickDetail resources) {
        return pickDetailMapper.toDto(pickDetailRepository.save(resources));
    }

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

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

    @Override
    public void download(List<PickDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        int i = 1;
        for (PickDetailDto pickDetail : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("序号", i++);
            map.put("任务号", pickDetail.getPo());
            map.put("物料编码", pickDetail.getItem().getCode());
            map.put("物料名称", pickDetail.getItem().getName());
            map.put("状态", pickDetail.getStatus());
            map.put("订单数量", pickDetail.getOrderQty());
            map.put("分配数量", pickDetail.getAllocatedQty());
            map.put("拣货数量", pickDetail.getPickedQty());
            map.put("备注", pickDetail.getRemark());
            map.put("创建人", pickDetail.getCreateBy());
            map.put("创建时间", pickDetail.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public long count() {
        return pickDetailRepository.count();
    }

    @Override
    public List<PickDetail> findByAllPickDetail(Long pickId) {
        return pickDetailRepository.findByAllPickDetail(pickId);
    }

    @Override
    public List<PickDetail> findByPickDetailNoAllocate(Long pickId) {
        return pickDetailRepository.findByPickDetailNoAllocate(pickId);
    }

}
