/*
*  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.basicdata.domain.Point;
import com.youchain.businessdata.domain.AsnDetail;
import com.youchain.businessdata.domain.CountDetail;
import com.youchain.businessdata.domain.CountPlan;
import com.youchain.businessdata.service.dto.AsnDetailDto;
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.CountPlanRepository;
import com.youchain.businessdata.service.CountPlanService;
import com.youchain.businessdata.service.dto.CountPlanDto;
import com.youchain.businessdata.service.dto.CountPlanQueryCriteria;
import com.youchain.businessdata.service.mapstruct.CountPlanMapper;
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.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 houjianlan
* @date 2023-10-19
**/
@Service
@RequiredArgsConstructor
public class CountPlanServiceImpl implements CountPlanService {

    private final CountPlanRepository countPlanRepository;
    private final CountPlanMapper countPlanMapper;
    private final EntityManager entityMapper;

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

    @Override
    public List<CountPlanDto> queryAll(CountPlanQueryCriteria criteria){
        return countPlanMapper.toDto(countPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CountPlanDto findById(Long id) {
        CountPlan countPlan = countPlanRepository.findById(id).orElseGet(CountPlan::new);
        ValidationUtil.isNull(countPlan.getId(),"CountPlan","id",id);
        return countPlanMapper.toDto(countPlan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CountPlanDto create(CountPlan resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return countPlanMapper.toDto(countPlanRepository.save(resources));
    }

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

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

    @Override
    public void download(List<CountPlanDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CountPlanDto countPlan : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("代码", countPlan.getCode());
            map.put("名称", countPlan.getName());
            map.put("仓库ID", countPlan.getDept());
            map.put("状态", countPlan.getStatus());
            map.put("类型", countPlan.getType());
            map.put("订单数量", countPlan.getOrderQty());
            map.put("盘点数量", countPlan.getCountQty());
            map.put("备注", countPlan.getRemark());
            map.put("来源名称", countPlan.getSourceName());
            map.put("来源序号", countPlan.getSourceId());
            map.put("创建人", countPlan.getCreateBy());
            map.put("修改人", countPlan.getUpdateBy());
            map.put("创建时间", countPlan.getCreateTime());
            map.put("修改时间", countPlan.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public CountPlan toEntity(CountPlanDto planDto) {
        return countPlanMapper.toEntity(planDto);
    }

    @Override
    public  List<CountDetail> findAllDetail(Long planId){
        String hql=" from CountDetail d where d.plan.id= "+planId;
        Query query=entityMapper.createQuery(hql);
        List<CountDetail> detailList=query.getResultList();
        return  detailList;
    }
}