/*
*  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;

import cn.ysk.cashier.pojo.TbUserInfo;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.TbUserInfoRepository;
import cn.ysk.cashier.service.TbUserInfoService;
import cn.ysk.cashier.dto.TbUserInfoDto;
import cn.ysk.cashier.dto.TbUserInfoQueryCriteria;
import cn.ysk.cashier.mapper.TbUserInfoMapper;
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 2023-11-13
**/
@Service
@RequiredArgsConstructor
public class TbUserInfoServiceImpl implements TbUserInfoService {

    private final TbUserInfoRepository tbUserInfoRepository;
    private final TbUserInfoMapper tbUserInfoMapper;

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

    @Override
    public List<TbUserInfoDto> queryAll(TbUserInfoQueryCriteria criteria){
        return tbUserInfoMapper.toDto(tbUserInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbUserInfoDto findById(Integer id) {
        TbUserInfo tbUserInfo = tbUserInfoRepository.findById(id).orElseGet(TbUserInfo::new);
        ValidationUtil.isNull(tbUserInfo.getId(),"TbUserInfo","id",id);
        return tbUserInfoMapper.toDto(tbUserInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbUserInfoDto create(TbUserInfo resources) {
        return tbUserInfoMapper.toDto(tbUserInfoRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbUserInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbUserInfoDto tbUserInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("钱包余额", tbUserInfo.getAmount());
            map.put("累计充值", tbUserInfo.getChargeAmount());
            map.put("授信额度", tbUserInfo.getLineOfCredit());
            map.put(" consumeAmount",  tbUserInfo.getConsumeAmount());
            map.put("消费次数累计", tbUserInfo.getConsumeNumber());
            map.put("总积分", tbUserInfo.getTotalScore());
            map.put("锁定积分", tbUserInfo.getLockScore());
            map.put("会员卡号", tbUserInfo.getCardNo());
            map.put("会员密码", tbUserInfo.getCardPassword());
            map.put("等级", tbUserInfo.getLevelId());
            map.put("用户头像", tbUserInfo.getHeadImg());
            map.put("用户昵称", tbUserInfo.getNickName());
            map.put("电话号码", tbUserInfo.getTelephone());
            map.put("小程序Id", tbUserInfo.getWxMaAppId());
            map.put("会员生日", tbUserInfo.getBirthDay());
            map.put("0-女 1男", tbUserInfo.getSex());
            map.put("小程序的openId", tbUserInfo.getMiniAppOpenId());
            map.put("公众号openId", tbUserInfo.getOpenId());
            map.put("联合Id", tbUserInfo.getUnionId());
            map.put("用户编号", tbUserInfo.getCode());
            map.put("用户类型-保留字段", tbUserInfo.getType());
            map.put("'DOCTOR'-->医生，'USER'->用户", tbUserInfo.getIdentify());
            map.put("1正常", tbUserInfo.getStatus());
            map.put("引荐人", tbUserInfo.getParentId());
            map.put(" parentLevel",  tbUserInfo.getParentLevel());
            map.put("上家类开型，店铺-SHOP/ 个人- PERSON", tbUserInfo.getParentType());
            map.put("关注进入的活动Id", tbUserInfo.getProjectId());
            map.put("商户Id", tbUserInfo.getMerchantId());
            map.put("0未认证，1认证", tbUserInfo.getIsResource());
            map.put("是否在线", tbUserInfo.getIsOnline());
            map.put(" isVip",  tbUserInfo.getIsVip());
            map.put(" vipEffectAt",  tbUserInfo.getVipEffectAt());
            map.put(" tips",  tbUserInfo.getTips());
            map.put("用户来源：公众号 WECHAT  小程序 WECHAT-APP 手机注册 TELEPHONE", tbUserInfo.getSourcePath());
            map.put("是否推广员 1 是 0不是", tbUserInfo.getIsSalesPerson());
            map.put("是否关注公众号", tbUserInfo.getIsAttentionMp());
            map.put("城市", tbUserInfo.getCity());
            map.put(" searchWord",  tbUserInfo.getSearchWord());
            map.put("最近登陆时间", tbUserInfo.getLastLogInAt());
            map.put(" lastLeaveAt",  tbUserInfo.getLastLeaveAt());
            map.put(" createdAt",  tbUserInfo.getCreatedAt());
            map.put(" updatedAt",  tbUserInfo.getUpdatedAt());
            map.put("绑定时间", tbUserInfo.getBindParentAt());
            map.put("上上家", tbUserInfo.getGrandParentId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}