package com.yonge.cooleshow.teacher.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.req.TotalReq;
import com.yonge.cooleshow.biz.dal.dto.search.UserAccountRecordSearch;
import com.yonge.cooleshow.biz.dal.enums.InOrOutEnum;
import com.yonge.cooleshow.biz.dal.enums.PeriodEnum;
import com.yonge.cooleshow.biz.dal.enums.TimeTypeEnum;
import com.yonge.cooleshow.biz.dal.service.UserAccountRecordService;
import com.yonge.cooleshow.biz.dal.service.UserAccountService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo;
import com.yonge.cooleshow.biz.dal.vo.UserAccountVo;
import com.yonge.cooleshow.biz.dal.vo.res.AccountTotal;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.exception.BizException;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.utils.date.DateUtil;
import com.yonge.toolset.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/userAccount")
@Api(value = "用户账户表", tags = "用户账户表")
public class UserAccountController extends BaseController {
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private UserAccountRecordService userAccountRecordService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

	/**
     * 查询单条
     */
    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    public HttpResponseResult<UserAccountVo> detail() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
    	return succeed(userAccountService.detail(user.getId()));
	}

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入userAccountRecordSearch")
    public HttpResponseResult<PageInfo<UserAccountRecordVo>> page(@RequestBody UserAccountRecordSearch query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        query.setUserId(user.getId());
        query.setInOrOut(InOrOutEnum.IN);

        if (StringUtil.isEmpty(query.getSearchDate())) {
            query.setSearchDate(DateUtil.format(new Date(), "yyyy-MM"));
        }
        String[] classDateSp = query.getSearchDate().split("-");
        try {
            LocalDate date = LocalDate.of(Integer.parseInt(classDateSp[0]), Integer.parseInt(classDateSp[1]), 1);

            LocalDateTime firstDay = LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
            LocalDateTime lastDay = LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);

            query.setStartTime(firstDay);
            query.setEndTime(lastDay);
        } catch (Exception e) {
            throw new BizException("查询时间格式不正确 [" + query.getSearchDate() + "]");
        }

        IPage<UserAccountRecordVo> pages = userAccountRecordService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "收入数据统计")
    @PostMapping("/accountTotal")
    public HttpResponseResult<AccountTotal> accountTotal(@Valid @RequestBody TotalReq query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        query.setUserId(user.getId());

        //参数处理
        try {
            LocalDateTime firstDay;
            LocalDateTime lastDay;
            if (PeriodEnum.YEAR.equals(query.getTimeType())) {
                LocalDate date = LocalDate.of(Integer.parseInt(query.getDateTime()), 1, 1);
                firstDay = LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
                lastDay = LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
            } else {
                String[] classDateSp = query.getDateTime().split("-");
                LocalDate date = LocalDate.of(Integer.parseInt(classDateSp[0]), Integer.parseInt(classDateSp[1]), 1);

                firstDay = LocalDateTime.of(date.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
                lastDay = LocalDateTime.of(date.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
            }
            query.setStartTime(firstDay);
            query.setEndTime(lastDay);
        } catch (Exception e) {
            throw new BizException("查询时间格式不正确 [" + query.getDateTime() + "]");
        }

        return userAccountService.accountTotal(query);
    }
}