package cn.iocoder.yudao.module.visit.controller.admin.home;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.visit.controller.admin.home.vo.*;
import cn.iocoder.yudao.module.visit.service.customerinfo.CustomerInfoService;
import cn.iocoder.yudao.module.visit.service.home.HomeService;
import cn.iocoder.yudao.module.visit.service.product.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 首页信息")
@RestController
@RequestMapping("/visit/home")
@Validated
public class HomeController {

    @Resource
    private CustomerInfoService customerInfoService;
    @Resource
    private ProductService  productService;
    @Resource
    private HomeService homeService;


    @GetMapping("/getHomeInfoFirst")
    @Operation(summary = "获得首页第一排信息")
    public CommonResult<HomeFirstRespVO> getHomeInfoFirst(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeFirstRespVO data = homeService.getHomeInfoFirst(reqVO);
        return success(data);
    }


    @GetMapping("/getHomeInfoBfztj")
    @Operation(summary = "获得首页拜访周统计")
    public CommonResult<HomeBfztjRespVO> getHomeInfoBfztj(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeBfztjRespVO data = homeService.getHomeInfoBfztj(reqVO);
        return success(data);
    }


    @GetMapping("/getHomeInfoKhxzdjzbqk")
    @Operation(summary = "获得首页客户性质等级占比情况")
    public CommonResult<HomeKhxzdjzbqkRespVO> getHomeInfoKhxzdjzbqk(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeKhxzdjzbqkRespVO data = homeService.getHomeInfoKhxzdjzbqk(reqVO);
        return success(data);
    }

    @GetMapping("/getHomeInfoBfrjfbqk")
    @Operation(summary = "获得首页拜访人均分布情况")
    public CommonResult<HomeKhxzdjzbqkRespVO> getHomeInfoBfrjfbqk(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeKhxzdjzbqkRespVO data = homeService.getHomeInfoBfrjfbqk(reqVO);
        return success(data);
    }

    @GetMapping("/getHomeInfoKhbffszbqk")
    @Operation(summary = "获得首页客户拜访方式占比情况")
    public CommonResult<HomeKhxzdjzbqkRespVO> getHomeInfoKhbffszbqk(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeKhxzdjzbqkRespVO data = homeService.getHomeInfoKhbffszbqk(reqVO);
        return success(data);
    }

    @GetMapping("/getHomeInfoKhbmzbqk")
    @Operation(summary = "获得首页客户部门占比情况")
    public CommonResult<HomeKhxzdjzbqkRespVO> getHomeInfoKhbmzbqk(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeKhxzdjzbqkRespVO data = homeService.getHomeInfoKhbmzbqk(reqVO);
        return success(data);
    }

    @GetMapping("/getHomeInfoKhbflxzbqk")
    @Operation(summary = "获得首页客户拜访类型占比情况")
    public CommonResult<HomeKhxzdjzbqkRespVO> getHomeInfoKhbflxzbqk(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeKhxzdjzbqkRespVO data = homeService.getHomeInfoKhbflxzbqk(reqVO);
        return success(data);
    }

    @GetMapping("/getHomeInfoBfcplxzbqk")
    @Operation(summary = "获得首页拜访产品类型占比情况")
    public CommonResult<HomeKhxzdjzbqkRespVO> getHomeInfoBfcplxzbqk(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeKhxzdjzbqkRespVO data = homeService.getHomeInfoBfcplxzbqk(reqVO);
        return success(data);
    }


    @GetMapping("/getHomeInfoBfrtj")
    @Operation(summary = "获得首页拜访日统计")
    public CommonResult<HomeBfrtjRespVO> getHomeInfoBfrtj(@Valid HomeReqVO reqVO) {
        LocalDateTime[] localDateTimes = parseSearchTime(reqVO.getSearchTimeString());
        reqVO.setSearchTime(localDateTimes);
        HomeBfrtjRespVO data = homeService.getHomeInfoBfrtj(reqVO);
        return success(data);
    }




    //将前端传的查询时间String[] 转换为LocalDateTime[]
    public static LocalDateTime[] parseSearchTime(String[] searchTime) {
        if (searchTime == null || searchTime.length != 2 ||
                Objects.equals(searchTime[0], "") || Objects.equals(searchTime[1], "")) {
            return null;
        }

        String startStr = searchTime[0];
        String endStr = searchTime[1];

        LocalDateTime startTime;
        LocalDateTime endTime;

        if (startStr.matches("^\\d{4}$") && endStr.matches("^\\d{4}$")) {
            // 年
            startTime = Year.parse(startStr).atDay(1).atStartOfDay();
            endTime = Year.parse(endStr).atMonth(12).atEndOfMonth().atTime(23, 59, 59);
        } else if (startStr.matches("^\\d{4}-\\d{2}$") && endStr.matches("^\\d{4}-\\d{2}$")) {
            // 年-月
            startTime = YearMonth.parse(startStr).atDay(1).atStartOfDay();
            endTime = YearMonth.parse(endStr).atEndOfMonth().atTime(23, 59, 59);
        } else if (startStr.matches("^\\d{4}-\\d{2}-\\d{2}$") && endStr.matches("^\\d{4}-\\d{2}-\\d{2}$")) {
            // 年-月-日
            startTime = LocalDate.parse(startStr).atStartOfDay();
            endTime = LocalDate.parse(endStr).atTime(23, 59, 59);
        } else {
            throw new IllegalArgumentException("searchTime 格式非法，必须是 [yyyy] 或 [yyyy-MM] 或 [yyyy-MM-dd]");
        }

        return new LocalDateTime[]{startTime, endTime};
    }


}