package com.fjhx.controller.supplier;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.entity.common.CommonFile;
import com.fjhx.entity.supplier.Supplier;
import com.fjhx.params.supplier.SupplierVo;
import com.fjhx.service.common.CommonFileService;
import com.fjhx.service.supplier.SupplierService;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 供应商 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private CommonFileService commonFileService;

    @PostMapping("/page")
    public R page(@RequestBody Map<String, String> condition) {
        Page<Map<String, Object>> result = supplierService.getPage(condition);
        return R.success(result);
    }

    @PostMapping("/add")
    public R add(@RequestBody SupplierVo supplierVo) {
        supplierService.add(supplierVo);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody SupplierVo supplierVo) {
        supplierService.edit(supplierVo);
        return R.success();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody SupplierVo supplierVo) {
        supplierService.delete(supplierVo);
        return R.success();
    }

    /**
     * 供应商其他资质
     */
    @PostMapping("/getOtherList")
    public R getOtherList(@RequestBody Supplier supplier) {
        List<CommonFile> commonFiles = commonFileService.fileList(supplier.getId());
        return R.success(commonFiles);
    }

    /**
     * 供应商详情
     */
    @PostMapping("/details")
    public R details(@RequestBody Supplier supplier) {
        supplier = supplierService.getById(supplier.getId());
        return R.success(supplier);
    }

    /**
     * 供应商名称集合
     */
    @PostMapping("/getNameList")
    public R getNameList() {
        return R.success(supplierService.getDistinctList(Supplier::getName));
    }

    /**
     * 供应商账期
     */
    @PostMapping("/getAccountDate")
    public R getAccountDate() {
        List<Map<String, Object>> result = supplierService.getAccountDate();
        return R.success(result);
    }

    /**
     * 供应商采购周期
     */
    @PostMapping("/getSafetyWarnDay")
    public R getSafetyWarnDay() {
        List<Map<String, Object>> result = supplierService.getSafetyWarnDay();
        return R.success(result);
    }

    /**
     * 供应商条件统计
     */
    @PostMapping("/getStatistics")
    public R getStatistics(@RequestBody Map<String, String> condition) {
        List<Map<String, Object>> result = supplierService.getStatistics(condition);
        return R.success(result);
    }

    /**
     * 合同采购概况
     */
    @PostMapping("/pagePurchase")
    public R pagePurchase(@RequestBody Map<String, String> condition) {
        Page<Map<String, Object>> result = supplierService.pagePurchase(condition);
        return R.success(result);
    }

}

