package com.qunzhixinxi.hnqz.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.entity.WmDaCustomer;
import com.qunzhixinxi.hnqz.admin.service.WmDaCustomerStatisticsService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * 统计管理-月度销售报表
 *
 * @author lixuesong
 * @date 2021年06月04日 15:44
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wmDaCustomerStatistics")
@Api(value = "wmDaCustomerStatistics", tags = "月度销售报表")
public class WmDaCustomerStatisticsController {

	private final WmDaCustomerStatisticsService wmDaCustomerStatisticsService;

	/**
	 * 查询当月或多月的销售列表
	 *
	 * @param page
	 * @param wmDaCustomer
	 * @return
	 */
	@GetMapping("/list")
	public R listMultiMonthStatisticsPage(Page page, WmDaCustomer wmDaCustomer, Integer startMonth, Integer endMonth) {
		try {
			// 拼接查询条件
			LambdaQueryWrapper<WmDaCustomer> queryWrapper = Wrappers.lambdaQuery();
			if (wmDaCustomer != null) {
				// 客户名称
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerName())) {
					queryWrapper.like(WmDaCustomer::getCustomerName, wmDaCustomer.getCustomerName());
				}
				// 客户类别
				if (wmDaCustomer.getCustomerType() != null) {
					queryWrapper.eq(WmDaCustomer::getCustomerType, wmDaCustomer.getCustomerType());
				}
				// 客户经理
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerManager())) {
					queryWrapper.like(WmDaCustomer::getCustomerManager, wmDaCustomer.getCustomerManager());
				}
				// 所属渠道
				if (StringUtils.isNotBlank(wmDaCustomer.getChannelName())) {
					queryWrapper.like(WmDaCustomer::getChannelName, wmDaCustomer.getChannelName());
				}
			}
			if (startMonth != null && endMonth != null) {
				if (startMonth < 1 || startMonth > 12 || endMonth < 1 || endMonth > 12 || startMonth > endMonth) {
					return R.failed("开始时间或结束时间错误");
				}
			} else {
				// 如果为空，则查当月
				int currentMonth = LocalDate.now().getMonthValue();
				startMonth = currentMonth;
				endMonth = currentMonth;
			}
			// 有开始结束时间则查询指定月份的业绩数据，否则查询当月的业绩
			IPage<WmDaCustomer> iPage = wmDaCustomerStatisticsService.listMultiMonthStatisticsPage(page, wmDaCustomer, startMonth, endMonth);
			return R.ok(iPage);
		} catch (Exception e) {
			log.error("查询失败", e);
		}
		return R.failed("查询失败");
	}

	/**
	 * 导出指定月的统计列表
	 *
	 * @param wmDaCustomer
	 * @param startMonth
	 * @param endMonth
	 * @return
	 */
	@GetMapping("/export")
	public R exportMultiMonthStatistics(WmDaCustomer wmDaCustomer, Integer startMonth, Integer endMonth) {
		try {
			// 拼接查询条件
			LambdaQueryWrapper<WmDaCustomer> queryWrapper = Wrappers.lambdaQuery();
			if (wmDaCustomer != null) {
				// 客户名称
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerName())) {
					queryWrapper.like(WmDaCustomer::getCustomerName, wmDaCustomer.getCustomerName());
				}
				// 客户类别
				if (wmDaCustomer.getCustomerType() != null) {
					queryWrapper.eq(WmDaCustomer::getCustomerType, wmDaCustomer.getCustomerType());
				}
				// 客户经理
				if (StringUtils.isNotBlank(wmDaCustomer.getCustomerManager())) {
					queryWrapper.like(WmDaCustomer::getCustomerManager, wmDaCustomer.getCustomerManager());
				}
				// 所属渠道
				if (StringUtils.isNotBlank(wmDaCustomer.getChannelName())) {
					queryWrapper.like(WmDaCustomer::getChannelName, wmDaCustomer.getChannelName());
				}
			}
			if (startMonth != null && endMonth != null) {
				if (startMonth < 1 || startMonth > 12 || endMonth < 1 || endMonth > 12 || startMonth > endMonth) {
					return R.failed("开始时间或结束时间错误");
				}
			} else {
				// 有开始结束时间则查询指定月份的业绩数据，否则查询当月的业绩
				int monthValue = LocalDate.now().getMonthValue();
				startMonth = monthValue;
				endMonth = monthValue;
			}
			List<WmDaCustomer> customerList = wmDaCustomerStatisticsService.listMultiMonthStatistics(wmDaCustomer, startMonth, endMonth);
			// 导出excel
			return wmDaCustomerStatisticsService.exportMultiMonthStatistics(customerList);
		} catch (Exception e) {
			log.error("导出失败", e);
		}
		return R.failed("导出失败");
	}

}
