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

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.stock.entity.CrmSupplyAlarm;
import com.sifang.modules.supply.stock.entity.SupplyMaterial;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

@Repository
public class CrmSupplyAlarmDao extends BaseDao<CrmSupplyAlarm> {
    @Autowired
    private SupplyMaterialDao materialDao;

    public void createAlarm(String materialId, String ascription, String ascriptionId) {
        SupplyMaterial material = materialDao.get(materialId);

        CrmSupplyAlarm alarm = new CrmSupplyAlarm();
        alarm.setType(SourceConstants.ONE);
        alarm.setObjId(materialId);
        alarm.setObjCode(material.getCode());
        alarm.setContent(String.format("物料代码【%s】低于库存预警数，请及时补充库存", material.getCode()));
        alarm.setStatus(SourceConstants.ZERO);
        alarm.setTop(SourceConstants.ZERO);
        alarm.setCreateTime(new Date());
        alarm.setAscription(ascription);
        alarm.setAscriptionId(ascriptionId);
        save(alarm);
    }

    public List<Record> listAlarm(LayuiPage page, Map<String, String> map) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select * from crm_supply_alarm a where 1=1");
        appendQueryCondition(sql, map);
        sql.line(" order by a.create_time desc");
        if (StrKit.notNull(page)) {
            sql.limit(page.getPage(), page.getLimit());
        }
        return Db.find(sql.build(), sql.params());
    }

    public List<Record> listAlarmByascrIds(List<String> list) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select * from crm_supply_alarm a where 1=1");
        sql.inst("and a.ascription_id in ",list);
        sql.line(" order by a.create_time desc");
        return Db.find(sql.build(), sql.params());
    }

    public long countAlarm(Map<String, String> map) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select count(*) from crm_supply_alarm a where 1=1");
        appendQueryCondition(sql, map);
        return Db.queryLong(sql.build(), sql.params());
    }

    public void appendQueryCondition(SqlBuilder sql, Map<String, String> map) {
        if (StringUtil.checkParamsValid(map.get("ascriptionId"))) {
            sql.line(" and a.ascription_id = ?", map.get("ascriptionId"));
        }
        if (StringUtil.checkParamsValid(map.get("status"))) {
            sql.line(" and a.status = ?", map.get("status"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMin"))) {
            sql.line(" and a.create_time >= concat(?, ' 00:00:00')", map.get("createTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            sql.line(" and a.create_time <= concat(?, ' 23:59:59')", map.get("createTimeMax"));
        }
    }
}
