/*
*  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 cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.youchain.basicdata.domain.Studen;
import com.youchain.basicdata.repository.PointRepository;
import com.youchain.businessdata.domain.Order;
import com.youchain.businessdata.inputJson.OrderInfo;
import com.youchain.businessdata.service.OrderService;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.StudenRepository;
import com.youchain.basicdata.service.StudenService;
import com.youchain.basicdata.service.dto.StudenDto;
import com.youchain.basicdata.service.dto.StudenQueryCriteria;
import com.youchain.basicdata.service.mapstruct.StudenMapper;
import org.springframework.scheduling.annotation.Scheduled;
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.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author LY
* @date 2024-04-16
**/
@Service
@RequiredArgsConstructor
public class StudenServiceImpl implements StudenService {

    private final StudenRepository studenRepository;
    private final StudenMapper studenMapper;
    private final OrderService orderService;


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

    @Override
    public List<StudenDto> queryAll(StudenQueryCriteria criteria){
        return studenMapper.toDto(studenRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public StudenDto findById(Integer id) {
        Studen studen = studenRepository.findById(id).orElseGet(Studen::new);
        ValidationUtil.isNull(studen.getId(),"Studen","id",id);
        return studenMapper.toDto(studen);
    }

    @Override
    public boolean findBycode(String code) {
        return studenRepository.getKyPointList(code) >= 1 ? true : false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StudenDto create(Studen resources) {
        return studenMapper.toDto(studenRepository.save(resources));
    }

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

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

    @Override
    public void download(List<StudenDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StudenDto studen : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("代码", studen.getCode());
            map.put("名称", studen.getName());
            map.put("是否启用", studen.getEnabled());
            map.put("创建人", studen.getCreator());
            map.put("创建时间", studen.getCreationTime());
            map.put("修改人", studen.getModifier());
            map.put("修改时间", studen.getModificationTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
