package com.sifang.modules.supply.warehouse.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.supply.warehouse.entity.CrmSupplyAllocationOrderDetail;
import com.sifang.modules.supply.warehouse.entity.CrmSupplyPurchaseOrderDetail;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CrmSupplyAllocationOrderDetailDao extends BaseDao<CrmSupplyAllocationOrderDetail> {


    public List<Record> getDetailList(String orderId){
        return Db.find("SELECT d.* FROM `crm_supply_allocation_order_detail` d WHERE d.status='1'  AND d.`inventory_id`=? ORDER BY d.`sort` ASC ",orderId);
    }

    public List<CrmSupplyAllocationOrderDetail> getDetail(String orderId){
        String sql = "from CrmSupplyAllocationOrderDetail where inventoryId = :inventoryId and status ='1'";
        Query query = getSession().createQuery(sql);
        query.setParameter("inventoryId", orderId);
        List<CrmSupplyAllocationOrderDetail> list = query.list();
        return list;
    }

    public List<Record> getDetailsByOrderIds(List<String> orderIds) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select d.*")
                .line("from crm_supply_allocation_order_detail d")
                .line("where d.status='1'  ");
        if(CollectionUtil.isNotEmpty(orderIds)){
            builder.inst(" and  d.inventory_id in ",orderIds);
        }
        builder.line("order by d.sort ");
        return Db.find(builder.build(), builder.params());
    }


    public List<Record> getMaterialList2(String orderId,String warehouseId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("SELECT d.*  FROM `crm_supply_allocation_order_detail` d")
                .line("WHERE d.status='1'  AND d.`inventory_id`=? ORDER BY d.`sort` ASC",orderId);
        return Db.find(builder.build(),builder.params());
    }

    public List<Record> findDetailsByOrderId(String orderId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select d.*")
                .line("m.remarks, m.unit_name, m.code, m.material_name, m.material_brand")
                .line("from crm_supply_material_deliver_order_detail d")
                .line("left join crm_supply_material m on d.material_id=m.id")
                .line("where d.main_id=? and d.status='0' ", orderId)
                .line("order by d.sort");
        return Db.find(builder.build(), builder.params());
    }
}
