package com.ly.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.controller.util.Constants;
import com.ly.controller.util.Result;
import com.ly.controller.util.TokenUtils;
import com.ly.dao.ArticleDao;
import com.ly.domain.Article;
import com.ly.domain.Budget;
import com.ly.domain.Course;
import com.ly.domain.Dict;
import com.ly.service.ArticleService;
import com.ly.service.BudgetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/budget")
@RestController
public class BudgetController {
    @Autowired
    private BudgetService budgetService;

    @Autowired
    private ArticleDao articleDao;

    @PostMapping
    public Result save(@RequestBody Budget budget) {
        if(budget.getId()==null){
            budget.setTime(DateUtil.now());

            budget.setUser(TokenUtils.getCurrentUser().getNickname());

        }
        budgetService.saveOrUpdate(budget);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        budgetService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        budgetService.removeByIds(ids);
        return Result.success();
    }
    @GetMapping
    public Result findAll() {

        return Result.success(budgetService.list());
    }
    @PostMapping("/articleBudget/{budgetId}/{articleId}")
    public Result articleBudget(@PathVariable Integer budgetId, @PathVariable Integer articleId) {
        budgetService.setArticleBudget(budgetId, articleId);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(budgetService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
       // QueryWrapper<Budget> queryWrapper = new QueryWrapper<>();
      //  queryWrapper.orderByDesc("id");
    //    if (StrUtil.isNotBlank(name)) {
     //       queryWrapper.like("name", name);
   //     }
      //  return Result.success(budgetService.page(new Page<>(pageNum, pageSize), queryWrapper));
        Page<Budget> page = budgetService.findPage(new Page<>(pageNum, pageSize), name);
        return Result.success(page);

    }

    @GetMapping("/name")
    public Result getName() {
        QueryWrapper<Article>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name", Constants.DICT_TYPE_ICON);
        return Result.success(articleDao.selectList(null));
    }
}
