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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.warehouse.entity.CrmSupplyBarcode;
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 org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

@Repository
public class CrmSupplyBarcodeDao extends BaseDao<CrmSupplyBarcode> {

    public List<Record> getList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line("SELECT b.id,a.code,a.material_name,a.material_type_id,a.material_specs,a.remarks,a.material_model,a.material_brand")
                .line(",a.unit_name,b.material_barcode,b.barcode_status,b.create_time,mt.name as material_type, a.qq_code,b.batch_Code,b.batch_Code_note ")
                .line("FROM `crm_supply_barcode` b")
                .line("left join `crm_supply_material` a on b.material_id = a.id and a.status='1'")
                .line("left join `crm_supply_material_type` mt on mt.id = a.material_type_id and mt.status='1'")
                .line("WHERE 1=1 ");

        appendCondition(map, sqlBuilder);
        sqlBuilder.line("order by b.create_time desc");
        if (page != null) {
            sqlBuilder.limit(page.getPage(), page.getLimit());
        }
        List<Record> records = Db.find(sqlBuilder.build(), sqlBuilder.params());
        for (Record r : records) {
            if ("1".equals(r.getStr("barcode_status"))) {
                Record record = Db.findFirst("SELECT ce.`power_station_code`,ce.partner_name FROM crm_plan_material_entry cp,crm_customer_entry ce WHERE ce.id = cp.entry_id AND cp.barcode = ?",
                        r.getStr("material_barcode"));
                if (record != null){
                    r.set("power_station_code", record.getStr("power_station_code"));
                    r.set("partner_name", record.getStr("partner_name"));
                }else {
                    r.set("power_station_code", "");
                    r.set("partner_name", "");
                }

            }
        }
        return records;
    }

    public Long countList(Map<String, String> map) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.line(" SELECT count(b.id) ")
                .line("FROM `crm_supply_barcode` b")
                .line("left join `crm_supply_material` a on b.material_id = a.id and a.status='1'")
                .line("left join `crm_supply_material_type` mt on mt.id = a.material_type_id and mt.status='1'")
                .line(" WHERE 1=1 ");

        appendCondition(map, sqlBuilder);
        return Db.queryLong(sqlBuilder.build(), sqlBuilder.params());
    }

    public void appendCondition(Map<String, String> map, SqlBuilder builder) {
        if (StringUtil.checkParamsValid(map.get("materialCode"))) {
            builder.line("and a.code like concat('%', ?, '%')", map.get("materialCode").trim());
        }
        if (StringUtil.checkParamsValid(map.get("materialName"))) {
            builder.line("and a.material_name like concat('%', ?, '%')", map.get("materialName").trim());
        }
        if (StringUtil.checkParamsValid(map.get("materialType"))) {
            builder.line("and a.material_type_id = ?", map.get("materialType").trim());
        }
        if (StringUtil.checkParamsValid(map.get("materialSpecification"))) {
            builder.line("and a.material_specs like concat('%', ?, '%')", map.get("materialSpecification").trim());
        }
        if (StringUtil.checkParamsValid(map.get("materialRemark"))) {
            builder.line("and a.remarks like concat('%', ?, '%')", map.get("materialRemark").trim());
        }
        if (StringUtil.checkParamsValid(map.get("materialBarcode"))) {
            builder.line("and b.material_barcode like concat('%', ?, '%') ", map.get("materialBarcode").trim());
        }
        if (StringUtil.checkParamsValid(map.get("barcodeStatus"))) {
            builder.line("and b.barcode_status = ? ", map.get("barcodeStatus").trim());
        }
        if (StringUtil.checkParamsValid(map.get("qq_code"))) {
            builder.line("and a.qq_code = ? ", map.get("qq_code").trim());
        }
        if (StringUtil.checkParamsValid(map.get("batch_Code"))) {
            builder.line("and b.batch_Code = ? ", map.get("batch_Code").trim());
        }
        if(StringUtil.checkParamsValid(map.get("create_time"))){
            builder.line(" and b.create_time >= ? ",String.valueOf(map.get("create_time")));
        }
        if(StringUtil.checkParamsValid(map.get("create_time_end"))){
            builder.line(" and b.create_time <= ? ",String.valueOf(map.get("create_time_end")));
        }
    }

    /**
     * 查询条码物料信息
     */
    public CrmSupplyBarcode findOneByBarcode(String barcode) {
        return getByHql("from CrmSupplyBarcode where materialBarcode =:p1", new Parameter(barcode));
    }

    /**
     * 物料信息变更选择变更后的条码列表
     */
    public List<Record> findBarcodesByBraCode(String materialCode,String barcode){
        SqlBuilder sb = new SqlBuilder();
        sb.line("select b.id , b.material_id , b.material_barcode ")
                .line("from crm_supply_barcode b")
                .line("left join crm_supply_material m on b.material_id = m.id  ")
                .line("where b.barcode_status = '2' " )
                .line(" and m.code = ? ",materialCode);
        sb.like(" and b.material_barcode like ? ", barcode);
        return Db.find(sb.build(), sb.params());
    }

    public CrmSupplyBarcode findOneById(String id) {
        return getByHql("from CrmSupplyBarcode where id =:p1", new Parameter(id));
    }
}
