package com.sifang.modules.releorder.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.entity.CrmCompletionInfo;
import com.sifang.modules.releorder.entity.CrmReleOrder;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import com.sifang.modules.taskdispatch.util.ItemDispatchUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @title: CrmReleOrderDao
 * @Author Tan
 * @Date: 2024/10/21 9:55
 * @Version 1.0
 */
@Repository
public class CrmReleOrderDao extends BaseDao<CrmReleOrder> {

    public List<Record> getReleOrderList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT rele_code,rele_order_status,certificate_type,putin_amount_total,actual_payment_amount,buckle_amount_total, ")
                .line(" rele_order_date,invoice_status,create_time ")
                .line(" FROM crm_rele_order  ")
                .line(" WHERE 1 =1  ");
        if (StringUtil.checkParamsValid(map.get("releCode"))) {
            builder.like("and rele_code like ?", map.get("releCode"));
        }
        if (StringUtil.checkParamsValid(map.get("invoiceStatus"))) {
            builder.line("and invoice_status=?", map.get("invoiceStatus"));
        }
        builder.line("order by create_time desc");
        builder.limit(page.getPage(), page.getLimit());
        List<Record> records = Db.find(builder.build(), builder.params());
        ItemDispatchUtil.setReviewFlag(records);
        return records;
    }

    public Long countgetReleOrderList(Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT count(1) ")
                .line(" FROM crm_rele_order  ")
                .line(" WHERE 1 =1  ");
        if (StringUtil.checkParamsValid(map.get("releCode"))) {
            builder.like("and rele_code like ?", map.get("releCode"));
        }
        if (StringUtil.checkParamsValid(map.get("invoiceStatus"))) {
            builder.line("and invoice_status=?", map.get("invoiceStatus"));
        }
        builder.line("order by create_time desc");
        return Db.queryLong(builder.build(), builder.params());
    }

    public Record getReleOrderList(String releCode) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT rele_code,rele_order_status,certificate_type,putin_amount_total,actual_payment_amount,buckle_amount_total,rele_order_date,invoice_status, ");
        sql.append(" certificate_number,project_job_name ");
        sql.append(" FROM crm_rele_order ");
        sql.append(" WHERE 1 = 1  ");
        sql.append(" AND rele_code = "+"'"+releCode+"'");
        return Db.findFirst(sql.toString());
    }

    public CrmReleOrder findReleOrderByCode(String releCode) {
        return getByHql(" from CrmReleOrder where rele_code =:p1", new Parameter(releCode));
    }

}