package com.bocloud.ims.repository.common;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
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.middleware.Kvs;
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.List;
import java.util.Map;

/**
 * 缓存服务Repository接口实现类
 *
 * @author wei wei
 */
@Repository("kvsRepository")
public class KvsRepository extends BasicGenericDao<Kvs, Long> {

    public KvsRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public boolean recycle(Long id, Long userId) {
        String sql = "update kvs set is_recycle = true,gmt_modify = :gmtModify,mender_id = :menderId,gmt_remove = :gmtRemove where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        params.put("gmtRemove", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean restore(Long id, Long userId) {
        String sql = "update kvs set is_recycle = false,gmt_modify = :gmtModify,mender_id = :menderId,gmt_remove = null where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public List<Kvs> list(int page, int rows, List<Param> params, Map<String, String> sorter, List<Long> projectIds) throws Exception {
        String sql = "select a.*,b.expired_time,c.name category_name from kvs a"
                + " left join service_category c on a.category_id = c.id,object_relation b"
                + " where a.is_deleted = 0 and a.id = b.res_id and b.category = 'KVS'";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(Kvs.class, sql, paramMap);
    }

    public int count(List<Param> params, List<Long> projectIds) throws Exception {
        String sql = "select count(*) from kvs a,object_relation b where a.is_deleted = 0 and a.id = b.res_id and b.category = 'KVS'";
        Map<String, Object> param = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds) ";
            param.put("projectIds", projectIds);
        }
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        return this.countQuery(sql, param).intValue();
    }

    public boolean remove(Long id, Long userId) throws Exception {
        String sql = "update kvs set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        return this.execute(sql, params) > 0;
    }

    public Kvs query(Long id) throws Exception {
        String sql = "select a.*,b.expired_time,c.name category_name from kvs a"
                + " left join service_category c on a.category_id = c.id,object_relation b"
                + " where a.is_deleted = 0 and a.id = b.res_id and b.category = 'KVS' and a.id = :id";
        Map<String, Object> paramMap = MapTools.simpleMap("id", id);
        List<Kvs> list = this.list(Kvs.class, sql, paramMap);
        return list.isEmpty() ? null : list.get(0);
    }

    public boolean updateStatus(Long id, String status, Long userId) throws Exception {
        String sql = "update kvs set status = :status, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        params.put("status", status);
        return this.execute(sql, params) > 0;
    }

    public Kvs get(String uuid) throws Exception {
        String sql = "select a.*,b.expired_time,c.name category_name from kvs a"
                + " left join service_category c on a.category_id = c.id,object_relation b"
                + " where a.is_deleted = 0 and a.id = b.res_id and b.category = 'KVS' and a.uuid = :uuid";
        Map<String, Object> params = MapTools.simpleMap(Common.UUID, uuid);
        List<Kvs> list = this.list(Kvs.class, sql, params);
        return list.isEmpty() ? null : list.get(0);
    }

}
