package cn.stylefeng.guns.zjxl.service;

import cn.stylefeng.guns.zjxl.dao.ZjxlSystemDictDao;
import cn.stylefeng.guns.zjxl.model.ZjxlArticleContent;
import cn.stylefeng.guns.zjxl.model.ZjxlSystemDict;
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: ZjxlSystemDictService
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/11/4 9:19
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ZjxlSystemDictService extends BaseService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ZjxlSystemDictDao systemDictDao;

    public String addSystemDict(ZjxlSystemDict systemDict){
        systemDict.setSystemDictId(getCode());
        ZjxlSystemDict dict = systemDictDao.save(systemDict);
        return dict.getSystemDictId();
    }

    public int findSystemDictClassify(String systemDictClassify){
        String sql = "select count(1) from zjxl_system_dict where system_dict_classify = '"+systemDictClassify+"' ";
        Integer articleContentList = jdbcTemplate.queryForObject(sql, Integer.class);
        return articleContentList;
    }

    public int findSystemDictSubclassify(String systemDictSubclassify){
        String sql = "select count(1) from zjxl_system_dict where system_dict_subclassify = '"+systemDictSubclassify+"' ";
        Integer articleContentList = jdbcTemplate.queryForObject(sql, Integer.class);
        return articleContentList;
    }


    public List<ZjxlSystemDict> findSystemDictById(String id,Integer systemDictType,Integer page,Integer pagesize){
        String sql = "select * from zjxl_system_dict where 1=1 ";
        if (StringUtils.isNotEmpty(id)){
            sql += " and system_dict_id = '"+id+"'";
        }
        if (systemDictType != null && systemDictType > 0){
            sql += " and system_dict_type = "+systemDictType+"";
        }
        sql += " order by concat(system_dict_sort,system_dict_create_time) desc limit "+page+" , "+pagesize+"";
        List<ZjxlSystemDict> systemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return systemDictList;
    }

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

    public int updateSystemDict(String id, Integer isLine){
        systemDictDao.updateBySystemDictId(id,isLine);
        return 1;
    }

    public int deleteSystemDict(String id){
        systemDictDao.deleteBySystemDictId(id);
        return 1;
    }


    public List<ZjxlSystemDict> findNewsType(Integer systemDictType){
        String sql = "select * from zjxl_system_dict where 1=1 ";
        if (systemDictType != null && systemDictType > 0){
            sql += " and system_dict_type = "+systemDictType+"";
        }
        sql += " group by system_dict_classify ";
        List<ZjxlSystemDict> systemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return systemDictList;
    }

    public List<ZjxlSystemDict> findAllType(){
        String sql = "select * from zjxl_system_dict where system_dict_is_line = 1 group by system_dict_type";
        List<ZjxlSystemDict> systemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return systemDictList;
    }


    public List<ZjxlSystemDict> findNewsTypeA(Integer systemDictType,String articleContentClassify){
        String sql = "select * from zjxl_system_dict where system_dict_type = "+systemDictType+"    AND system_dict_classify = '"+articleContentClassify+"' group by system_dict_subclassify";
        List<ZjxlSystemDict> systemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return systemDictList;
    }
}
