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

import cn.ysk.cashier.pojo.shop.TbMerchantAccount;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.shop.TbMerchantAccountRepository;
import cn.ysk.cashier.service.shop.TbMerchantAccountService;
import cn.ysk.cashier.dto.shop.TbMerchantAccountDto;
import cn.ysk.cashier.dto.shop.TbMerchantAccountQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbMerchantAccountMapper;
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-02-08
**/
@Service
@RequiredArgsConstructor
public class TbMerchantAccountServiceImpl implements TbMerchantAccountService {

    private final TbMerchantAccountRepository tbMerchantAccountRepository;
    private final TbMerchantAccountMapper tbMerchantAccountMapper;

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

    @Override
    public List<TbMerchantAccountDto> queryAll(TbMerchantAccountQueryCriteria criteria){
        return tbMerchantAccountMapper.toDto(tbMerchantAccountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbMerchantAccountDto findById(Integer id) {
        TbMerchantAccount tbMerchantAccount = tbMerchantAccountRepository.findById(id).orElseGet(TbMerchantAccount::new);
        ValidationUtil.isNull(tbMerchantAccount.getId(),"TbMerchantAccount","id",id);
        return tbMerchantAccountMapper.toDto(tbMerchantAccount);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbMerchantAccountDto create(TbMerchantAccount resources) {
        return tbMerchantAccountMapper.toDto(tbMerchantAccountRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbMerchantAccountDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbMerchantAccountDto tbMerchantAccount : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("登陆帐号", tbMerchantAccount.getAccount());
            map.put("登陆密码", tbMerchantAccount.getPassword());
            map.put("商家Id", tbMerchantAccount.getMerchantId());
            map.put("门店Id", tbMerchantAccount.getShopId());
            map.put(" shopSnap",  tbMerchantAccount.getShopSnap());
            map.put("是否管理员", tbMerchantAccount.getIsAdmin());
            map.put("是否商户：1商户帐号0-店铺帐号", tbMerchantAccount.getIsMercantile());
            map.put("姓名", tbMerchantAccount.getName());
            map.put("性别:0女 1 男", tbMerchantAccount.getSex());
            map.put("邮箱", tbMerchantAccount.getEmail());
            map.put("头像", tbMerchantAccount.getHeadImg());
            map.put("联系电话", tbMerchantAccount.getTelephone());
            map.put("状态", tbMerchantAccount.getStatus());
            map.put("排序", tbMerchantAccount.getSort());
            map.put(" roleId",  tbMerchantAccount.getRoleId());
            map.put("最后登陆时间", tbMerchantAccount.getLastLoginAt());
            map.put("公众号openId", tbMerchantAccount.getMpOpenId());
            map.put("是否接收消息通知", tbMerchantAccount.getMsgAble());
            map.put(" createdAt",  tbMerchantAccount.getCreatedAt());
            map.put(" updatedAt",  tbMerchantAccount.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}