package com.fjhx.supplier.controller;

import com.fjhx.base.ListPageMap;
import com.fjhx.entity.supplier.Supplier;
import com.fjhx.supplier.service.SupplierService;
import com.fjhx.utils.BigDecimalUtil;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private SupplierService supplierService;

    /**
     * 列表
     *
     * @param condition
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> condition) {
        ListPageMap.getListPageMap(condition);
        List<Supplier> result = supplierService.getList(condition);
        return R.success(result);
    }

    /**
     * 列表条数
     *
     * @param condition
     * @return
     */
    @PostMapping("/list/count")
    public R listCount(@RequestBody Map<String, Object> condition) {
        ListPageMap.getListPageMap(condition);
        Integer count = supplierService.getListCount(condition);
        return R.success(count);
    }

    /**
     * 供应商采购列表
     *
     * @param condition
     * @return
     */
    @PostMapping("/purchase/list")
    public R purchaseList(@RequestBody Map<String, Object> condition) {
        ListPageMap.getListPageMap(condition);
        List<Supplier> result = supplierService.getPurList(condition);
        return R.success(result);
    }

    /**
     * 供应商采购列表条数
     *
     * @param condition
     * @return
     */
    @PostMapping("/purchase/list/count")
    public R purchaseListCount(@RequestBody Map<String, Object> condition) {
        ListPageMap.getListPageMap(condition);
        Integer count = supplierService.getPurListCount(condition);
        return R.success(count);
    }

    /**
     * 统计物料分类
     *
     * @return
     */
    @PostMapping("/statisticsClassify")
    public R statisticsClassify(@RequestBody Map<String, Object> condition) {
        List<Supplier> list = supplierService.statisticsClassify(condition);
        return R.success(list);
    }

    /**
     * 添加
     *
     * @param supplier
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Supplier supplier) {
        supplierService.add(supplier);
        return R.success();
    }

    /**
     * 修改
     *
     * @param supplier
     * @return
     */
    @PostMapping("/edit")
    public R edit(@RequestBody Supplier supplier) {
        supplierService.edit(supplier);
        return R.success();
    }

    /**
     * 删除
     *
     * @param supplier
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Supplier supplier) {
        supplierService.delete(supplier);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public R detail(@RequestParam("id") String id) {
        return R.success(supplierService.detail(id));
    }

    /**
     * 供应商决策辅助详情
     *
     * @param id
     * @return
     */
    @GetMapping("/su/detail")
    public R suDetail(@RequestParam("id") String id) {
        return R.success(supplierService.getSuById(id));
    }

    /**
     * 供应商采购分析详情
     *
     * @param id
     * @return
     */
    @GetMapping("/su/count")
    public R suCount(@RequestParam("id") String id) {
        return R.success(supplierService.getSuCount(id));
    }


    /**
     * 列表
     *
     * @param condition
     * @return
     */
    @PostMapping("/su/spmAnalyse")
    public R suSpmAnalyse(@RequestBody Map<String, Object> condition) {
        ListPageMap.getListPageMap(condition);
        List<Supplier> result = supplierService.spmAnalyse(condition);
        return R.success(result);
    }

    /**
     * 列表条数
     *
     * @param condition
     * @return
     */
    @PostMapping("/su/spmAnalyse/count")
    public R suSpmAnalyseCount(@RequestBody Map<String, Object> condition) {
        ListPageMap.getListPageMap(condition);
        Integer count = supplierService.spmAnalyseCount(condition);
        return R.success(count);
    }

    /**
     * 月度退货记录
     *
     * @param id
     * @return
     */
    @GetMapping("/month/getSum")
    public R monthGetSum(@RequestParam("id") String id, @RequestParam("date") String date) {
        List<Supplier> list = supplierService.getBackSumMoneyAndCount(id, date);
        return R.success(list);
    }

    /**
     * 退货记录总数和总金额
     *
     * @param id
     * @return
     */
    @GetMapping("/back/sum")
    public R backSum(@RequestParam("id") String id, @RequestParam("date") String date) {
        Map<String, Object> map = supplierService.getAllSumMoneyAndCount(id, date);
        return R.success(map);
    }

    /**
     * 月度采购金额统计
     *
     * @param id
     * @return
     */
    @GetMapping("/purchase/month/sum")
    public R purchaseSum(@RequestParam("id") String id, @RequestParam("date") String date) {
        List<Supplier> list = supplierService.getMonthSumPurMoney(id, date);
        return R.success(list);
    }

    /**
     * 下拉供应商
     *
     * @param condition
     * @return
     */
    @PostMapping("/selectList")
    public R selectList(@RequestBody Map<String, Object> condition) {
        ListPageMap.getListPageMap(condition);
        List<Supplier> list = supplierService.selectList(condition);
        return R.success(list);
    }

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

    /**
     * 账期数量
     */
    @PostMapping("getSafetyWarnDayList")
    public R getSafetyWarnDayList() {
        List<Map<String, Object>> result = supplierService.getSafetyWarnDayList();
        return R.success(result);
    }

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

    @PostMapping("/paymentGetBackAmount")
    public R paymentGetBackAmount(@RequestBody Map<String, String> condition) {
        String purchaseBillNo = condition.get("purchaseBillNo");
        BigDecimal amount = supplierService.paymentGetBackAmount(purchaseBillNo);
        return R.data(BigDecimalUtil.keepDecimals(amount, 2));
    }

}

