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.SangforAz;
import com.bocloud.ims.entity.resource.SangforProject;
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
public class SangforProjectsRepository extends BasicGenericDao<SangforProject, Long> {

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

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

    public List<SangforProject> listByVendor(Long vendorId) {
        String sql = "select a.* from sangfor_project 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(SangforProject.class, sql, params);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update sangfor_project 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 int stCount(List<Param> params, Long vendorId){
        String sql = "select count(1) from sangfor_project a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.build(sql,params,null,"a");
        Map<String,Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql,param).intValue();
    }

    public List<SangforProject> stList(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId) throws BoCloudDBException {
        String sql = "select * from sangfor_project a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(SangforProject.class, sql);
    }

    public List<SangforProject> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId) throws BoCloudDBException {
        String sql = "select a.name,a.uuid from sangfor_project a where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(SangforProject.class, sql);
    }
}
