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.entity.main.YcqAppConfigUrl;
import com.crunii.micro.domain.entity.main.YcqAppVersion;
import com.crunii.micro.domain.repo.main.YcqAppVersionRepo;
import com.crunii.micro.service.sysmgr.dto.post.PostPageReqVO;
import com.crunii.micro.service.sysmgr.dto.post.PostRespVO;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AccessPageDto;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AccessPageVo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.YcqAppVersionDto;
import com.crunii.micro.service.sysmgr.dto.sysmgr.YcqAppVersionVo;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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

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

    @Resource
    private YcqAppVersionRepo repo;


    public static final String MAX_SQL =  "SELECT CASE " +
            "WHEN client_Type =1 THEN '安卓'  " +
            "ELSE '安卓' END as clientTypeValue ," +
            "CASE " +
            "WHEN publication_type =1 THEN '正式包'  " +
            "ELSE '正式包' END  AS publicationTypeValue," +
            "CASE " +
            "WHEN updates_type =1 THEN '强制升级'  " +
            "WHEN updates_type =2 THEN '强提示升级'  " +
            "WHEN updates_type =3 THEN '不提示升级'  " +
            "ELSE '正式包' END  AS updatesTypeValue, *" +" FROM YCQ_APP_VERSION c where c.CREATED_TIME=(SELECT max(CREATED_TIME) FROM YCQ_APP_VERSION where c.publish_time<= now() and is_available=1 )   ";
    public static final String PAGE_SQL = "SELECT CASE " +
            "WHEN client_Type =1 THEN '安卓'  " +
            "ELSE '安卓' END as clientTypeValue ," +
            "CASE " +
            "WHEN publication_type =1 THEN '正式包'  " +
            "ELSE '正式包' END  AS publicationTypeValue," +
            "CASE " +
            "WHEN updates_type =1 THEN '强制升级'  " +
            "WHEN updates_type =2 THEN '强提示升级'  " +
            "WHEN updates_type =3 THEN '不提示升级'  " +
            "ELSE '正式包' END  AS updatesTypeValue, *" +
            " FROM YCQ_APP_VERSION where 1=1  ";
    public static final String PAGE_COUNT_SQL = "SELECT COUNT(*) FROM YCQ_APP_VERSION where 1=1 ";

    public static final String LIST_SQL = "SELECT CASE " +
            "WHEN client_Type =1 THEN '安卓'  " +
            "ELSE '安卓' END as clientTypeValue ," +
            "CASE " +
            "WHEN publication_type =1 THEN '正式包'  " +
            "ELSE '正式包' END  AS publicationTypeValue," +
            "CASE " +
            "WHEN updates_type =1 THEN '强制升级'  " +
            "WHEN updates_type =2 THEN '强提示升级'  " +
            "WHEN updates_type =3 THEN '不提示升级'  " +
            "ELSE '正式包' END  AS updatesTypeValue, *" +" FROM YCQ_APP_VERSION where publish_time<=now() and is_available=1   ";

    public YcqAppVersionVo getMaxAppVersion() {
        List<Object> params = new ArrayList<>();
        YcqAppVersionVo ycqAppVersionVo = this.jt.queryForBean(YcqAppVersionVo.class, MAX_SQL, params.toArray());
        return ycqAppVersionVo;
    }


    public PageRsp<YcqAppVersionVo> getPageAppVersion(PageReq<YcqAppVersionDto> req) {
        StringBuilder sb = new StringBuilder(PAGE_SQL);
        StringBuilder sbCount = new StringBuilder(PAGE_COUNT_SQL);
        List<Object> params = new ArrayList<Object>();
        int total = this.jt.queryForInt(sbCount.toString(), params.toArray());
        List<YcqAppVersionVo> pubs = null;
        if (StringUtils.hasText(req.getQuery().getVersion())) {
            sb.append(" and version like concat('%',?,'%') ");
            sbCount.append(" and version like concat('%',?,'%') ");
            params.add(req.getQuery().getVersion());
        }
        if (req.getQuery().getIsAvailable()!=null) {
            sb.append(" and is_available= ? ");
            sbCount.append(" and is_available= ? ");
            params.add(req.getQuery().getIsAvailable());
        }
        if (req.getQuery().getClientType()!=null) {
            sb.append(" and client_type= ? ");
            sbCount.append(" and client_type= ? ");
            params.add(req.getQuery().getClientType());
        }
        if (req.getQuery().getUpdatesType()!=null) {
            sb.append(" and updates_type= ? ");
            sbCount.append(" and updates_type= ? ");
            params.add(req.getQuery().getUpdatesType());
        }
        if (req.getQuery().getPublicationType()!=null) {
            sb.append(" and publication_type= ? ");
            sbCount.append(" and publication_type= ? ");
            params.add(req.getQuery().getPublicationType());
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (req.getQuery().getPublishTimeStart()!=null) {
            Date start = req.getQuery().getPublishTimeStart();
            sb.append(" and  to_char(publish_time,'yyyy-mm-dd') >=? ");
            sbCount.append(" and  to_char(publish_time,'yyyy-mm-dd') >=? ");
            params.add(df.format(start));
        }

        if (req.getQuery().getPublishTimeStartEnd()!=null) {
            Date end = req.getQuery().getPublishTimeStartEnd();
            sb.append(" and  to_char(publish_time,'yyyy-mm-dd') <=? ");
            sbCount.append(" and  to_char(publish_time,'yyyy-mm-dd') <=? ");
            params.add(df.format(end));
        }


        if (total > 0) {
            sb.append("  limit ?,? ");
            params.add(req.getMyStart());
            params.add(req.getPageSize());
            pubs = this.jt.queryForBeans(YcqAppVersionVo.class, sb.toString(), params.toArray());
        } else {
            pubs = new ArrayList<YcqAppVersionVo>(0);
        }
        PageRsp<YcqAppVersionVo> page = new PageRsp<YcqAppVersionVo>();
        page.setPageNo(req.getPageNo());
        page.setPageSize(req.getPageSize());
        page.setTotalNum(total);
        page.setResult(pubs);
        // 为手机计算是否由下一页
        page.next();
        return page;
    }


    public List<YcqAppVersionVo> getListAppVersion(YcqAppVersionDto req) {
        StringBuilder sb = new StringBuilder(LIST_SQL);
        List<Object> params = new ArrayList<Object>();
        if (req.getVersion()!=null) {
            sb.append(" and version= ? ");
            params.add(req.getVersion());
        }
        List<YcqAppVersionVo> pubs = new ArrayList<>();
        pubs = this.jt.queryForBeans(YcqAppVersionVo.class, sb.toString(), params.toArray());
        return pubs;
    }
}
