package com.ly.controller;

import cn.hutool.core.collection.CollUtil;
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.RoleEnum;
import com.ly.controller.util.TokenUtils;
import com.ly.dao.ArticleDao;
import com.ly.domain.*;
import com.ly.exception.ServiceException;
import com.ly.service.ArticleService;
import com.ly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

@RequestMapping("/article")
@RestController
public class ArticleController  {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private UserService userService;
    @PostMapping
    public Result save(@RequestBody Article article) {
        if(article.getId()==null){
        article.setUser(TokenUtils.getCurrentUser().getNickname());
       // article.setUser(TokenUtils.getCurrentUser().getUsername());
            QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
            queryWrapper.eq("nickname",TokenUtils.getCurrentUser().getNickname());
            User one =  userService.getOne(queryWrapper);

            userService.updateById(one);

        }else {
            return Result.error(Constants.CODE_400, "参数错误");

        }
        articleService.saveOrUpdate(article);
        return Result.success();


    }
    @PostMapping("save1")
    public Result save1(@RequestBody Article article) {
        if(article.getId()==null){
            article.setUser(TokenUtils.getCurrentUser().getNickname());
            article.setUid(TokenUtils.getCurrentUser().getId());
            List<Article> list = articleService.list(new QueryWrapper<Article>()
                    .eq("uid", TokenUtils.getCurrentUser().getId())
                    .eq("user", TokenUtils.getCurrentUser().getNickname()));
            if (CollUtil.isNotEmpty(list)){
                throw  new ServiceException("-1","您已经投标过项目了！");
            }
        }
        articleService.saveOrUpdate(article);
        return Result.success();
    }
    @PutMapping
    public Result updates(@RequestBody Article article){
        article.setState("待审核");
        article.setFeedback("");
        articleService.updateById(article);
        return Result.success();
    }
    @PutMapping("update")
    public Result update1(@RequestBody Article article){
        articleService.updateById(article);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        articleService.removeById(id);
        return Result.success();
    }

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

        return Result.success(articleService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(articleService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Manage> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        User currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.bidder.toString().equals(currentUser.getRole())) {
            queryWrapper.eq("user",currentUser.getNickname()).or().eq("state","审核通过").eq("states","待开标");

        }
        if (RoleEnum.tenderee.toString().equals(currentUser.getRole())) {
            queryWrapper.eq("user",currentUser.getNickname());
        }
        return Result.success(articleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
