package com.ruoyi.web.v2.v1.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.app.model.Supplier;
import com.ruoyi.app.service.ISupplierService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.entity.SupplierEva;
import com.ruoyi.web.v2.v1.service.ISupplierEvaService;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-11-21
 */
@RestController
@RequestMapping("/supplier-eva")
public class SupplierEvaController {
    @Autowired
    private ISupplierEvaService supplierEvaService;
    @Autowired
    private ISupplierService supplierService;


    @PostMapping("/add")
    public AjaxResult add(@RequestBody SupplierEva checkProject) throws JsonProcessingException {
        supplierEvaService.save(checkProject);
        Supplier supplier = new Supplier();
        supplier.setId((long) checkProject.getSupplierId());
        supplier.setEvaluate(checkProject.getZongfen()+"");
        supplierService.updateSupplier(supplier);
        return success();
    }


    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SupplierEva checkProject) throws JsonProcessingException {
        Supplier supplier = new Supplier();
        supplier.setId((long) checkProject.getSupplierId());
        supplier.setEvaluate(checkProject.getZongfen()+"");
        supplierService.updateSupplier(supplier);
        return success(supplierEvaService.updateById(checkProject));
    }


    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            supplierEvaService.removeById(s);
        }
        return success();
    }


    @PostMapping("/list")
    public AjaxResult listAll(@RequestBody Map<String, Object> paramsMap){
        return success(supplierEvaService.list(getMapper(paramsMap)));
    }
    @PostMapping("/getLastOne")
    public AjaxResult getLastOne(@RequestBody Map<String, Object> paramsMap){
        QueryWrapper<SupplierEva> mapper = getMapper(paramsMap);
        mapper.orderByDesc("id");
        mapper.last("limit 1");
        return success(supplierEvaService.list(mapper));
    }


    @PostMapping("/page")
    public AjaxResult page(@RequestBody Map<String, Object> paramsMap) {
        String pageNum = (String)paramsMap.get("pageNum");
        String pageSize =  (String)paramsMap.get("pageSize");
        QueryWrapper<SupplierEva> queryWrapper =getMapper(paramsMap);
        return success(supplierEvaService.page(new Page<SupplierEva>(Long.parseLong(pageNum),Long.parseLong(pageSize)),queryWrapper));
    }

    private QueryWrapper<SupplierEva> getMapper(Map<String, Object> paramsMap) {
        Integer supplierId = (Integer)paramsMap.get("supplierId");
        String supplier_name = (String)paramsMap.get("supplierName");
        String startDate = (String)paramsMap.get("startDate");
        String endDate = (String)paramsMap.get("endDate");
        Integer  dengji = (Integer)paramsMap.get("dengji");
        QueryWrapper<SupplierEva> supplierEvaQueryWrapper = new QueryWrapper<>();
        supplierEvaQueryWrapper.eq(StringUtils.isNotEmpty(supplier_name),"supplier_name",supplier_name);
        supplierEvaQueryWrapper.eq(ObjectUtil.isNotEmpty(supplierId),"supplier_id",supplierId);
        supplierEvaQueryWrapper.ge(StringUtils.isNotEmpty(startDate),"eva_date",startDate);
        supplierEvaQueryWrapper.le(StringUtils.isNotEmpty(endDate),"eva_date",endDate);
        supplierEvaQueryWrapper.eq(ObjectUtil.isNotEmpty(dengji),"dengji",dengji);
        return supplierEvaQueryWrapper;
    }


    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(supplierEvaService.getById(id));
    }
}
