/**
 *
 */
package org.springblade.finance.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springblade.common.excel.utils.ExcelUtil;
import org.springblade.core.boot.ctrl.BladeController;
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.BenifitAccountExcel;
import org.springblade.finance.entity.BenifitAccount;
import org.springblade.finance.entity.DealUserAccounts;
import org.springblade.finance.service.BenifitAccountService;
import org.springblade.finance.service.DealUserAccountsService;
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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("finance/benifitAccount")
@Api(value = "结算范围内有效用户利润", tags = "结算范围内有效用户利润")
public class BenifitAccountController extends BladeController {

	@Autowired
	private BenifitAccountService benifitAccountService;


	@Autowired
	private DealUserAccountsService dealUserAccountsService;

	/**
	 * @param dealUserIds
	 * @param query
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "数据列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "dealUserIds", value = "结算用户ID，逗号隔开。全部渠道传空或者不传递", required = false),
		@ApiImplicitParam(name = "spid", value = "产品ID", dataType = "string", required = false),
		@ApiImplicitParam(name = "startDate", value = "开始日期(格式：yyyy-MM)", dataType = "string", required = false),
		@ApiImplicitParam(name = "endDate", value = "结束日期(格式：yyyy-MM)", dataType = "string", required = false)
	})
	public R<IPage<BenifitAccount>> list(String[] dealUserIds, String spid, String startDate, String endDate, Query query){
		if(query.getSize() == null){
			query.setSize(5);
		}
		LambdaQueryWrapper<BenifitAccount> queryWrapper = Wrappers.<BenifitAccount>lambdaQuery();
		if(dealUserIds != null && dealUserIds.length > 0) {
			List<String> strings = Arrays.asList(dealUserIds);
			List<String> collect = null;
			if(strings != null && strings.size() > 0){
				QueryWrapper<DealUserAccounts> dealQueryWrapper = new QueryWrapper<DealUserAccounts>();
				dealQueryWrapper.select("DEAL_ACCOUNT_ID").isNotNull("DEAL_ACCOUNT_ID");
				dealQueryWrapper.in("DEAL_USERID",strings);
				List<DealUserAccounts> dealList = dealUserAccountsService.list(dealQueryWrapper);
				collect = dealList.stream().map(DealUserAccounts::getDealAccountId).collect(Collectors.toList());
			}
			queryWrapper.in(BenifitAccount::getDealAccountId, collect);
		}
		if(StringUtils.isNotEmpty(spid)) {
			queryWrapper.eq(BenifitAccount::getSpid, spid);
		}
		if(StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
			Integer intStartDate = Integer.valueOf(startDate.replaceAll("\\-", ""));
			Integer intEndDate = Integer.valueOf(endDate.replaceAll("\\-", ""));
			queryWrapper.between(BenifitAccount::getTheMonth, intStartDate, intEndDate);
		}
		return R.data(benifitAccountService.page(Condition.getPage(query), queryWrapper.orderByDesc(BenifitAccount::getTheMonth)));
	}
	/**
	 *
	 * @param dealUserid
	 * @param spid
	 * @param startDate
	 * @param endDate
	 */
	@GetMapping("/export")
	@ApiOperation(value = "导出数据")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "dealUserid", value = "结算用户ID，逗号隔开。全部渠道传空或者不传递", required = false),
		@ApiImplicitParam(name = "spid", value = "产品ID", dataType = "string", required = false),
		@ApiImplicitParam(name = "startDate", value = "开始日期(格式：yyyy-MM)", dataType = "string", required = false),
		@ApiImplicitParam(name = "endDate", value = "结束日期(格式：yyyy-MM)", dataType = "string", required = false)
	})
	public void export(String[] dealUserid, String spid, String startDate, String endDate, HttpServletResponse response) {
		LambdaQueryWrapper<BenifitAccount> queryWrapper = Wrappers.<BenifitAccount>lambdaQuery();
		if(dealUserid != null && dealUserid.length > 0) {
			List<String> strings = Arrays.asList(dealUserid);
			List<String> collect = null;
			if(strings != null && strings.size() > 0){
				QueryWrapper<DealUserAccounts> dealQueryWrapper = new QueryWrapper<DealUserAccounts>();
				dealQueryWrapper.select("DEAL_ACCOUNT_ID").isNotNull("DEAL_ACCOUNT_ID");
				dealQueryWrapper.in("DEAL_USERID",strings);
				List<DealUserAccounts> dealList = dealUserAccountsService.list(dealQueryWrapper);
				collect = dealList.stream().map(DealUserAccounts::getDealAccountId).collect(Collectors.toList());
			}
			//queryWrapper.in(BenifitAccount::getChannelId, Arrays.asList(collect));
			queryWrapper.in(BenifitAccount::getDealAccountId, collect);
		}
		if(StringUtils.isNotEmpty(spid)) {
			queryWrapper.eq(BenifitAccount::getSpid, spid);
		}
		if(StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
			Integer intStartDate = Integer.valueOf(startDate.replaceAll("\\-", ""));
			Integer intEndDate = Integer.valueOf(endDate.replaceAll("\\-", ""));
			queryWrapper.between(BenifitAccount::getTheMonth, intStartDate, intEndDate);
		}
		List<BenifitAccount> resultList = benifitAccountService.list(queryWrapper);
		List<BenifitAccountExcel> orderUsersExcelList = new ArrayList<BenifitAccountExcel>();
		if(resultList != null && resultList.size() > 0) {
			for(BenifitAccount item : resultList) {
				BenifitAccountExcel benifitAccountExcel = BeanUtil.copyProperties(item, BenifitAccountExcel.class);
				benifitAccountExcel.setMoneyPct(benifitAccountExcel.getMoneyPct().multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP));
				orderUsersExcelList.add(benifitAccountExcel);
			}
		}
		ExcelUtil.export(response, "权益利润率估算表", "数据", orderUsersExcelList, BenifitAccountExcel.class);
	}
}
