/*
*  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.Gd;
import com.youchain.businessdata.domain.GdDetail;
import com.youchain.businessdata.domain.Pick;
import com.youchain.businessdata.repository.GdDetailRepository;
import com.youchain.businessdata.service.PickDetailService;
import com.youchain.businessdata.service.PickService;
import com.youchain.modules.system.domain.Dept;
import com.youchain.modules.system.service.DeptService;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.GdRepository;
import com.youchain.businessdata.service.GdService;
import com.youchain.businessdata.service.dto.GdDto;
import com.youchain.businessdata.service.dto.GdQueryCriteria;
import com.youchain.businessdata.service.mapstruct.GdMapper;
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 GdServiceImpl implements GdService {

    private final GdRepository gdRepository;
    private final GdDetailRepository gdDetailRepository;
    private final PickService pickService;
    private final PickDetailService pickDetailService;
    private final DeptService deptService;
    private final GdMapper gdMapper;

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

    @Override
    public List<GdDto> queryAll(GdQueryCriteria criteria){
        return gdMapper.toDto(gdRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GdDto findById(Long id) {
        Gd gd = gdRepository.findById(id).orElseGet(Gd::new);
        ValidationUtil.isNull(gd.getId(),"Gd","id",id);
        return gdMapper.toDto(gd);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GdDto create(Gd resources) {
        return gdMapper.toDto(gdRepository.save(resources));
    }

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

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

    @Override
    public void download(List<GdDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GdDto gd : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建人", gd.getCreateBy());
            map.put("修改人", gd.getUpdateBy());
            map.put("创建时间", gd.getCreateTime());
            map.put("修改时间", gd.getUpdateTime());
            map.put("工单编号", gd.getCode());
            map.put("任务编号", gd.getName());
            map.put("工单状态", gd.getStatus());
            map.put("需求工位", gd.getStation());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Gd save(String orderNo, String taskCode, String station) {
        List<Gd> gdList=gdRepository.findRepeat(orderNo,taskCode,station);
        if (gdList.size()>0){
            return gdList.get(0);
        }else {
            Gd gd=new Gd();
            gd.setCode(orderNo);
            gd.setName(taskCode);
            gd.setStation(station);
            gd.setStatus(BizStatus.OPEN);
            gd.setDept(deptService.isDept());
            gdRepository.save(gd);
            return gd;
        }
    }

    @Override
    public void addPickDetail(Gd gd) {
        int no=1;// 序号
        List<GdDetail> gdDetailList=gdDetailRepository.findByGd(gd.getId());
        for(GdDetail gdDetail : gdDetailList) {
            /*生成出库单*/
            Pick pick = pickService.save(gdDetail,no);
            no=no+1;
            /*生成出库明细*/
            pickDetailService.save(pick);
        }
    }
}