/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.security.trans.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.security.trans.basic.dto.SelfHelpBankDTO;
import org.security.ht.entity.basic.Department;
import org.security.ht.entity.basic.SelfHelpBank;
import org.security.trans.basic.service.IDepartmentService;
import org.security.ht.vo.basic.SelfHelpBankVO;
import org.security.trans.basic.mapper.SelfHelpBankMapper;
import org.security.trans.basic.service.ISelfHelpBankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础信息-自助银行 服务实现类
 *
 * @author BladeX
 * @since 2022-03-16
 */
@Service
public class SelfHelpBankServiceImpl extends ServiceImpl<SelfHelpBankMapper, SelfHelpBank> implements ISelfHelpBankService {
	@Autowired
	private IDepartmentService departmentService;

	@Override
	public IPage<SelfHelpBankVO> selectSelfHelpBankPage(IPage<SelfHelpBankDTO> page, SelfHelpBankDTO selfHelpBankDTO) {
		Page<SelfHelpBank> selfHelpBankPage = new Page<>(page.getCurrent(), page.getSize());
		SelfHelpBank selfHelpBank = Func.copy(selfHelpBankDTO, SelfHelpBank.class);
		QueryWrapper<SelfHelpBank> queryWrapper = Condition.getQueryWrapper(selfHelpBank);
		Page<SelfHelpBank> resultPage = this.page(selfHelpBankPage, queryWrapper);
		List<SelfHelpBank> records = resultPage.getRecords();
		List<SelfHelpBankVO> selfHelpBankVOList = new ArrayList<>();
		records.forEach(item -> {
			SelfHelpBankVO selfHelpBankVO = Func.copy(item, SelfHelpBankVO.class);
			selfHelpBankVOList.add(selfHelpBankVO);
		});
		return new Page<SelfHelpBankVO>()
			.setCurrent(resultPage.getCurrent())
			.setSize(resultPage.getSize())
			.setTotal(resultPage.getTotal())
			.setRecords(selfHelpBankVOList);
	}

	@Override
	public SelfHelpBankVO getSelfHelpBankById(Long id) {
		SelfHelpBank selfHelpBank = this.getById(id);
		Department department = departmentService.getById(selfHelpBank.getDeptId());
		SelfHelpBankVO selfHelpBankVO = Func.copy(selfHelpBank, SelfHelpBankVO.class);
		assert selfHelpBankVO != null;
		selfHelpBankVO.setDeptName(department.getDeptName());
		return selfHelpBankVO;
	}

}
