/*
*  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.BomAccount;
import com.youchain.basicdata.domain.BomAccountLog;
import com.youchain.basicdata.repository.BomAccountRepository;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.BomAccountLogRepository;
import com.youchain.basicdata.service.BomAccountLogService;
import com.youchain.basicdata.service.dto.BomAccountLogDto;
import com.youchain.basicdata.service.dto.BomAccountLogQueryCriteria;
import com.youchain.basicdata.service.mapstruct.BomAccountLogMapper;
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 houjianlan
* @date 2024-04-24
**/
@Service
@RequiredArgsConstructor
public class BomAccountLogServiceImpl implements BomAccountLogService {

    private final BomAccountLogRepository bomAccountLogRepository;
    private final BomAccountLogMapper bomAccountLogMapper;
    private final BomAccountRepository bomAccountRepository;

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

    @Override
    public List<BomAccountLogDto> queryAll(BomAccountLogQueryCriteria criteria){
        return bomAccountLogMapper.toDto(bomAccountLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BomAccountLogDto findById(Long id) {
        BomAccountLog bomAccountLog = bomAccountLogRepository.findById(id).orElseGet(BomAccountLog::new);
        ValidationUtil.isNull(bomAccountLog.getId(),"BomAccountLog","id",id);
        return bomAccountLogMapper.toDto(bomAccountLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BomAccountLogDto create(BomAccountLog resources) {
        return bomAccountLogMapper.toDto(bomAccountLogRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BomAccountLogDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>>  list= ExcelDownUtils.CreateMap(all,"BomAccountLog");
        FileUtil.downloadExcel(list, response);
    }

    public void copyBomAccount(Long id,String operType){
        BomAccount bomAccount=bomAccountRepository.getById(id);
        BomAccountLog log=new BomAccountLog();
        log.setOperType(operType);
        log.setBomAccountId(bomAccount.getId());
        log.setDept(bomAccount.getDept());
        log.setAc(bomAccount.getAc());
        log.setBonded(bomAccount.getBonded());
        log.setContents(bomAccount.getContents());
        log.setSingles(bomAccount.getSingles());
        log.setSupplier(bomAccount.getSupplier());
        log.setStationType(bomAccount.getStationType());
        log.setOutType(bomAccount.getOutType());
        log.setBigItem(bomAccount.getBigItem());
        log.setEnabled(bomAccount.getEnabled());
        log.setItem(bomAccount.getItem());
        log.setBp_type(bomAccount.getBp_type());
        log.setCArea(bomAccount.getCArea());
        log.setRArea(bomAccount.getRArea());
        log.setHPoint(bomAccount.getHPoint());
        log.setZPoint(bomAccount.getZPoint());
        log.setDescription(bomAccount.getDescription());
        this.create(log);
    }
}