package org.springblade.modules.ldt.loginuser.controller;

import com.alibaba.fastjson.support.geo.Point;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.SystemConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.ldt.billrecord.entity.BillRecord;
import org.springblade.modules.ldt.billrecord.service.IBillRecordService;
import org.springblade.modules.ldt.channeluserpoint.dto.ChannelUserPointDTO;
import org.springblade.modules.ldt.channeluserpoint.entity.ChannelUserPoint;
import org.springblade.modules.ldt.channeluserpoint.service.IChannelUserPointService;
import org.springblade.modules.ldt.channeluserpoint.vo.ChannelUserPointVO;
import org.springblade.modules.ldt.loginuser.entity.LoginUser;
import org.springblade.modules.ldt.loginuser.service.ILoginUserService;
import org.springblade.modules.ldt.pointdetail.dto.PointDetailDTO;
import org.springblade.modules.ldt.pointdetail.entity.PointDetail;
import org.springblade.modules.ldt.pointdetail.service.IPointDetailService;
import org.springblade.modules.ldt.pointdetail.vo.PointDetailVO;
import org.springblade.modules.ldt.pointdetail.wrapper.PointDetailWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * Created By lidexi in 2021/8/11
 **/
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-ldt/loginuser")
@Api(value = "", tags = "接口")
public class AppUserController {

	private final IChannelUserPointService channelUserPointService;
	private final IPointDetailService pointDetailService;
	private final IBillRecordService billRecordService;

	/**
	 * 渠道积分记录分页
	 */
	@GetMapping("/channeluserpoint/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入channelUserPoint")
	public R<IPage<PointDetailVO>> channeluserpointList(PointDetailDTO pointDetailDTO, Query query) {
		PointDetail pointDetail = Objects.requireNonNull(BeanUtil.copy(pointDetailDTO,PointDetail.class));
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		String selectMonth = null;
		try {
			selectMonth = simpleDateFormat.format(simpleDateFormat.parse(pointDetailDTO.getMonthTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		IPage<PointDetail> pages = pointDetailService.page(Condition.getPage(query), Condition.getQueryWrapper(pointDetail).lambda()
			.like(selectMonth != null,PointDetail::getCreateTime,selectMonth)
			.eq(PointDetail::getBillRecordStatus,SystemConstant.BillRecordPayStatus.PAYED.getValue())
			.isNotNull(PointDetail::getChannelId)
			.in(PointDetail::getType,
				Arrays.asList(SystemConstant.PointDetailType.CHANNEL.getValue(),SystemConstant.PointDetailType.MALL_SEND.getValue(),SystemConstant.PointDetailType.USER_PAY.getValue())));
		return R.data(PointDetailWrapper.build(billRecordService).pageVO(pages));
	}

	/**
	 * 余额分页
	 */
	@GetMapping("/remaining/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入channelUserPoint")
	public R<IPage<PointDetailVO>> list(PointDetailDTO pointDetailDTO, Query query) {
		PointDetail pointDetail = Objects.requireNonNull(BeanUtil.copy(pointDetailDTO,PointDetail.class));
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		String selectMonth = null;
		try {
			selectMonth = simpleDateFormat.format(simpleDateFormat.parse(pointDetailDTO.getMonthTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		IPage<PointDetail> pages = pointDetailService.page(Condition.getPage(query), Condition.getQueryWrapper(pointDetail).lambda()
			.like(selectMonth != null,PointDetail::getCreateTime,selectMonth)
			.eq(PointDetail::getBillRecordStatus,SystemConstant.BillRecordPayStatus.PAYED.getValue())
			.in(PointDetail::getType,Arrays.asList(SystemConstant.PointDetailType.USER_PAY.getValue(),SystemConstant.PointDetailType.SHOP_SEND.getValue())));
		return R.data(PointDetailWrapper.build(billRecordService).pageVO(pages));
	}
}
