package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.VendorTaskMsg;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * @ClassName: TaskMsgRepository
 * @Description:
 * @Author lxy
 * @Date 2021/8/26 10:44
 */
@Repository("taskMsgRepository")
public class TaskMsgRepository extends BasicGenericDao<VendorTaskMsg, Long> {
    public TaskMsgRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public void remove(long id) {
        String sql = "delete from vendor_task_msg where id = :id ";
        Map<String, Object> params = new HashMap<>(1, 1);
        params.put("id", id);
        execute(sql, params);
    }

    public List<VendorTaskMsg> listByVendorId(Long vendorId) throws BoCloudDBException {
        String sql = "select * from vendor_task_msg where vendor_id = :vendorId ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(VendorTaskMsg.class, sql, params);
    }

    /**
     * 查询近期的任务
     *
     * @param vendorId 云平台唯一标识
     * @param minQueueTime 查询queue_time大于等于这个minQueueTime的数据
     * @author sxp
     * @since 2023/8/26
     */
    public List<VendorTaskMsg> queryRecentTask(Long vendorId, Date minQueueTime) throws BoCloudDBException {
        String sql = "select * from vendor_task_msg where vendor_id = :vendorId and queue_time >= :minQueueTime ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("minQueueTime", minQueueTime);
        return this.list(VendorTaskMsg.class, sql, params);
    }

    public List<VendorTaskMsg> listRootByVendorId(long vendorId) {
        String sql = "select * from vendor_task_msg where (parent_id = 0 or parent_id is null) and vendor_id = :vendorId ";
        Map<String, Object> params = new HashMap<>(1, 1);
        params.put(Common.VENDORID, vendorId);
        return this.list(VendorTaskMsg.class, sql, params);
    }

    public List<VendorTaskMsg> listByParent(long parentId) {
        String sql = "select * from vendor_task_msg where parent_id = :parentId ";
        Map<String, Object> params = new HashMap<>(1, 1);
        params.put(Common.PARENTID, parentId);
        return this.list(VendorTaskMsg.class, sql, params);
    }

    public void removeByVendor(long vendorId) {
        String sql = "delete from vendor_task_msg where vendor_id = :vendorId ";
        Map<String, Object> params = new HashMap<>(1, 1);
        params.put("vendorId", vendorId);
        execute(sql, params);
    }

    public int count(List<Param> params) {
        String sql = "select count(1) from vendor_task_msg a where 1=1 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        sql = QueryBuilder.build(sql, params, null, "a");
        return this.countQuery(sql, paramMap).intValue();
    }

    public List<VendorTaskMsg> list(Integer page, Integer rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select * from vendor_task_msg a where 1=1 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(VendorTaskMsg.class, sql, paramMap);
    }

    public VendorTaskMsg get(long id) {
        String sql = "select * from vendor_task_msg where id = :id ";
        Map<String, Object> params = new HashMap<>(1, 1);
        params.put("id", id);
        List<VendorTaskMsg> list = this.list(VendorTaskMsg.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public VendorTaskMsg getByKey(String key) {
        String sql = "select * from vendor_task_msg where task_key = :key ";
        Map<String, Object> params = new HashMap<>(1, 1);
        params.put("key", key);
        List<VendorTaskMsg> list = this.list(VendorTaskMsg.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }
}
