/*
*  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.Pick;
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.businessdata.repository.PickRepository;
import com.youchain.businessdata.service.PickService;
import com.youchain.businessdata.service.dto.PickDto;
import com.youchain.businessdata.service.dto.PickQueryCriteria;
import com.youchain.businessdata.service.mapstruct.PickMapper;
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 hjl
* @date 2024-03-11
**/
@Service
@RequiredArgsConstructor
public class PickServiceImpl implements PickService {

    private final PickRepository pickRepository;
    private final PickMapper pickMapper;

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

    @Override
    public List<PickDto> queryAll(PickQueryCriteria criteria){
        return pickMapper.toDto(pickRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PickDto findById(Long id) {
        Pick pick = pickRepository.findById(id).orElseGet(Pick::new);
        ValidationUtil.isNull(pick.getId(),"Pick","id",id);
        return pickMapper.toDto(pick);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PickDto create(Pick resources) {
        return pickMapper.toDto(pickRepository.save(resources));
    }

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

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

    @Override
    public void download(List<PickDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PickDto pick : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("仓库ID", pick.getDeptId());
            map.put("单据类型ID", pick.getBillTypeId());
            map.put("订单来源", pick.getOrderOrigin());
            map.put("优先级", pick.getPriority());
            map.put("货主", pick.getOwner());
            map.put("系统单号", pick.getRelatedBill1());
            map.put("客户订单号", pick.getRelatedBill2());
            map.put("来源名称", pick.getSourceName());
            map.put("订单状态", pick.getStatus());
            map.put("合并或分阶段处理的地点", pick.getAddress());
            map.put("发货时间", pick.getDispatchDate());
            map.put("操作时间", pick.getErTime());
            map.put("创建人", pick.getCreateBy());
            map.put("修改人", pick.getUpdateBy());
            map.put("创建时间", pick.getCreateTime());
            map.put("修改时间", pick.getUpdateTime());
            map.put("订单时间", pick.getOrderDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}