package com.dsk.web.controller.search.macroMarket;

import cn.hutool.core.bean.BeanUtil;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.dtos.SpecialPurposeBondsDto;
import com.dsk.system.domain.SpecialBondInformationV1;
import com.dsk.system.domain.SpecialBondProjectsV1;
import com.dsk.system.service.SpecialPurposeBondsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName SpecialPurposeBondsController
 * @Description 专项债项目
 * @Author Dgm
 * @Date 2023/5/18 10:09
 * @Version 1.0.0
 */
@RestController
@RequestMapping(value ="/specialPurposeBonds")
public class SpecialPurposeBondsController {

    @Autowired
    private SpecialPurposeBondsService specialPurposeBondsService;

    /***
     *@Description: 专项债项目分页列表
     *@Param:
     *@return: com.dsk.common.core.domain.AjaxResult
     *@Author: Dgm
     *@date: 2023/5/18 10:29
     */
    @PostMapping("/projects/page")
    public AjaxResult page() {
//        return specialPurposeBondsService.page();

        Map<String, Object> map = BeanUtil.beanToMap(new SpecialBondProjectsV1(), false, false);
        return new AjaxResult(HttpStatus.OK.value(), "", map);
    }


    /***
     *@Description: 专项债项目详情
     *@Param:
     *@return: com.dsk.common.core.domain.AjaxResult
     *@Author: Dgm
     *@date: 2023/5/18 10:29
     */
    @GetMapping("/details/{id}")
    public AjaxResult details(@PathVariable("id") String id) {
        return specialPurposeBondsService.details(id);
    }


    /***
     *@Description: 专项债-项目类别统计
     *@Param:
     *@return: com.dsk.common.core.domain.AjaxResult
     *@Author: Dgm
     *@date: 2023/5/18 10:29
     */
    @PostMapping("/bond/statistics")
    public AjaxResult bondStatistics(@RequestBody SpecialPurposeBondsDto dto) {
//        return specialPurposeBondsService.bondStatistics();

        Map<String, Object> map = BeanUtil.beanToMap(new SpecialPurposeBondsDto(), false, false);

        List<SpecialPurposeBondsDto> list = new ArrayList<>(4);
        SpecialPurposeBondsDto one = new SpecialPurposeBondsDto();
        one.setCount(3);
        one.setProportion(55.55);

        SpecialPurposeBondsDto two = new SpecialPurposeBondsDto();
        two.setCount(8);
        two.setProportion(0.12);

        SpecialPurposeBondsDto three = new SpecialPurposeBondsDto();
        three.setCount(1);
        three.setProportion(33.33);

        SpecialPurposeBondsDto four = new SpecialPurposeBondsDto();
        four.setCount(5);
        four.setProportion(11.11);

        list.add(one);
        list.add(two);
        list.add(three);
        list.add(four);
        switch (dto.getField()) {
            case "count": list = sortedCount(list, dto);
            break;
            case "totalInvestment": list = sortedTotalInvestment(list, dto);
            break;
            case "proportion": list = sortedProportion(list, dto);
            break;
            default:
                break;
        }
        return new AjaxResult(HttpStatus.OK.value(), "", list);
    }


    /***
     *@Description: 专项债分页列表
     *@Param:
     *@return: com.dsk.common.core.domain.AjaxResult
     *@Author: Dgm
     *@date: 2023/5/18 10:29
     */
    @PostMapping("/bond/page")
    public AjaxResult bondPage() {
//        return specialPurposeBondsService.bondPage();

        Map<String, Object> map = BeanUtil.beanToMap(new SpecialBondInformationV1(), false, false);
        return new AjaxResult(HttpStatus.OK.value(), "", map);
    }

    /***
     *@Description:  项目数量
     *@Param:
     *@return: void
     *@Author: Dgm
     *@date: 2023/5/25 11:26
     */
    public List<SpecialPurposeBondsDto> sortedCount(List<SpecialPurposeBondsDto> list, SpecialPurposeBondsDto dto) {
        if ("desc".equals(dto.getOrder())) {
            list = list.stream().sorted(Comparator.comparing(SpecialPurposeBondsDto::getCount).reversed()).collect(Collectors.toList());
        } else {
            list = list.stream().sorted(Comparator.comparing(SpecialPurposeBondsDto::getCount)).collect(Collectors.toList());
        }
        return list;
    }

    /***
     *@Description: 金额
     *@Param:
     *@return: void
     *@Author: Dgm
     *@date: 2023/5/25 11:26
     */
    public List<SpecialPurposeBondsDto> sortedTotalInvestment(List<SpecialPurposeBondsDto> list, SpecialPurposeBondsDto dto) {
        if ("desc".equals(dto.getOrder())) {
            list = list.stream().sorted(Comparator.comparing(SpecialPurposeBondsDto::getTotalInvestment).reversed()).collect(Collectors.toList());
        } else {
            list = list.stream().sorted(Comparator.comparing(SpecialPurposeBondsDto::getTotalInvestment)).collect(Collectors.toList());
        }
        return list;
    }

    /***
     *@Description: 比例
     *@Param:
     *@return: void
     *@Author: Dgm
     *@date: 2023/5/25 11:26
     */
    public List<SpecialPurposeBondsDto> sortedProportion(List<SpecialPurposeBondsDto> list, SpecialPurposeBondsDto dto) {
        if ("desc".equals(dto.getOrder())) {
            list = list.stream().sorted(Comparator.comparing(SpecialPurposeBondsDto::getProportion).reversed()).collect(Collectors.toList());
        } else {
            list = list.stream().sorted(Comparator.comparing(SpecialPurposeBondsDto::getProportion)).collect(Collectors.toList());
        }
        return list;
    }

}
