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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.excel.utils.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.finance.dto.InnerDealDetailExcel;
import org.springblade.finance.dto.ProvDetailExcel;
import org.springblade.finance.entity.InnerDealDetail;
import org.springblade.finance.entity.InnerDealProvDetailView;
import org.springblade.finance.service.IInnerDealDetailService;
import org.springblade.finance.service.InnerDealProvDetailViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 内部结算-结算明细表 控制器
 *
 * @author zbc
 * @since 2022-07-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("finance/innerDealDetail")
@Api(value = "内部结算-结算明细表", tags = "内部结算-结算明细表接口")
public class InnerDealDetailController {

	@Autowired
	private IInnerDealDetailService innerDealDetailService;

	@Autowired
	private InnerDealProvDetailViewService innerDealProvDetailViewService;


	/**
	 * @param query
	 * @return
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页查询其他渠道结算数据")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "company", value = "结算方名称", dataType = "string", required = false),
		@ApiImplicitParam(name = "startTime", value = "开始月份(格式：yyyyMM)", dataType = "string", required = false),
		@ApiImplicitParam(name = "endTime", value = "结束月份(格式：yyyyMM)", dataType = "string", required = false)
	})
	public R<IPage<InnerDealDetail>> page(String company, String startTime, String endTime, Query query){
		if(query.getSize() == null){
			query.setSize(5);
		}
		LambdaQueryWrapper<InnerDealDetail> queryWrapper = Wrappers.<InnerDealDetail>lambdaQuery();
		if(StringUtils.isNotBlank(company)){
			queryWrapper.like(InnerDealDetail::getCompany,company);
		}
		if(StringUtils.isNotEmpty(startTime)) {
			int startDate = Integer.valueOf(startTime);
			queryWrapper.ge(InnerDealDetail::getThemonth, startDate);
		}
		if(StringUtils.isNotEmpty(endTime)) {
			int endDate = Integer.valueOf(endTime);
			queryWrapper.le(InnerDealDetail::getThemonth, endDate);
		}
		IPage<InnerDealDetail> pageResult = innerDealDetailService.page(Condition.getPage(query), queryWrapper.orderByDesc(InnerDealDetail::getThemonth));
		return R.data(pageResult);
	}


	@GetMapping("/export")
	@ApiOperation(value = "内部结算结算明细导出")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "company", value = "结算方名称", dataType = "string", required = false),
			@ApiImplicitParam(name = "startTime", value = "开始月份(格式：yyyyMM)", dataType = "string", required = false),
			@ApiImplicitParam(name = "endTime", value = "结束月份(格式：yyyyMM)", dataType = "string", required = false)
	})
	public void export(String company, String startTime, String endTime, HttpServletResponse response) {
		LambdaQueryWrapper<InnerDealDetail> queryWrapper = Wrappers.<InnerDealDetail>lambdaQuery();
		if(StringUtils.isNotBlank(company)){
			queryWrapper.like(InnerDealDetail::getCompany,company);
		}
		if(StringUtils.isNotEmpty(startTime)) {
			int startDate = Integer.valueOf(startTime);
			queryWrapper.ge(InnerDealDetail::getThemonth, startDate);
		}
		if(StringUtils.isNotEmpty(endTime)) {
			int endDate = Integer.valueOf(endTime);
			queryWrapper.le(InnerDealDetail::getThemonth, endDate);
		}

		List<InnerDealDetail> resultList = innerDealDetailService.list(queryWrapper.orderByDesc(InnerDealDetail::getThemonth));
		List<InnerDealDetailExcel> excelList = new ArrayList<InnerDealDetailExcel>();
		if(resultList != null && resultList.size() > 0) {
			for (InnerDealDetail item : resultList) {
				excelList.add(BeanUtil.copyProperties(item, InnerDealDetailExcel.class));
			}
		}
		ExcelUtil.export(response, "内部结算明细表", "内部结算明细", excelList, InnerDealDetailExcel.class);
	}


	@GetMapping("/provDetailPage")
	@ApiOperation(value = "分页查询-内部结算-省分明细表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "company", value = "结算方名称", dataType = "string", required = false),
			@ApiImplicitParam(name = "startTime", value = "开始月份(格式：yyyyMM)", dataType = "string", required = false),
			@ApiImplicitParam(name = "endTime", value = "结束月份(格式：yyyyMM)", dataType = "string", required = false)
	})
	public R<IPage<InnerDealProvDetailView>> provDetailPage(String company, String startTime, String endTime, Query query){
		if(query.getSize() == null){
			query.setSize(5);
		}
		LambdaQueryWrapper<InnerDealProvDetailView> queryWrapper = Wrappers.<InnerDealProvDetailView>lambdaQuery();
		if(StringUtils.isNotBlank(company)){
			queryWrapper.like(InnerDealProvDetailView::getCompany,company);
		}
		if(StringUtils.isNotEmpty(startTime)) {
			int startDate = Integer.valueOf(startTime);
			queryWrapper.ge(InnerDealProvDetailView::getThemonth, startDate);
		}
		if(StringUtils.isNotEmpty(endTime)) {
			int endDate = Integer.valueOf(endTime);
			queryWrapper.le(InnerDealProvDetailView::getThemonth, endDate);
		}
		IPage<InnerDealProvDetailView> pageResult = innerDealProvDetailViewService.page(Condition.getPage(query), queryWrapper.orderByDesc(InnerDealProvDetailView::getThemonth));
		return R.data(pageResult);
	}


	/**
	 * 省分明细表导出
	 * @param company
	 * @param startTime
	 * @param endTime
	 * @param response
	 */
	@GetMapping("/provDetailExport")
	@ApiOperation(value = "内部结算-省分明细表导出")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "company", value = "结算方名称", dataType = "string", required = false),
			@ApiImplicitParam(name = "startTime", value = "开始月份(格式：yyyyMM)", dataType = "string", required = false),
			@ApiImplicitParam(name = "endTime", value = "结束月份(格式：yyyyMM)", dataType = "string", required = false)
	})
	public void provDetailExport(String company, String startTime, String endTime, HttpServletResponse response) {
		LambdaQueryWrapper<InnerDealProvDetailView> queryWrapper = Wrappers.<InnerDealProvDetailView>lambdaQuery();
		if(StringUtils.isNotBlank(company)){
			queryWrapper.like(InnerDealProvDetailView::getCompany,company);
		}
		if(StringUtils.isNotEmpty(startTime)) {
			int startDate = Integer.valueOf(startTime);
			queryWrapper.ge(InnerDealProvDetailView::getThemonth, startDate);
		}
		if(StringUtils.isNotEmpty(endTime)) {
			int endDate = Integer.valueOf(endTime);
			queryWrapper.le(InnerDealProvDetailView::getThemonth, endDate);
		}
		List<InnerDealProvDetailView> resultList = innerDealProvDetailViewService.list(queryWrapper.orderByDesc(InnerDealProvDetailView::getThemonth));
		List<ProvDetailExcel> excelList = new ArrayList<ProvDetailExcel>();
		if(resultList != null && resultList.size() > 0) {
			for (InnerDealProvDetailView item : resultList) {
				excelList.add(BeanUtil.copyProperties(item, ProvDetailExcel.class));
			}
		}
		ExcelUtil.export(response, "省份明细表", "省份明细", excelList, ProvDetailExcel.class);
	}
}
