package com.njyunzhi.pet_identity.controller;

import com.njyunzhi.pet_identity.common.BaseController;
import com.njyunzhi.pet_identity.common.Constants;
import com.njyunzhi.pet_identity.common.DateUtils;
import com.njyunzhi.pet_identity.common.ResponseBean;
import com.njyunzhi.pet_identity.service.ITaApplicationService;
import com.njyunzhi.pet_identity.service.ITaPetIdentityService;
import com.njyunzhi.pet_identity.vo.StatisValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "查询统计")
@RestController
@RequestMapping("/admin/statis")
public class StatisController extends BaseController {

    @Autowired
    ITaPetIdentityService iTaPetIdentityService;

    @Autowired
    ITaApplicationService iTaApplicationService;

    /**
     * 工作台通用统计
     * @return
     */
    @RequestMapping(value="/common",method= RequestMethod.GET)
    @ApiOperation(value="工作台通用统计", notes = "工作台通用统计", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean getStatisCommon() throws Exception {
        String today = DateUtils.today();
        LocalDateTime todayStart = DateUtils.getDayStart(today);
        LocalDateTime todayEnd = DateUtils.getDayEnd(today);

        // 把 today 的日期换成 01 就是月初
        String[] parts = today.split("-");
        parts[2] = "01";
        String monthFirstDay = String.join("-", parts);
        LocalDateTime monthStart = DateUtils.getDayStart(monthFirstDay);

        int personTotal = iTaPersonService.countRegiste();
        int cartTotal = iTaPetIdentityService.countNotDelete();
        int notAuditNum = iTaApplicationService.countBy("status", Constants.WORKFLOW_STATUS_PROCESSING, true);
        int rejectNum = iTaApplicationService.countBy("verify_status", Constants.AUDIT_STATUS_REJECT, true);
        int todayApplyNum = iTaApplicationService.countBetween(todayStart, todayEnd);
        int monthApplyNum = iTaApplicationService.countBetween(monthStart, todayEnd);

        List<StatisValue> result = new ArrayList<>();
        result.add(new StatisValue().setName("personTotal").setValue(String.valueOf(personTotal)));
        result.add(new StatisValue().setName("cartTotal").setValue(String.valueOf(cartTotal)));
        result.add(new StatisValue().setName("notAuditNum").setValue(String.valueOf(notAuditNum)));
        result.add(new StatisValue().setName("rejectNum").setValue(String.valueOf(rejectNum)));
        result.add(new StatisValue().setName("todayApplyNum").setValue(String.valueOf(todayApplyNum)));
        result.add(new StatisValue().setName("monthApplyNum").setValue(String.valueOf(monthApplyNum)));

        return ResponseBean.success(result);
    }
}
