package com.fjhx.controller.material;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.base.Condition;
import com.fjhx.entity.material.Material;
import com.fjhx.params.JumpVo;
import com.fjhx.service.flow.MaterialUpdateFlowService;
import com.fjhx.service.material.MaterialService;
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-05
 */
@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private MaterialUpdateFlowService materialUpdateFlowService;

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

    /**
     * 物料修改创建流程
     */
    @PostMapping("/editFlowCreate")
    public R editFlowCreate(@RequestBody Material material) {
        materialUpdateFlowService.create(material);
        return R.success();
    }

    /**
     * 物料修改流程跳转
     */
    @PostMapping("/editFlowJump")
    public R editFlowJump(@RequestBody JumpVo jumpVo) {
        materialUpdateFlowService.jump(jumpVo);
        return R.success();
    }

    /**
     * 查看滞留周期
     */
    @PostMapping("/getDelayPeriod")
    public R getDelayPeriod() {
        String delayPeriod = materialService.getDelayPeriod();
        return R.data(delayPeriod);
    }

    /**
     * 设置滞留周期
     */
    @PostMapping("/setDelayPeriod")
    public R setDelayPeriod(@RequestBody Map<String, Integer> condition) {
        materialService.setDelayPeriod(condition);
        return R.success();
    }

    /**
     * 物料用途
     */
    @PostMapping("/getPurpose")
    public R getPurpose(@RequestBody Material material) {
        Integer technologyType = material.getTechnologyType();
        List<String> distinctList = materialService.getDistinctList(
                q -> q.eq(ObjectUtil.isNotEmpty(technologyType), Material::getTechnologyType, technologyType),
                Material::getPurpose);
        return R.success(distinctList);
    }

    /**
     * 获取通过供应商id获取物料
     */
    @PostMapping("/getMaterialBySupplierId")
    public R getMaterialBySupplierId(Map<String, Long> condition) {
        List<Map<String, Object>> result = materialService.getMaterialBySupplierId(condition.get("supplierId"));
        return R.success(result);
    }

    /**
     * 物料库存分页
     */
    @PostMapping("/stockPage")
    public R stockPage(@RequestBody Condition condition) {
        Page<Map<String, Object>> result = materialService.getStockPage(condition);
        return R.success(result);
    }

    /**
     * 获取物料信息以及库存
     */
    @PostMapping("/getMaterialAndStock")
    public R getMaterialAndStock(@RequestBody Condition condition) {
        Page<Map<String, Object>> result = materialService.getMaterialAndStock(condition);
        return R.success(result);
    }

}

