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

import me.zhengjie.modules.dm.daypc.repository.DmDayPcRepository;
import me.zhengjie.modules.dm.order.domain.DmOrderItem;
import me.zhengjie.modules.dm.order.domain.DmOrderRecord;
import me.zhengjie.modules.dm.order.service.dto.DmExpenseCalendar;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.order.repository.DmOrderRecordRepository;
import me.zhengjie.modules.dm.order.service.DmOrderRecordService;
import me.zhengjie.modules.dm.order.service.dto.DmOrderRecordDto;
import me.zhengjie.modules.dm.order.service.dto.DmOrderRecordQueryCriteria;
import me.zhengjie.modules.dm.order.service.mapstruct.DmOrderRecordMapper;
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.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author sunmz
* @date 2021-09-15
**/
@Service
@RequiredArgsConstructor
public class DmOrderRecordServiceImpl implements DmOrderRecordService {

    private final DmOrderRecordRepository dmOrderRecordRepository;
    private final DmOrderRecordMapper dmOrderRecordMapper;
    private final DmDayPcRepository dmDayPcRepository;

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

    @Override
    public Map<String, Object> queryDetails(DmOrderRecordQueryCriteria criteria, Pageable pageable) {
//        criteria.setId(criteria.getId());
        Page<DmOrderRecord> page = dmOrderRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dmOrderRecordMapper::toDto));
    }

    @Override
    public List<DmOrderRecordDto> queryAll(DmOrderRecordQueryCriteria criteria){
        return dmOrderRecordMapper.toDto(dmOrderRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmOrderRecordDto findById(Long id) {
        DmOrderRecord dmOrderRecord = dmOrderRecordRepository.findById(id).orElseGet(DmOrderRecord::new);
        ValidationUtil.isNull(dmOrderRecord.getId(),"DmOrderRecord","id",id);
        return dmOrderRecordMapper.toDto(dmOrderRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmOrderRecordDto create(DmOrderRecord resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmOrderRecordMapper.toDto(dmOrderRecordRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmOrderRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmOrderRecordDto dmOrderRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单编号", dmOrderRecord.getBh());//
            map.put("实付金额", dmOrderRecord.getMoney());
            map.put("用户姓名", dmOrderRecord.getUserName());
            map.put("人员种类", dmOrderRecord.getUserType());
            map.put("用户手机号", dmOrderRecord.getPhone());
            map.put("下单时间", dmOrderRecord.getCreateDate());
            map.put("合计金额", dmOrderRecord.getTotalMoney());
            map.put("折扣", dmOrderRecord.getZkMoney());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> panelGroup() {
        Integer orderQuantity=dmOrderRecordRepository.countOrderQuantity();
        Integer income=dmOrderRecordRepository.countIncome();
        Integer consumerNumber=dmOrderRecordRepository.countConsumerNumber();
        Integer dishesNumber=dmDayPcRepository.countDishesNumber();
        Map<String,Object> map =new HashMap<>();
        map.put("orderQuantity",orderQuantity);
        map.put("income",income);
        map.put("consumerNumber",consumerNumber);
        map.put("dishesNumber",dishesNumber);
        return map;
    }

    @Override
    public List<Map<String,Object>> selectOrderRecord() {
        return dmOrderRecordRepository.selectOrderRecord();
    }

    @Override
    @Transactional
    public void createOrderRecord(DmExpenseCalendar resources) {
        dmOrderRecordRepository.insertOrderRecord(resources.getBh(),resources.getMoney(),resources.getMob(),resources.getUserCode(),resources.getUserName(),resources.getDeptId(),resources.getUserType(),resources.getPhone(),resources.getCreateDate(),resources.getTotalMoney(),resources.getZkMoney(),resources.getSeq(),resources.getCard(),resources.getCardNo(),resources.getPart(),resources.getTerm(),resources.getShop(),resources.getTermName(),resources.getShopName(),resources.getAmt(),resources.getBalance(),resources.getAccount(),resources.getType());
    }

    @Override
    @Transactional
    public void createOrderItem(DmExpenseCalendar resources) {
        dmOrderRecordRepository.insertDmOrderItem(resources.getBh(),resources.getPid(),resources.getCode(),resources.getName(),resources.getPrice(),resources.getNum(),resources.getOrderItemMoney(),resources.getOrderItemSeq(),resources.getOrderItemAmt(),resources.getPjType());
    }

    @Override
    public Integer countOrderAmt() {
        return dmOrderRecordRepository.countOrderAmt();
    }

}