/*
*  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.Area;
import com.youchain.basicdata.domain.Item;
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.AreaRepository;
import com.youchain.basicdata.service.AreaService;
import com.youchain.basicdata.service.dto.AreaDto;
import com.youchain.basicdata.service.dto.AreaQueryCriteria;
import com.youchain.basicdata.service.mapstruct.AreaMapper;
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.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author HJL
* @date 2023-08-14
**/
@Service
@RequiredArgsConstructor
public class  AreaServiceImpl implements AreaService {

    private final AreaRepository areaRepository;
    private final AreaMapper areaMapper;
    private final EntityManager entityMapper;

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

    @Override
    public List<AreaDto> queryAll(AreaQueryCriteria criteria){
        return areaMapper.toDto(areaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AreaDto findById(Long id) {
        Area area = areaRepository.findById(id).orElseGet(Area::new);
        ValidationUtil.isNull(area.getId(),"Area","id",id);
        return areaMapper.toDto(area);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AreaDto create(Area resources) {
        return areaMapper.toDto(areaRepository.save(resources));
    }

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

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

    @Override
    public void download(List<AreaDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AreaDto area : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("库区编号", area.getCode());
            map.put("库区名称", area.getName());
            map.put("描述", area.getDescription());
            map.put("状态：1启用、0禁用", area.getEnabled());
            map.put("热度值", area.getHeat());
            map.put("坐标X", area.getPosX());
            map.put("坐标Y", area.getPosY());
            map.put("仓库ID", area.getDeptId());
            map.put("序列号", area.getSequenceNo());
            map.put("org编号", area.getOrgCode());
            map.put("创建人", area.getCreateBy());
            map.put("更新人", area.getUpdateBy());
            map.put("创建时间", area.getCreateTime());
            map.put("更新时间", area.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Area toEntity(AreaDto areaDto) {
        return areaMapper.toEntity(areaDto);
    }

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