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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.modules.dmRechargeRecord.domain.DmRechargeRecord;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dmRechargeRecord.repository.DmRechargeRecordRepository;
import me.zhengjie.modules.dmRechargeRecord.service.DmRechargeRecordService;
import me.zhengjie.modules.dmRechargeRecord.service.dto.DmRechargeRecordDto;
import me.zhengjie.modules.dmRechargeRecord.service.dto.DmRechargeRecordQueryCriteria;
import me.zhengjie.modules.dmRechargeRecord.service.mapstruct.DmRechargeRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;

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 zhirong
* @date 2021-11-23
**/
@Service
@RequiredArgsConstructor
public class DmRechargeRecordServiceImpl implements DmRechargeRecordService {

    private final DmRechargeRecordRepository dmRechargeRecordRepository;
    private final DmRechargeRecordMapper dmRechargeRecordMapper;
    private final UserService userService;

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

    @Override
    public List<DmRechargeRecordDto> queryAll(DmRechargeRecordQueryCriteria criteria){
        return dmRechargeRecordMapper.toDto(dmRechargeRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmRechargeRecordDto findById(Long id) {
        DmRechargeRecord dmRechargeRecord = dmRechargeRecordRepository.findById(id).orElseGet(DmRechargeRecord::new);
        ValidationUtil.isNull(dmRechargeRecord.getId(),"DmRechargeRecord","id",id);
        return dmRechargeRecordMapper.toDto(dmRechargeRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmRechargeRecordDto create(DmRechargeRecord resources) {
        return dmRechargeRecordMapper.toDto(dmRechargeRecordRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmRechargeRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmRechargeRecordDto dmRechargeRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("用户", dmRechargeRecord.getUserNumber());
            map.put("持卡人姓名", dmRechargeRecord.getUserName());
            map.put("持卡人号码", dmRechargeRecord.getUserPhone());
            map.put("卡号", dmRechargeRecord.getCardNumber());
            map.put("充值金额", dmRechargeRecord.getRechargeAmount());
            map.put("充值后金额", dmRechargeRecord.getAmountAfterRecharge());
            map.put("充值时间", dmRechargeRecord.getRechargeTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public DmRechargeRecord findBySeq(String seq) {
        return dmRechargeRecordRepository.findBySeq(seq);
    }

    @Override
    public void vehicleRechargeRecord(JSONObject json){
        String seq = json.getString("seq");
        DmRechargeRecord dmRechargeRecord = findBySeq(seq);
        if(ObjectUtils.isEmpty(dmRechargeRecord)) {
            //组装返回数据
            DmRechargeRecordDto dmRechargeRecordDto = create(JSON.toJavaObject(json, DmRechargeRecord.class));
            String sendContent = "您已充值成功："+dmRechargeRecordDto.getRechargeAmount()+"元 充值后金额："+dmRechargeRecordDto.getAmountAfterRecharge();
            MessageSendUtils.sendChatMessage("dm_recharge_record|seq|"+seq,"System",sendContent,userService.getUsernameByCardId(dmRechargeRecordDto.getCardNumber()));
        } else {
            System.out.println("dmRechargeRecord is not null. seq:"+seq);
        }
    }
}