package cn.stylefeng.guns.zjxl.service;

import cn.stylefeng.guns.zjxl.dao.ZjxlArticleContentDao;
import cn.stylefeng.guns.zjxl.model.ZjxlArticleContent;
import cn.stylefeng.guns.zjxlUtil.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/***
 * @ClassName: ZjxlArticleContentService
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/11/3 17:40
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ZjxlArticleContentService extends BaseService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ZjxlArticleContentDao articleContentDao;

    public List<ZjxlArticleContent> findArticleContentById(String id,String articleContentTitle,Integer systemDictType,Integer page,Integer pagesiz){
        String sql = "select * from zjxl_article_content where 1=1 ";
        if (StringUtils.isNotEmpty(id)){
            sql += " and article_content_id = '"+id+"'";
        }
        if (StringUtils.isNotEmpty(articleContentTitle)){
            sql += " and article_content_title like '%"+articleContentTitle+"%'";
        }
        if (systemDictType != null && systemDictType > 0){
            sql += " and article_content_type = "+systemDictType+"";
        }
        sql += " order by concat(article_content_sort,article_content_create_time) desc limit "+page+" , "+pagesiz+"";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public int allCount(){
        String sql = "select count(*) from zjxl_article_content ";
        Integer allCount = jdbcTemplate.queryForObject(sql, Integer.class);;
        return allCount;
    }

    public String addArticleContent(ZjxlArticleContent articleContent){
        articleContent.setArticleContentId(getCode());
        ZjxlArticleContent content = articleContentDao.save(articleContent);
        return content.getArticleContentId();
    }

    public int deleteArticleContent(String id){
        articleContentDao.deleteByArticleContentId(id);
        return 1;
    }

    public int updateArticleContent(String id, Integer isLine){
        articleContentDao.updateByArticleContentId(id, isLine);
        return 1;
    }

    public List<ZjxlArticleContent> getLeftTabs(String articleContentType){
        String sql = "select * from zjxl_article_content where article_content_type = "+articleContentType+" group by article_content_classify,article_content_subclassify";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public List<ZjxlArticleContent> getArticleContentSubclassify(String articleContentType,String articleContentClassify){
        String sql = "select * from zjxl_article_content where article_content_type = "+articleContentType+" and article_content_classify = '"+articleContentClassify+"' group by article_content_subclassify";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public List<ZjxlArticleContent> getArticleContent(String articleContentType,String articleContentClassify,String articleContentSubclassify){
        String sql = "select * from zjxl_article_content where article_content_type = "+articleContentType+" and article_content_classify = '"+articleContentClassify+"' and article_content_subclassify = '"+articleContentSubclassify+"' order by concat(article_content_sort,article_content_create_time) desc limit 1";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }


    public List<ZjxlArticleContent> findNewsList(String id,String articleContentClassify,String articleContentSubclassify,Integer systemDictType,Integer page,Integer pagesiz){
        String sql = "select * from zjxl_article_content where 1=1 ";
        if (StringUtils.isNotEmpty(id)){
            sql += " and article_content_id = '"+id+"'";
        }
        if (StringUtils.isNotEmpty(articleContentClassify)){
            sql += " and article_content_classify = '"+articleContentClassify+"'";
        }
        if (StringUtils.isNotEmpty(articleContentSubclassify)){
            sql += " and article_content_subclassify = '"+articleContentSubclassify+"'";
        }
        if (systemDictType != null && systemDictType > 0){
            sql += " and article_content_type = "+systemDictType+"";
        }
        sql += " order by article_content_sort,article_content_create_time desc limit "+page+" , "+pagesiz+"";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public int allNewsCount(String articleContentClassify,String articleContentSubclassify){
        String sql = "select count(*) from zjxl_article_content where article_content_type = 3 ";
        if (StringUtils.isNotEmpty(articleContentClassify)){
            sql += " and article_content_classify = '"+articleContentClassify+"'";
        }
        if (StringUtils.isNotEmpty(articleContentSubclassify)){
            sql += " and article_content_subclassify = '"+articleContentSubclassify+"'";
        }
        Integer allCount = jdbcTemplate.queryForObject(sql, Integer.class);;
        return allCount;
    }

    public List<ZjxlArticleContent> findArticleContentById(String id){
        String sql = "select * from zjxl_article_content where article_content_id = '"+id+"'";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public List<ZjxlArticleContent> findArticleAll(){
        String sql = "select * from zjxl_article_content ";
        sql += " order by concat(article_content_sort,article_content_create_time) desc ";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

}
