/*
*  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.Point;
import com.youchain.basicdata.domain.StockType;
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.basicdata.repository.StockTypeRepository;
import com.youchain.basicdata.service.StockTypeService;
import com.youchain.basicdata.service.dto.StockTypeDto;
import com.youchain.basicdata.service.dto.StockTypeQueryCriteria;
import com.youchain.basicdata.service.mapstruct.StockTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author liuxue
* @date 2023-07-28
**/
@Service
@RequiredArgsConstructor
public class StockTypeServiceImpl implements StockTypeService {

    private final StockTypeRepository stockTypeRepository;
    private final StockTypeMapper stockTypeMapper;
    private final EntityManager entityMapper;

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

    @Override
    public List<StockTypeDto> queryAll(StockTypeQueryCriteria criteria){
        return stockTypeMapper.toDto(stockTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public Map<String ,StockType> queryAll(){
       List<StockType> stockTypeList= stockTypeRepository.findAll();
        Map<String, StockType> map = new HashMap<>();
        for (StockType s:stockTypeList ) {
            map.put(s.getCode(),s);
        }
        return map;
    }

    @Override
    @Transactional
    public StockTypeDto findById(Long id) {
        StockType stockType = stockTypeRepository.findById(id).orElseGet(StockType::new);
        ValidationUtil.isNull(stockType.getId(),"StockType","id",id);
        return stockTypeMapper.toDto(stockType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StockTypeDto create(StockType resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return stockTypeMapper.toDto(stockTypeRepository.save(resources));
    }

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

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

    @Override
    public void download(List<StockTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StockTypeDto stockType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", stockType.getName());
            map.put("代码", stockType.getCode());
            map.put("长", stockType.getLength());
            map.put("宽", stockType.getWidth());
            map.put("高", stockType.getHeight());
            map.put("重量", stockType.getWeight());
            map.put("体积", stockType.getSize());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public StockType findByCode(String code){
        String hql="select t from StockType t where t.code='"+code+"'";
        Query query=entityMapper.createQuery(hql);
        List<StockType> ts=query.getResultList();
        if(ts.size()>0){
            return ts.get(0);
        }
        return  null;
    }
}