package com.bocloud.ims.repository.resource.contentlibrary;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
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.resource.contentlibrary.ContentLibraryItem;
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;
import java.util.stream.Collectors;

/**
 * 内容库模板数据访问
 *
 * @author wangyu
 * @version 5.0.0-SNAPSHOT
 * @since 2021/2/26
 */
@Repository
public class ContentLibraryItemRepository extends BasicGenericDao<ContentLibraryItem, Long> {

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

    public int count(List<Param> params, List<Long> projectIds) throws BoCloudDBException {
        StringBuilder builder = new StringBuilder("select count(a.id) from content_library_item a where a.is_deleted = 0");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            builder.append(" and a.project_id in (:projectIds)");
            paramMap.put("projectIds", projectIds);
        }
        String sql = QueryBuilder.buildRaw(builder.toString(), params, null, "a");
        return this.countQuery(sql, paramMap).intValue();
    }

    public List<ContentLibraryItem> list(int page, int rows, List<Param> params, Map<String, String> sorter, List<Long> projectIds) throws BoCloudDBException {
        StringBuilder builder = new StringBuilder("select a.*,b.name vendor_name,c.name tenant_name,d.name project_name from content_library_item a left join cloud_vendor b on b.id = a.vendor_id left join tenant c on c.id = a.tenant_id left join project d on d.id = a.project_id where a.is_deleted = 0");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            builder.append(" and a.project_id in (:projectIds)");
            paramMap.put("projectIds", projectIds);
        }
        String sql = QueryBuilder.buildRaw(builder.toString(), new Pager(page, rows, params, sorter), "a");
        return this.list(ContentLibraryItem.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter, List<Long> projectIds) throws BoCloudDBException {
        String sql = "select a.id,a.name from content_library_item a where a.is_deleted = 0";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds)";
            paramMap.put("projectIds", projectIds);
        }
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        List<ContentLibraryItem> objects = this.list(ContentLibraryItem.class, sql, paramMap);
        return objects.stream().map(object -> new SimpleBean(object.getId(), object.getName())).collect(Collectors.toList());
    }

    public ContentLibraryItem query(Long id) throws Exception {
        String sql = "select a.*,b.name vendor_name,c.name tenant_name,d.name project_name from content_library_item a left join cloud_vendor b on b.id = a.vendor_id left join tenant c on c.id = a.tenant_id left join project d on d.id = a.project_id where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
        List<ContentLibraryItem> list = this.list(ContentLibraryItem.class, sql, param);
        return list.isEmpty() ? null : list.get(0);
    }

    public List<ContentLibraryItem> listByVendorId(Long vendorId, Long libraryId) throws BoCloudDBException {
        String sql = "select * from content_library_item where is_deleted = 0 and vendor_id = :vendorId and library_id = :libraryId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("libraryId", libraryId);
        return this.list(ContentLibraryItem.class, sql, params);
    }

    public List<ContentLibraryItem> listByLibraryId(Long libraryId) throws BoCloudDBException {
        String sql = "select * from content_library_item where is_deleted = 0 and library_id = :libraryId ";
        Map<String, Object> params = MapTools.simpleMap("libraryId", libraryId);
        return this.list(ContentLibraryItem.class, sql, params);
    }


    public boolean removeByVendorId(Long vendorId, Long managerId) throws BoCloudDBException {
        String sql = "update content_library_item set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("menderId", managerId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean removeByLibraryId(Long libraryId, Long managerId) throws BoCloudDBException {
        String sql = "update content_library_item set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and library_id = :libraryId";
        Map<String, Object> params = MapTools.simpleMap("libraryId", libraryId);
        params.put("menderId", managerId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean remove(Long id, Long managerId) throws BoCloudDBException {
        String sql = "update content_library_item set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        params.put("menderId", managerId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }
}
