/*
*  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.GdDetail;
import com.youchain.businessdata.domain.Pick;
import com.youchain.utils.*;
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 huojin
* @date 2024-06-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("创建人", pick.getCreateBy());
            map.put("修改人", pick.getUpdateBy());
            map.put("创建时间", pick.getCreateTime());
            map.put("修改时间", pick.getUpdateTime());
            map.put("仓库ID", pick.getDeptId());
            map.put("出库单头", pick.getCode());
            map.put("序号", pick.getLineNo());
            map.put("状态", pick.getStatus());
            map.put("关联工单明细", pick.getGdDetaiId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Pick save(GdDetail gdDetail, int no) {
        Pick pick=new Pick();
        pick.setGdDetail(gdDetail);
        pick.setLineNo(no);
        pick.setCode(gdDetail.getGd().getName()+"_"+no);
        pick.setDept(UserUtils.getDept());
        pickRepository.save(pick);
        return pick;
    }
}