package com.JN.demo.bizquery.controller;

import com.JN.common.R;
import com.JN.demo.bizquery.dto.BizDto;
import com.JN.demo.bizquery.service.BizService;
import com.JN.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BizController
 * @Description 业务查询的controller类
 * @Author JN
 * @Date 2024/1/8 8:54
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/bizquery")
public class BizController {

    @Autowired
    BizService bizService;

    @PostMapping("list")
    public R<List<BizDto>> queryBizList(@RequestParam("name") String name) {
        log.info("开始调用sql----------------------");
        log.info(name);

        log.info(String.valueOf(bizService == null));
        List<BizDto> resultList = bizService.queryBizListByName(name);
        if(resultList.size() == 0) {
            return R.error("您查询的客户并未与本公司发生业务关系，查询结果为空！");
        } else {
            return R.success(resultList);
        }

    }

    @PostMapping("detail")
    public R<Map<String, String>> queryBizDetail(@RequestParam("kunnr") String kunnr, @RequestParam("name") String name,
                               @RequestParam("time") String time, @RequestParam("employeeId") String employeeId) {
        //根据传入的时间前后获取格式化后的时间
        LocalDate currentDate = LocalDate.now();
        LocalDate manipulatedDate;
        String yearAgoFormat;
        //通过传入的过去时间值来格式化对应的日期
        switch (time) {
            case "1":
                manipulatedDate = currentDate.minusYears(1);
                break;
            case "2":
                manipulatedDate = currentDate.minusYears(2);
                break;
            case "3":
                manipulatedDate = currentDate.minusYears(3);
                break;
            default:
                manipulatedDate = currentDate;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        //当前日期和之前的日期格式化
        yearAgoFormat = manipulatedDate.format(formatter);
        //获取最近发货日期，最近下单日期，最近报价日期，处理的业务员数量
        String latestShippingDate = StringUtils.getDefaultIfNull(bizService.queryLatestShippingDate(kunnr, yearAgoFormat), "0");
        String latestOrderDate = StringUtils.getDefaultIfNull(bizService.queryLatestOrderDate(kunnr, yearAgoFormat), "0");
        String latestQuotationDate = StringUtils.getDefaultIfNull(bizService.queryLatestQuotationDate(name), "0");
        String salespersonCount = StringUtils.getDefaultIfNull(bizService.querySalespersonCount(kunnr, yearAgoFormat), "0");
        String latestContractDate = StringUtils.getDefaultIfNull(bizService.queryLatestContractDate(kunnr, yearAgoFormat), "0");
        String latestBidDate = StringUtils.getDefaultIfNull(bizService.queryLatestBidDate(kunnr), "0");

        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("latestShippingDate", "0".equals(latestShippingDate) ? latestShippingDate : StringUtils.formatDateString(latestShippingDate, "yyyy-MM-dd"));
        resultMap.put("latestOrderDate", "0".equals(latestOrderDate) ? latestOrderDate : StringUtils.formatDateString(latestOrderDate, "yyyy-MM-dd"));
        resultMap.put("latestQuotationDate", "0".equals(latestQuotationDate) ? latestQuotationDate : StringUtils.formatDateString(latestQuotationDate, "yyyy-MM-dd"));
        resultMap.put("salespersonCount", salespersonCount );
        resultMap.put("latestContractDate", "0".equals(latestContractDate) ? latestContractDate : StringUtils.formatDateString(latestContractDate, "yyyy-MM-dd"));
        resultMap.put("latestBidDate", "0".equals(latestBidDate) ? latestBidDate : StringUtils.formatDateString(latestBidDate, "yyyy-MM-dd"));

        log.info("开始记录日志信息: {}", "工号为：" + employeeId + "的用户查询的客户名称为：" + name);
        return R.success(resultMap);
    }

}
