package com.fjhx.controller.bom;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.entity.bom.Bom;
import com.fjhx.params.bom.BomEx;
import com.fjhx.params.bom.BomVo;
import com.fjhx.service.bom.BomService;
import com.fjhx.utils.Assert;
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>
 * bom 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-07-01
 */
@RestController
@RequestMapping("/bom")
public class BomController {

    @Autowired
    private BomService bomService;

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

    @PostMapping("/add")
    public R add(@RequestBody BomVo bomVo) {
        bomService.add(bomVo);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody BomVo bomVo) {
        bomService.edit(bomVo);
        return R.success();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody BomVo bomVo) {
        bomService.delete(bomVo);
        return R.success();
    }

    /**
     * 获取bom已关联的产品分类
     */
    @PostMapping("/getBomRelationProductClassify")
    public R getBomRelationProductClassify() {
        List<Map<String, Object>> result = bomService.getBomRelationProductClassify();
        return R.success(result);
    }

    /**
     * 获取产品bom版本列表
     */
    @PostMapping("/getVersionList")
    public R getVersionList(@RequestBody Bom bom) {
        Assert.notEmpty(bom.getProductId(), "产品id不能为空");
        List<Bom> list = bomService.list(q -> q.eq(Bom::getProductId, bom.getProductId()));
        return R.success(list);
    }

    /**
     * 更新产品bom版本
     */
    @PostMapping("/updateVersion")
    public R updateVersion(@RequestBody Bom bom) {
        bomService.updateVersion(bom);
        return R.success();
    }

    /**
     * 包装bom列表
     */
    @PostMapping("/packList")
    public R packList(@RequestBody Bom bom) {
        List<Bom> list = bomService.list(q -> q.eq(ObjectUtil.isNotEmpty(bom.getPackName()), Bom::getPackName, bom.getPackName()));
        return R.success(list);
    }

}
