package com.ly.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
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.ManageDao;
import com.ly.domain.Approval;
import com.ly.domain.Article;
import com.ly.domain.Manage;
import com.ly.domain.User;
import com.ly.exception.ServiceException;
import com.ly.service.ArticleService;
import com.ly.service.ManageService;
import com.ly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage")
public class ManageController {
    @Autowired
    private ManageService manageService;
    @Autowired
    private ManageDao manageDao;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @PostMapping
    public Result save(@RequestBody Manage manage) {
      if (manage.getId()==null){
            manage.setNickname(TokenUtils.getCurrentUser().getNickname());


      }


        manageService.saveOrUpdate(manage);
        return Result.success();
    }

    @PutMapping
    public Result updates(@RequestBody Manage manage){

       String name =  manage.getName();
        QueryWrapper<Article> queryWrapper = new QueryWrapper<Article>();
        queryWrapper.eq("name",name);
        Article getone =  articleService.getOne(queryWrapper);
        getone.setStates(manage.getStates());
        articleService.updateById(getone);
        manageService.updateById(manage);
        //  article.setStates(manage.getStates());
        //   articleService.updateById(article);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        manageService.removeById(id);
        return Result.success();
    }

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

    @GetMapping
    public Result findAll() {
        return Result.success(manageService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(manageService.getById(id));
    }
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Manage> queryWrapper = 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("nickname", currentUser.getNickname());

       }  if (RoleEnum.tenderee.toString().equals(currentUser.getRole())){  // 角色是
            queryWrapper.eq("user", currentUser.getNickname());

        }
        return Result.success(manageService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

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

    @GetMapping("/pages")
    public Result findPages(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Manage> queryWrapper = 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("nickname", currentUser.getNickname()).eq("states","已中标");

        }
        if (RoleEnum.tenderee.toString().equals(currentUser.getRole())){
            queryWrapper.eq("nickname", currentUser.getNickname()).eq("states","已中标");

        }
        return Result.success(manageService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}
