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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.business.entity.ContractLog;
import org.springblade.business.vo.ContractLogVO;
import org.springblade.business.mapper.ContractLogMapper;
import org.springblade.business.service.IContractLogService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 日志通用表 服务实现类
 *
 * @author BladeX
 * @since 2022-06-15
 */
@Service
public class ContractLogServiceImpl extends BaseServiceImpl<ContractLogMapper, ContractLog> implements IContractLogService {

	@Override
	public IPage<ContractLogVO> constructionLogPage(ContractLogVO logVO) {
		Integer current = (logVO.getCurrent() - 1) * logVO.getSize();

		Query query = new Query();
		query.setCurrent(logVO.getCurrent());
		query.setSize(logVO.getSize());

		//总数
		Integer count = this.baseMapper.selectPageCount(logVO);
		//分页信息
		List<ContractLog> result = this.baseMapper.constructionLogPage(current, logVO.getSize(), logVO);
		//没有数据
		if(result == null || result.size() == 0){
			return Condition.getPage(query);
		}
		//有数据，转换成VO
		List<ContractLogVO> voResult = JSONArray.parseArray(JSONObject.toJSONString(result), ContractLogVO.class);

		IPage<ContractLogVO> iPage = Condition.getPage(query);
		iPage.setTotal(count);
		return iPage.setRecords(voResult);
	}

	@Override
	public IPage<ContractLogVO> selectContractLogPage(IPage<ContractLogVO> page, ContractLogVO contractLog) {
		return page.setRecords(this.baseMapper.selectContractLogPage(page, contractLog));
	}

}
