package com.crunii.micro.service.sysmgr.jdbc;

import cn.hutool.core.util.ObjectUtil;
import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.utils.SqlUtil;
import com.crunii.micro.domain.common.jdbc.BaseJdbcTemplate;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.domain.entity.main.YcqAppConfigUrl;
import com.crunii.micro.service.sysmgr.dto.sysmgr.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author baoxy
 * @date 2023-07-11
 */
@Repository
public class AccessJdbcDao {

    @Resource(name = "mainJT")
    protected BaseJdbcTemplate jt;

    public static final String PAGE_SQL = "select a.app_id,a.type type,( select value_name from tsm_property_value a,tsm_property b WHERE a.del_flag=0 and a.prop_id=b.prop_id AND b.prop_code='yygl-zt' and  value_code=a.type || '' limit 1 )  typeValue,a.is_check_login, a.app_code,a.system_name,a.sponsor,a.logo_url,a.app_name," +
            " a.app_url,a.sort,to_char(a.create_time, 'yyyy-MM-dd hh24:mi:ss') as create_time,a.public_key,a.private_key," +
            " a.must_hex,a.must_auth,a.state,a.notes,b.org_id as sponsor,b.org_name as sponsorName,a.app_type," +
            " (case when a.app_type=1 then '自有应用' when a.app_type=2 then '外部应用' else null end) as appTypeName," +
            " a.is_clear_local_storage,a.subject,c.value_name as subjectName" +
            " from ycq_app_config_url a " +
            " left join tsm_org b on a.sponsor=b.org_id" +
            " left join tsm_property_value c on a.subject=c.value_code" +
            " where 1=1 ";

    public static final String PAGE_COUNT_SQL = "select count(*) from ycq_app_config_url where 1=1 ";

    public PageRsp<AccessPageVo> pageSearch(PageReq<AccessPageDto> req) {
        StringBuilder sql = new StringBuilder(PAGE_SQL);
        StringBuilder countSql = new StringBuilder(PAGE_COUNT_SQL);
        List<Object> param = new ArrayList<>();
        if (StringUtils.isNotBlank(req.getQuery().getSystemName())) {
            sql.append(" and a.system_name like concat('%',?,'%') ");
            countSql.append(" and system_name like concat('%',?,'%') ");
            param.add(req.getQuery().getSystemName());
        }
        if (ObjectUtil.isNotEmpty(req.getQuery().getState())) {
            sql.append(" and a.state = ? ");
            countSql.append(" and state = ? ");
            param.add(req.getQuery().getState());
        }
        sql.append(" order by a.sort asc ");
        int total = jt.queryForInt(countSql.toString(), param.toArray());
        String dbType = jt.getDbType();
        List<AccessPageVo> result;
        if (total > 0) {
            SqlUtil.appendLimit(dbType, param, sql, req);
            result = jt.queryForBeans(AccessPageVo.class, sql.toString(), param.toArray());
        } else {
            result = new ArrayList<>();
        }
        PageRsp<AccessPageVo> rsp = new PageRsp<>();
        rsp.setResult(result);
        rsp.setTotalNum(total);
        rsp.setPageNo(req.getPageNo());
        rsp.setPageSize(req.getPageSize());
        return rsp;
    }

    public int checkRepeat(String field, String data) {
        String sql = "select count(*) from ycq_app_config_url where " + field + " = ? ";
        return jt.queryForInt(sql, data);
    }

    public int checkRepeatUpdate(String field, String data, String appId) {
        String sql = "select count(*) from ycq_app_config_url where " + field + " = ? and app_id != ?";
        return jt.queryForInt(sql, data, appId);
    }

    public YcqAppConfigUrl getState(String appId) {
        String sql = "select state from ycq_app_config_url where app_id = ? ";
        return jt.queryForBean(YcqAppConfigUrl.class, sql, appId);
    }

    public void updateState(String appId, int state, Integer loginId) {
        String sql = "update ycq_app_config_url set state = ?, update_worker = ?, update_time = ? where app_id = ? ";
        jt.update(sql, state, loginId, new Date(), appId);
    }

    public List<AccessPageVo> list(Integer loginId) {
        String sql = "select * from ycq_app_config_url a where a.state = 1 and a.app_id in(select app_id from tsm_role_app tra where tra.role_id in(select role_id from TSM_WORKER_ROLE where login_id = ?))";
        return jt.queryForBeans(AccessPageVo.class, sql, loginId);
    }
}
