package com.zzsmart.qomo.kn.cost.manage.mapper;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostBomRelevancy;
import com.zzsmart.qomo.kn.cost.manage.vo.BomTreeVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description: app_scene_cost_bom_relevancy
 * @Author: jeecg-boot
 * @Date: 2024-09-08
 * @Version: V1.0
 */
public interface AppSceneCostBomRelevancyMapper extends BaseMapper<AppSceneCostBomRelevancy> {
    @Select("WITH RECURSIVE BOM_Tree AS (\n" +
            "    SELECT  werks,matnr, idnrk, menge/bmeng as menge,1.000000 as pmenge,menge/bmeng as topmenge, 1 AS depth,cast( ROW_NUMBER() OVER (partition by matnr  ORDER BY posnr ) AS CHAR ) AS layer,'00000000000000000000' as player \n" +
                    "    FROM app_scene_cost_bom_relevancy\n" +
                    "    WHERE matnr = #{parentId} and werks= #{werk}\n" +
                    "    UNION ALL\n" +
                    "    SELECT b.werks,b.matnr, b.idnrk, b.menge/b.bmeng as menge, BOM_Tree.menge as pmenge,BOM_Tree.topmenge*(b.menge/b.bmeng) AS topmenge,BOM_Tree.depth + 1 as depth,concat( BOM_Tree.layer, '.', b.layer ) AS layer,BOM_Tree.layer as player \n" +
                    "    FROM ( SELECT werks,matnr, idnrk, menge, bmeng, cast( ROW_NUMBER() OVER (partition by matnr,werks ORDER BY posnr ) AS CHAR ) AS layer FROM app_scene_cost_bom_relevancy ) b\n" +
                    "    JOIN BOM_Tree ON b.matnr = BOM_Tree.idnrk and b.werks=BOM_Tree.werks \n" +
                    ")\n" +
                    "SELECT werks,matnr, idnrk, menge,pmenge,topmenge, depth,layer,player FROM BOM_Tree")
    List<BomTreeVo> findChildrenByParentId(String parentId,String werk);


    @Select("${selectSql}")
    JSONObject customSelectOne(@Param("selectSql") String selectSql);

    @Select("${SelectBlobModel}")
    String customSelectBlobModelJson(@Param("SelectBlobModel") String SelectBlobModel);


}
