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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.ir.ReturnNode;
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.text.SimpleDateFormat;
import java.util.Date;
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 String findSystemDictByCondition(Integer systemDictType,String systemDictClassify,String systemDictSubclassify){
        String sql = "select system_dict_id from zjxl_system_dict where  system_dict_type ='"+systemDictType+"' " +
                "and system_dict_classify ='"+systemDictClassify+"' " +
                "and system_dict_subclassify = '"+systemDictSubclassify+"' ";
        String  articleContentList;
        try {
            articleContentList= jdbcTemplate.queryForObject(sql, String.class);
        }catch (Exception e){
            articleContentList ="";
        }
        return articleContentList;
    }

    public int findSystemDictExistByCondition(String id,Integer systemDictType,String systemDictClassify,String systemDictSubclassify){
        StringBuffer sql = new StringBuffer("select count(1) from zjxl_system_dict where 1=1 ");

        if(StringUtils.isNoneBlank(id)){
            sql.append("and system_dict_id <>'"+id+"' ");
        }
        if (systemDictType !=null){
            sql.append("and system_dict_type ='"+systemDictType+"' ");
        }
        if (StringUtils.isNoneBlank(systemDictClassify)){
           sql.append("and system_dict_classify ='"+systemDictClassify+"' ");
        }
        if (StringUtils.isNoneBlank(systemDictSubclassify)){
            sql.append("and system_dict_subclassify ='"+systemDictSubclassify+"' ");
        }
        Integer articleContentList = jdbcTemplate.queryForObject(sql.toString(), Integer.class);
        return articleContentList;
    }

    public int findSystemDictSubclassifyByCondition(String systemDictSubclassify,String dictId){
        String sql = "select count(1) from zjxl_system_dict where system_dict_subclassify = '"+systemDictSubclassify+"' ";
        if (StringUtils.isNoneBlank(dictId)){
            sql +="and system_dict_id <>'"+dictId+"' ";
        }
        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 system_dict_is_line desc,system_dict_sort asc,system_dict_create_time desc limit "+page+" , "+pagesize+"";
        List<ZjxlSystemDict> systemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return systemDictList;
    }

    public int allCount(String systemDictType){
        String sql = "select count(*) from zjxl_system_dict ";
        if (StringUtils.isNoneBlank(systemDictType)){
            sql +="where  system_dict_type ='"+systemDictType+"'";
        }
        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 += " and system_dict_is_line ='1' group by system_dict_classify order by system_dict_sort asc,system_dict_create_time desc ";
        List<ZjxlSystemDict> systemDictList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlSystemDict.class));
        return systemDictList;
    }

    public JSONObject updateSystemDictInfo(String systemDictId,Integer systemDictType,String systemDictClassify,String systemDictSubclassify,Integer systemDictSort,Integer systemDictIsLine){
        JSONObject result = new JSONObject();
        ZjxlSystemDict zjxlSystemDict = systemDictDao.findBySystemDictId(systemDictId);
        if (zjxlSystemDict ==null){
            result.put("status",-1);
            return result;
        }
        //查询子分类是否存在
        int systemIsExist = findSystemDictExistByCondition(systemDictId,systemDictType,systemDictClassify,systemDictSubclassify);
        if (systemIsExist>0){
            result.put("status",0);
            return result;
        }
        else{
            if (StringUtils.isBlank(systemDictClassify)||StringUtils.isBlank(systemDictSubclassify)){
                result.put("status",-2);
                return result;
            }
            else{
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                zjxlSystemDict.setSystemDictCreateTime(df.format(new Date()));
                zjxlSystemDict.setSystemDictType(systemDictType);
                zjxlSystemDict.setSystemDictClassify(systemDictClassify);
                zjxlSystemDict.setSystemDictSubclassify(systemDictSubclassify);
                zjxlSystemDict.setSystemDictSort(systemDictSort);
                zjxlSystemDict.setSystemDictIsLine(systemDictIsLine);
                systemDictDao.save(zjxlSystemDict);
                result.put("status",1);
            }
        }
        result.put("dictInfo", JSON.toJSONString(zjxlSystemDict));
        return result;
    }

    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;
    }
}
