package com.bocloud.ims.repository.resource;

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.ResourceType;
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;

@Repository("resourceTypeRepository")
public class ResourceTypeRepository extends BasicGenericDao<ResourceType, Long> {

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

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

    public int count(List<Param> params) {
        Map<String, Object> param = QueryBuilder.getParam(params);
        String sql = "select count(1) from resource_type r where r.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, params, null, "r");
        return this.countQuery(sql, param).intValue();
    }

    public boolean delete(ResourceType resourceType) {
        return this.delete(ResourceType.class, resourceType.getId());
    }

    public List<ResourceType> listByVendor(Long vendorId) {
        String sql = "select a.* from resource_type a where a.is_deleted = 0";
        Map<String, Object> params = new HashMap<>(8);
        if (null != vendorId) {
            sql += " and a.vendor_id = :vendorId";
            params.put(Common.VENDORID, vendorId);
        }
        return this.list(ResourceType.class, sql, params);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update resource_type set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId 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 ResourceType query(Long id) {
        String sql = "select * from resource_type where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<ResourceType> list = this.list(ResourceType.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public boolean removeByVid(Long vendorId, Long userId) {
        String sql = "update resource_type set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        return this.execute(sql, params) > 0;
    }
}
