/*
*  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 cn.ysk.cashier.service.impl.order;

import cn.ysk.cashier.pojo.order.TbOrderDetail;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.order.TbOrderDetailRepository;
import cn.ysk.cashier.service.order.TbOrderDetailService;
import cn.ysk.cashier.dto.order.TbOrderDetailDto;
import cn.ysk.cashier.dto.order.TbOrderDetailQueryCriteria;
import cn.ysk.cashier.mapper.order.TbOrderDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
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 lyf
* @date 2024-03-02
**/
@Service
@RequiredArgsConstructor
public class TbOrderDetailServiceImpl implements TbOrderDetailService {

    private final TbOrderDetailRepository tbOrderDetailRepository;
    private final TbOrderDetailMapper tbOrderDetailMapper;

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

    @Override
    public List<TbOrderDetailDto> queryAll(TbOrderDetailQueryCriteria criteria){
        return tbOrderDetailMapper.toDto(tbOrderDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbOrderDetailDto findById(Integer id) {
        TbOrderDetail tbOrderDetail = tbOrderDetailRepository.findById(id).orElseGet(TbOrderDetail::new);
        ValidationUtil.isNull(tbOrderDetail.getId(),"TbOrderDetail","id",id);
        return tbOrderDetailMapper.toDto(tbOrderDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbOrderDetailDto create(TbOrderDetail resources) {
        return tbOrderDetailMapper.toDto(tbOrderDetailRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbOrderDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbOrderDetailDto tbOrderDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" orderId",  tbOrderDetail.getOrderId());
            map.put(" shopId",  tbOrderDetail.getShopId());
            map.put(" productId",  tbOrderDetail.getProductId());
            map.put(" productSkuId",  tbOrderDetail.getProductSkuId());
            map.put(" num",  tbOrderDetail.getNum());
            map.put(" productName",  tbOrderDetail.getProductName());
            map.put(" productSkuName",  tbOrderDetail.getProductSkuName());
            map.put(" productImg",  tbOrderDetail.getProductImg());
            map.put(" createTime",  tbOrderDetail.getCreateTime());
            map.put(" updateTime",  tbOrderDetail.getUpdateTime());
            map.put(" price",  tbOrderDetail.getPrice());
            map.put(" priceAmount",  tbOrderDetail.getPriceAmount());
            map.put(" status",  tbOrderDetail.getStatus());
            map.put("打包费", tbOrderDetail.getPackAmount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}