package com.ly.controller;

import cn.hutool.core.collection.CollUtil;
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.dao.BidsDao;
import com.ly.domain.Article;
import com.ly.domain.Bids;
import com.ly.domain.Manage;
import com.ly.domain.User;
import com.ly.exception.ServiceException;
import com.ly.service.ArticleService;
import com.ly.service.BidsService;
import com.ly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RequestMapping("/bids")
@RestController
public class BidsController {
    @Autowired
    private BidsService bidsService;
    @Resource
    private BidsDao bidsDao;
    @Autowired
    private UserService userService;
    @PostMapping
    public Result save(@RequestBody Bids bids) {
        if(bids.getId()==null){
            bids.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, "参数错误");

        }
        bidsService.saveOrUpdate(bids);
        return Result.success();


    }
    @PostMapping("save1")
    public Result save1(@RequestBody Bids bids) {
        if(bids.getId()==null){
            bids.setUser(TokenUtils.getCurrentUser().getNickname());
            bids.setUid(TokenUtils.getCurrentUser().getId());
            List<Bids> list = bidsService.list(new QueryWrapper<Bids>()
                    .eq("uid", TokenUtils.getCurrentUser().getId())
                    .eq("type", bids.getType()));
            List<Bids> list1 = bidsService.list(new QueryWrapper<Bids>()
                    .eq("name", bids.getName()));
            if (CollUtil.isNotEmpty(list)){
                throw  new ServiceException("500","您已经发布过该类型招标项目了！");
            }
            if (CollUtil.isNotEmpty(list1)){
                throw  new ServiceException("500","招标项目名已存在了，请修改！");
            }
        }
        bidsService.saveOrUpdate(bids);
        return Result.success();
    }
    @PutMapping
    public Result updates(@RequestBody Bids bids){
        bids.setState("待审核");
        bids.setFeedback("");
        bidsService.updateById(bids);
        return Result.success();
    }
    @PutMapping("update")
    public Result update1(@RequestBody Bids bids){
        bidsService.updateById(bids);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        bidsService.removeById(id);
        return Result.success();
    }

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

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

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Bids> 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("uid",currentUser.getId());
        }
        return Result.success(bidsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
