package com.ly.controller;

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

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

@RestController
@RequestMapping("/tender")
public class TenderController {
    @Resource
    private TenderService tenderService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private BidsService bidsService;
    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Tender tender) {

        if(tender.getId()==null){
            List<Tender> list = tenderService.list(new QueryWrapper<Tender>().eq("pid", tender.getPid())
                    .eq("uid", TokenUtils.getCurrentUser().getId()));
            if (CollUtil.isNotEmpty(list)){
                throw  new ServiceException("-1","您已经投标过该项目了！");
            }
            tender.setTime(DateUtil.now());
            tender.setUid(TokenUtils.getCurrentUser().getId());
            tender.setUser(TokenUtils.getCurrentUser().getNickname());

        }
        tenderService.saveOrUpdate(tender);
        return Result.success();
    }
    @PutMapping
    public Result updates(@RequestBody Tender tender){


        Integer pid =  tender.getPid();
        QueryWrapper<Bids> queryWrapper = new QueryWrapper<Bids>();
        queryWrapper.eq("id",pid);
        Bids getone =  bidsService.getOne(queryWrapper);
        getone.setStates(tender.getStates());
        bidsService.updateById(getone);
        tenderService.updateById(tender);
        return Result.success();
    }
    @PutMapping("/update")
    public Result updates1(@RequestBody Tender tender){

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

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

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

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

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Tender> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Article>queryWrapper1=new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
      //  Page<Tender> page = tenderService.findPage(new Page<>(pageNum, pageSize), name);
        User currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.bidder.toString().equals(currentUser.getRole())){  // 角色是投标人
            queryWrapper.eq("uid", currentUser.getId());

        }


       if (RoleEnum.tenderee.toString().equals(currentUser.getRole())){  // 角色是招标人
           queryWrapper.eq("tid", currentUser.getId());

        }
        return Result.success(tenderService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @GetMapping("/pages")
    public Result findPages(@RequestParam(defaultValue = "") String name,
                            @RequestParam Integer pageNum,
                            @RequestParam Integer pageSize) {
        QueryWrapper<Tender> 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("uid", currentUser.getId()).eq("states","已中标");

        }
        if (RoleEnum.tenderee.toString().equals(currentUser.getRole())){

            queryWrapper.eq("tid", currentUser.getId()).eq("states","已中标");

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

}
