/*
*  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.basicdata.domain.BigItem;
import com.youchain.basicdata.domain.Item;
import com.youchain.businessdata.domain.Gd;
import com.youchain.businessdata.domain.GdDetail;
import com.youchain.modules.system.service.DeptService;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.GdDetailRepository;
import com.youchain.businessdata.service.GdDetailService;
import com.youchain.businessdata.service.dto.GdDetailDto;
import com.youchain.businessdata.service.dto.GdDetailQueryCriteria;
import com.youchain.businessdata.service.mapstruct.GdDetailMapper;
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-06
**/
@Service
@RequiredArgsConstructor
public class GdDetailServiceImpl implements GdDetailService {

    private final GdDetailRepository gdDetailRepository;
    private final GdDetailMapper gdDetailMapper;
    private final DeptService deptService;

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

    @Override
    public List<GdDetailDto> queryAll(GdDetailQueryCriteria criteria){
        return gdDetailMapper.toDto(gdDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GdDetailDto findById(Long id) {
        GdDetail gdDetail = gdDetailRepository.findById(id).orElseGet(GdDetail::new);
        ValidationUtil.isNull(gdDetail.getId(),"GdDetail","id",id);
        return gdDetailMapper.toDto(gdDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GdDetailDto create(GdDetail resources) {
        return gdDetailMapper.toDto(gdDetailRepository.save(resources));
    }

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

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

    @Override
    public void download(List<GdDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GdDetailDto gdDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建人", gdDetail.getCreateBy());
            map.put("修改人", gdDetail.getUpdateBy());
            map.put("创建时间", gdDetail.getCreateTime());
            map.put("修改时间", gdDetail.getUpdateTime());
            map.put("行号", gdDetail.getLineNo());
            map.put("关联物料", gdDetail.getItemId());
            map.put("订单数量", gdDetail.getItemQty());
            map.put("总成代码", gdDetail.getBigItemId());
            map.put("总成套数", gdDetail.getBigItemQty());
            map.put("单据类型", gdDetail.getOrderType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public GdDetail save(Gd gd, String orderType, BigItem bigItem, Double completeQty, Item item, Double itemQty) {
        GdDetail gdDetail= new GdDetail();
        List<GdDetail> gdList;
        if (orderType.equals(BaseStatus.GD_TYPE_DP)){
            gdList=gdDetailRepository.findRepeat(gd.getId(),null,item.getId(),BaseStatus.GD_TYPE_DP);
        }else {
            gdList=gdDetailRepository.findRepeat(gd.getId(),bigItem.getId(),item.getId(),BaseStatus.GD_TYPE_CT);
        }
        if (gdList.size()>0){
            gdDetail=gdList.get(0);
        }
        gdDetail.setGd(gd);
        gdDetail.setItem(item);
        gdDetail.setItemQty(itemQty);
        gdDetail.setBigItem(bigItem);
        gdDetail.setBigItemQty(completeQty);
        gdDetail.setDept(deptService.isDept());
        gdDetail.setOrderType(orderType);
        gdDetailRepository.save(gdDetail);
        return gdDetail;
    }
}