/*
*  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.CountDetail;
import com.youchain.businessdata.domain.CountRecord;
import com.youchain.businessdata.domain.Inventory;
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.CountRecordRepository;
import com.youchain.businessdata.service.CountRecordService;
import com.youchain.businessdata.service.dto.CountRecordDto;
import com.youchain.businessdata.service.dto.CountRecordQueryCriteria;
import com.youchain.businessdata.service.mapstruct.CountRecordMapper;
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 2023-10-20
**/
@Service
@RequiredArgsConstructor
public class CountRecordServiceImpl implements CountRecordService {

    private final CountRecordRepository countRecordRepository;
    private final CountRecordMapper countRecordMapper;

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

    @Override
    public List<CountRecordDto> queryAll(CountRecordQueryCriteria criteria){
        return countRecordMapper.toDto(countRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CountRecordDto findById(Long id) {
        CountRecord countRecord = countRecordRepository.findById(id).orElseGet(CountRecord::new);
        ValidationUtil.isNull(countRecord.getId(),"CountRecord","id",id);
        return countRecordMapper.toDto(countRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CountRecordDto create(CountRecord resources) {
        return countRecordMapper.toDto(countRecordRepository.save(resources));
    }

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

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


    @Override
    public void download(List<CountRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CountRecordDto countRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("物料序号", countRecord.getItem());
            map.put("物料序号", countRecord.getItemKey());
            map.put("单据号", countRecord.getBillCode());
            map.put("盘点明细ID", countRecord.getCountDetail());
            map.put("源容器", countRecord.getSrcStock());
            map.put("目标容器", countRecord.getDstStock());
            map.put("源点位", countRecord.getSrcPoint());
            map.put("目标点位", countRecord.getDstPoint());
            map.put("源容器编码", countRecord.getSrcStockCode());
            map.put("目标容器编码", countRecord.getDstStockCode());
            map.put("源点位编码", countRecord.getSrcPointCode());
            map.put("目标点位编码", countRecord.getDstPointCode());
            map.put("属性6", countRecord.getInvStatus());
            map.put("属性6", countRecord.getTaskStatus());
            map.put("跳过", countRecord.getBeSkip());
            map.put("退回", countRecord.getBeBack());
            map.put("计划数量", countRecord.getPlanQty());
            map.put("移位数量", countRecord.getCountQty());
            map.put("库存ID", countRecord.getInvId());
            map.put("仓库ID", countRecord.getDept());
            map.put("创建人", countRecord.getCreateBy());
            map.put("修改人", countRecord.getUpdateBy());
            map.put("创建时间", countRecord.getCreateTime());
            map.put("修改时间", countRecord.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public CountRecord createCountRecord(CountDetail d, Inventory inv) {
        CountRecord cr = new CountRecord();
        cr.setItemKey(inv.getItemKey());
        cr.setItem(inv.getItemKey().getItem());
        cr.setCountDetail(d);
        cr.setPlanQty(inv.getQuantity());
        cr.setSrcPoint(inv.getPoint());
        cr.setSrcPointCode(inv.getPointCode());
        cr.setSrcStock(inv.getStock());
        cr.setSrcStockCode(inv.getStockCode());
        cr.setBillCode(d.getPlan().getCode());
        cr.setBillCode(d.getPlan().getCode());
        cr.setDept(inv.getDept());
        create(cr);
        return cr;
    }
}