/*
*  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.TaskLog;
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.TaskLogRepository;
import com.youchain.businessdata.service.TaskLogService;
import com.youchain.businessdata.service.dto.TaskLogDto;
import com.youchain.businessdata.service.dto.TaskLogQueryCriteria;
import com.youchain.businessdata.service.mapstruct.TaskLogMapper;
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 2023-08-25
**/
@Service
@RequiredArgsConstructor
public class TaskLogServiceImpl implements TaskLogService {

    private final TaskLogRepository taskLogRepository;
    private final TaskLogMapper taskLogMapper;

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

    @Override
    public List<TaskLogDto> queryAll(TaskLogQueryCriteria criteria){
        return taskLogMapper.toDto(taskLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TaskLogDto findById(Long id) {
        TaskLog taskLog = taskLogRepository.findById(id).orElseGet(TaskLog::new);
        ValidationUtil.isNull(taskLog.getId(),"TaskLog","id",id);
        return taskLogMapper.toDto(taskLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TaskLogDto create(TaskLog resources) {
        return taskLogMapper.toDto(taskLogRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TaskLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TaskLogDto taskLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("物料序号", taskLog.getItem());
            map.put("物料属性序号", taskLog.getItemKey());
            map.put("任务序号", taskLog.getTask());
            map.put("源容器", taskLog.getSrcStock());
            map.put("目标容器", taskLog.getDstStock());
            map.put("源点位", taskLog.getSrcPoint());
            map.put("目标点位", taskLog.getDstPoint());
            map.put("源容器编码", taskLog.getSrcStockCode());
            map.put("目标容器编码", taskLog.getDstStockCode());
            map.put("源点位编码", taskLog.getSrcLocCode());
            map.put("目标点位编码", taskLog.getDstLocCode());
            map.put("属性6", taskLog.getInvStatus());
            map.put("退回", taskLog.getBeBack());
            map.put("拣货数量", taskLog.getMoveQty());
            map.put("发运数量", taskLog.getShipQty());
            map.put("上架号", taskLog.getPutCode());
            map.put("库存ID", taskLog.getInvId());
            map.put("仓库ID", taskLog.getDept());
            map.put("创建人", taskLog.getCreateBy());
            map.put("修改人", taskLog.getUpdateBy());
            map.put("发运人", taskLog.getShipBy());
            map.put("发运时间", taskLog.getShipTime());
            map.put("创建时间", taskLog.getCreateTime());
            map.put("修改时间", taskLog.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}
