/*
*  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 me.zhengjie.modules.dm.settlement.service.impl;

import me.zhengjie.modules.dm.settlement.domain.DmSettlement;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.settlement.repository.DmSettlementRepository;
import me.zhengjie.modules.dm.settlement.service.DmSettlementService;
import me.zhengjie.modules.dm.settlement.service.dto.DmSettlementDto;
import me.zhengjie.modules.dm.settlement.service.dto.DmSettlementQueryCriteria;
import me.zhengjie.modules.dm.settlement.service.mapstruct.DmSettlementMapper;
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 me.zhengjie.utils.PageUtil;
import me.zhengjie.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://el-admin.vip
* @description 服务实现
* @author xwx
* @date 2021-04-22
**/
@Service
@RequiredArgsConstructor
public class DmSettlementServiceImpl implements DmSettlementService {

    private final DmSettlementRepository dmSettlementRepository;
    private final DmSettlementMapper dmSettlementMapper;

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

    @Override
    public List<DmSettlementDto> queryAll(DmSettlementQueryCriteria criteria){
        return dmSettlementMapper.toDto(dmSettlementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmSettlementDto findById(Long id) {
        DmSettlement dmSettlement = dmSettlementRepository.findById(id).orElseGet(DmSettlement::new);
        ValidationUtil.isNull(dmSettlement.getId(),"DmSettlement","id",id);
        return dmSettlementMapper.toDto(dmSettlement);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmSettlementDto create(DmSettlement resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmSettlementMapper.toDto(dmSettlementRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmSettlementDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmSettlementDto dmSettlement : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单编号", dmSettlement.getBh());
            map.put("菜品名称", dmSettlement.getFoodName());
            map.put("用户名称", dmSettlement.getDmUser().getName());
            map.put("菜品单价", dmSettlement.getFoodPrice());
            map.put("点餐数量", dmSettlement.getFoodNum());
            map.put("小计(点餐价格合计)", dmSettlement.getTotalPrice());
            map.put("点餐时间", dmSettlement.getOrderTime());
            map.put("创建者", dmSettlement.getCreateBy());
            map.put("更新者", dmSettlement.getUpdateBy());
            map.put("创建日期", dmSettlement.getCreateTime());
            map.put("更新时间", dmSettlement.getUpdateTime());
            map.put("结算卡号", dmSettlement.getCardCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}