package cn.stylefeng.guns.zjxl.service;

import cn.stylefeng.guns.zjxl.dao.ZjxlArticleContentDao;
import cn.stylefeng.guns.zjxl.model.ZjxlArticleContent;
import cn.stylefeng.guns.zjxl.model.ZjxlSystemDict;
import cn.stylefeng.guns.zjxlUtil.BaseService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiParam;
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 org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
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 ZjxlSystemDictService systemDictService;
    @Autowired
    private ZjxlArticleContentDao articleContentDao;

    public List<ZjxlArticleContent> findArticleContentById(String id,String articleContentTitle,Integer systemDictType,Integer page,Integer pagesiz){
        String sql = "select ac.article_content_id,ac.article_content_title,ac.article_content_cover,ac.article_content_abstract,\n" +
                "sd.system_dict_type article_content_type,sd.system_dict_classify article_content_classify,sd.system_dict_subclassify article_content_subclassify,\n" +
                "ac.article_content_sort,ac.article_content_is_line,ac.article_content_create_time,ac.article_content_content,ac.dict_id\n" +
                "from zjxl_article_content ac \n" +
                "LEFT JOIN zjxl_system_dict sd on ac.dict_id = sd.system_dict_id\n" +
                "where 1=1  ";

        if (StringUtils.isNoneBlank(id)){
            sql += " and ac.article_content_id = '"+id+"' ";
        }
        if (StringUtils.isNoneBlank(articleContentTitle)){
            sql += " and ac.article_content_title like '%"+articleContentTitle+"%' ";
        }
        if (systemDictType != null && systemDictType > 0){
            sql += " and sd.system_dict_type = "+systemDictType+" ";
        }
        sql += " order by ac.article_content_sort asc,ac.article_content_create_time desc limit "+page+" , "+pagesiz+"";
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public int allCount(String id,String articleContentTitle,Integer systemDictType){
        String sql = "select count(*) from zjxl_article_content ac LEFT JOIN zjxl_system_dict sd on ac.dict_id = sd.system_dict_id\n" +
                "where 1=1 ";
        if (StringUtils.isNoneBlank(id)){
            sql += " and ac.article_content_id = '"+id+"' ";
        }
        if (StringUtils.isNoneBlank(articleContentTitle)){
            sql += " and ac.article_content_title like '%"+articleContentTitle+"%' ";
        }
        if (systemDictType != null && systemDictType > 0){
            sql += " and sd.system_dict_type = "+systemDictType+" ";
        }
        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<ZjxlSystemDict> getLeftTabs(String articleContentType){
        String sql = "select * from zjxl_system_dict where system_dict_type = "+articleContentType+" and system_dict_is_line='1' group by system_dict_classify order by system_dict_sort asc,system_dict_create_time desc ";
        List<ZjxlSystemDict> zjxlSystemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return zjxlSystemDictList;
    }

    public List<ZjxlSystemDict> getArticleContentSubclassify(String articleContentType,String articleContentClassify){
        String sql = "select * from zjxl_system_dict where system_dict_type = "+articleContentType+" and system_dict_classify = '"+articleContentClassify+"' and system_dict_is_line='1'  group by system_dict_subclassify order by system_dict_sort asc,system_dict_create_time desc ";
        List<ZjxlSystemDict> zjxlsystemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return zjxlsystemDictList;
        //SELECT * FROM zjxl_system_dict WHERE system_dict_type = 4 AND system_dict_classify = '公司介绍分类4'
    }

    public List<ZjxlArticleContent> getArticleContent(String articleContentType,String articleContentClassify,String articleContentSubclassify){
        StringBuffer sql = new  StringBuffer("select ac.article_content_id,ac.article_content_title,ac.article_content_cover,ac.article_content_abstract,\n" +
                "sd.system_dict_type article_content_type,sd.system_dict_classify article_content_classify,sd.system_dict_subclassify article_content_subclassify,\n" +
                "ac.article_content_sort,ac.article_content_is_line,ac.article_content_create_time,ac.article_content_content,ac.dict_id\n" +
                "from zjxl_article_content ac \n" +
                "INNER  JOIN zjxl_system_dict sd on ac.dict_id = sd.system_dict_id\n" +
                "where 1=1  ");
        if (StringUtils.isNoneBlank(articleContentType)){
            sql.append("and sd.system_dict_type='"+articleContentType+"' ");
        }
        if (StringUtils.isNoneBlank(articleContentClassify)){
            sql.append("and sd.system_dict_classify ='"+articleContentClassify+"' ");
        }
        if (StringUtils.isNoneBlank(articleContentSubclassify)){
            sql.append("and sd.system_dict_subclassify='"+articleContentSubclassify+"' ");
        }
        sql.append("order by ac.article_content_sort asc ,ac.article_content_create_time desc limit 1");

        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }


    public List<ZjxlArticleContent> findNewsList(String id,String articleContentClassify,String articleContentSubclassify,Integer systemDictType,Integer page,Integer pagesiz){
        StringBuffer sql =new StringBuffer("select ac.article_content_id,ac.article_content_title,ac.article_content_cover,ac.article_content_abstract,\n" +
                "sd.system_dict_type article_content_type,sd.system_dict_classify article_content_classify,sd.system_dict_subclassify article_content_subclassify,\n" +
                "ac.article_content_sort,ac.article_content_is_line,ac.article_content_create_time,ac.article_content_content,ac.dict_id\n" +
                "from zjxl_article_content ac \n" +
                "INNER JOIN zjxl_system_dict sd on ac.dict_id = sd.system_dict_id\n" +
                "where 1=1 and ac.article_content_is_line = 1 and sd.system_dict_is_line =1 ") ;
        if (StringUtils.isNoneBlank(id)){
            sql.append(" and ac.article_content_id = '"+id+"' ") ;
        }
        if (StringUtils.isNoneBlank(articleContentClassify)){
            sql.append(" and sd.system_dict_classify = '"+articleContentClassify+"' ");
        }
        if (StringUtils.isNoneBlank(articleContentSubclassify)){
            sql.append(" and sd.system_dict_subclassify = '"+articleContentSubclassify+"' ");
        }
        if (systemDictType != null && systemDictType > 0){
            sql.append(" and sd.system_dict_type = "+systemDictType+" ");
        }
        sql.append("ORDER BY ac.article_content_sort asc,ac.article_content_create_time desc limit "+page+" , "+pagesiz+" ");
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public int allNewsCount(String id,String articleContentClassify,String articleContentSubclassify){
        StringBuffer sql = new StringBuffer("\n" +
                "select count(1)\n" +
                "from zjxl_article_content ac \n" +
                "INNER JOIN zjxl_system_dict sd on ac.dict_id = sd.system_dict_id\n" +
                "where 1=1 and ac.article_content_is_line = 1 and sd.system_dict_is_line =1 and sd.system_dict_type = '3' ");
        if (StringUtils.isNoneBlank(id)){
            sql.append(" and ac.article_content_id = '"+id+"' ") ;
        }
        if (StringUtils.isNoneBlank(articleContentClassify)){
            sql.append(" and sd.system_dict_classify = '"+articleContentClassify+"' ");
        }
        if (StringUtils.isNoneBlank(articleContentSubclassify)){
            sql.append(" and sd.system_dict_subclassify = '"+articleContentSubclassify+"' ");
        }
        Integer allCount = jdbcTemplate.queryForObject(sql.toString(), 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 articleContentType){
        StringBuffer sql = new StringBuffer("select ac.article_content_id,ac.article_content_title,ac.article_content_cover,ac.article_content_abstract,\n" +
                "sd.system_dict_type article_content_type,sd.system_dict_classify article_content_classify,sd.system_dict_subclassify article_content_subclassify,\n" +
                "ac.article_content_sort,ac.article_content_is_line,ac.article_content_create_time,ac.article_content_content,ac.dict_id\n" +
                "from zjxl_article_content ac \n" +
                "LEFT JOIN zjxl_system_dict sd on ac.dict_id = sd.system_dict_id\n" +
                "where 1=1 and ac.article_content_is_line = 1 and sd.system_dict_is_line =1 ");
        if (StringUtils.isNotEmpty(articleContentType)){
            sql.append("and sd.system_dict_type ='"+articleContentType+"' ");
        }
        sql.append("ORDER BY ac.article_content_sort asc,ac.article_content_create_time,sd.system_dict_subclassify desc ");
        List<ZjxlArticleContent> articleContentList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ZjxlArticleContent.class));
        return articleContentList;
    }

    public JSONObject updateArticleAllInfo(ZjxlArticleContent zjxlArticleContent){
        ZjxlArticleContent tmp = articleContentDao.findById(zjxlArticleContent.getArticleContentId());
        JSONObject result = new JSONObject();
        if (tmp==null){
            result.put("status",-1);
        }
        else{
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            zjxlArticleContent.setArticleContentCreateTime(df.format(new Date()));
            articleContentDao.save(zjxlArticleContent);
            result.put("status",1);
        }
        result.put("articleInfo", JSON.toJSONString(zjxlArticleContent));
        return result;
    }

    public String updateArticleDictId(){

       List<ZjxlArticleContent> list = articleContentDao.findAll();
       list.forEach(article->{
           String  dictId = systemDictService.findSystemDictByCondition(article.getArticleContentType(),article.getArticleContentClassify(),article.getArticleContentSubclassify());
           article.setDictId(StringUtils.isBlank(dictId)?"":dictId);
           articleContentDao.save(article);
       });
       return null;
    }

}
