package org.jeecg.modules.ctop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.ctop.service.IMaterialTopService;
import org.jeecg.modules.ctop.vo.MaterialTopVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  Created by JQ.bi on 2020.5.21
 */
@Slf4j
@RestController
@RequestMapping("/ctop/material")
public class MaterialTopCtrl {

    @Autowired
    IMaterialTopService materialTopService;

    @PostMapping("/top/cost")
    public Result<IPage<MaterialTopVO>> getCostTop(@RequestBody JSONObject requestBody,
                                                   @RequestParam(defaultValue = "cost") String target,
                                                   @RequestParam(defaultValue = "desc") String order,
                                                   @RequestParam(defaultValue = "1") int pageNo,
                                                   @RequestParam(defaultValue = "10") int pageSize) {
        Result<IPage<MaterialTopVO>> result = new Result<>();
        if(requestBody.isEmpty()){
            log.error("request body is empty");
            result.setSuccess(false);
        }
        try {
            String startDate= requestBody.getString("startDate");
            String endDate= requestBody.getString("endDate");
            if(startDate.isEmpty()||endDate.isEmpty()){
                log.error("startDate or endDate is empty");
                result.setSuccess(false);
            }
            IPage<MaterialTopVO> voiPage = materialTopService.selectByPage(endDate, startDate, target, order, pageNo, pageSize);
            result.setResult(voiPage);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.setSuccess(false);
        }
        return result;
    }
}
