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

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.common.jdbc.BaseNPJdbcTemplate;
import com.crunii.micro.service.sysmgr.api.domain.dto.SendMessageTaskQueryDTO;
import com.crunii.micro.service.sysmgr.api.domain.vo.SendMessageTaskPageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

@Repository
public class SendMessageTaskDao {

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

    @Resource(name = "mainNPJT")
    protected BaseNPJdbcTemplate NPJT;
    private static final String BASE_QUERY_SQL = "select " +
            "mst.id, mst.create_time sendTime, mst.DATA_SOURCE,mst.MESSAGE_CLASSIFY,mst.MESSAGE_CONTENT,mst.SCOPE_TYPE,mst.SEND_WAY,mst.MESSAGE_TYPE,ymt.title " +
            "from MESSAGE_SEND_TASK mst left join YCQ_MESSAGE_TEMPLATE ymt on mst.template_id = ymt.template_id where 1=1 ";
    private static final String BASE_COUNT_SQL = "select count(1) c from MESSAGE_SEND_TASK mst left join YCQ_MESSAGE_TEMPLATE ymt on mst.template_id = ymt.template_id where 1=1 ";

    public PageRsp<SendMessageTaskPageVO> pageByCondition(PageReq<SendMessageTaskQueryDTO> req){

        StringBuilder base = new StringBuilder(BASE_QUERY_SQL);
        StringBuilder countStr = new StringBuilder(BASE_COUNT_SQL);
        SendMessageTaskQueryDTO query = req.getQuery();
        List<Object> params = new ArrayList<>();
        if (query != null){
            String keyword = query.getTitle();
            if (StringUtils.isNotBlank(keyword)){
                base.append(" and ymt.title like concat('%',?,'%') ");
                countStr.append(" and ymt.title like concat('%',?,'%') ");
                params.add(keyword);
            }
            Long appId = query.getAppId();
            if (null != appId){
                base.append(" and ymt.app_id = ? ");
                countStr.append(" and ymt.app_id = ? ");
                params.add(appId);
            }
            String classify = query.getClassify();
            if (StringUtils.isNotBlank(classify)){
                base.append(" and mst.MESSAGE_CLASSIFY like concat('%',?,'%') ");
                countStr.append(" and mst.MESSAGE_CLASSIFY like concat('%',?,'%') ");
                params.add(classify);
            }
            Integer sendWay = query.getSendWay();
            if (null != sendWay && sendWay != 0){
                base.append(" and mst.SEND_WAY like concat('%',?,'%') ");
                countStr.append(" and mst.SEND_WAY like concat('%',?,'%') ");
                params.add(sendWay);
            }

            String startTime = query.getStartTime();
            if (StringUtils.isNotBlank(startTime)){
                base.append(" and mst.create_time >= ? ");
                countStr.append(" and mst.create_time >= ? ");
                params.add(startTime);
            }

            String endTime = query.getEndTime();
            if (StringUtils.isNotBlank(endTime)){
                base.append(" and mst.create_time <= ? ");
                countStr.append(" and mst.create_time <= ? ");
                params.add(endTime);
            }

        }
        int total = JT.queryForInt(countStr.toString(), params.toArray());
        List<SendMessageTaskPageVO> values = new ArrayList<>(req.getPageSize());
        String dbType = JT.getDbType();
        if (total > 0) {
            //根据数据库类型设置limit
            SqlUtil.appendLimit(dbType, params, base, req);
            values = JT.queryForBeans(SendMessageTaskPageVO.class, base.toString(), params.toArray());
        }
        PageRsp<SendMessageTaskPageVO> page = new PageRsp<>();
        page.setPageNo(req.getPageNo());
        page.setPageSize(req.getPageSize());
        page.setTotalNum(total);
        page.setResult(values);
        page.next();
        return page;
    }
}
