package com.happy.dao.impl;

import com.happy.Model.Admin;
import com.happy.Model.Advertise;
import com.happy.Model.app.News;
import com.happy.dao.AdvertiseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("AdvertiseDao")
public class AdvertiseImplDao implements AdvertiseDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(
            NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    public int insertAdvertise(Advertise advertise){
        String sql = "insert into `advertise`(logo_url,title,image_url,jump_way,jump_direction,jump_point,state,sort_level,create_time,town_id,sub_title) values(:logo_url,:title,:image_url,:jump_way,:jump_direction,:jump_point,:state,:sort_level,:create_time,:town_id,:sub_title) ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("logo_url", advertise.getLogoUrl());
        sps.addValue("title", advertise.getTitle());
        sps.addValue("image_url", advertise.getImageUrl());
        sps.addValue("jump_way", advertise.getJumpWay());
        sps.addValue("jump_direction", advertise.getJumpDirection());
        sps.addValue("jump_point", advertise.getJumpPoint());
        sps.addValue("state", advertise.getState());
        sps.addValue("sort_level", advertise.getSortLevel());
        sps.addValue("create_time", advertise.getCreateTime());
        sps.addValue("town_id", advertise.getTownId());
        sps.addValue("sub_title",advertise.getSubTitle());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    public int updateAdvertise(Advertise advertise){
        String sql = "update `advertise` set logo_url=:logo_url,title=:title,image_url=:image_url,jump_way=:jump_way,jump_direction=:jump_direction,jump_point=:jump_point,state=:state,sort_level=:sort_level,create_time=:create_time,town_id=:town_id,sub_title=:sub_title where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("logo_url", advertise.getLogoUrl());
        sps.addValue("title", advertise.getTitle());
        sps.addValue("image_url", advertise.getImageUrl());
        sps.addValue("jump_way", advertise.getJumpWay());
        sps.addValue("jump_direction", advertise.getJumpDirection());
        sps.addValue("jump_point", advertise.getJumpPoint());
        sps.addValue("state", advertise.getState());
        sps.addValue("sort_level", advertise.getSortLevel());
        sps.addValue("create_time", advertise.getCreateTime());
        sps.addValue("town_id", advertise.getTownId());
        sps.addValue("sub_title",advertise.getSubTitle());
        sps.addValue("id", advertise.getId());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    public int delAdvertise(int id){
        String sql = "delete from `advertise` where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    // 查询已开启的广告
    public List<Advertise> queryOpenAdvertise(){
        String sql = "select * from `advertise` where `state`=1 ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        List<Advertise> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Advertise.class));
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    public Advertise queryById(int id){
        String sql = "select * from `advertise` where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        List<Advertise> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Advertise.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public Advertise queryByTitle(String title){
        String sql = "select * from `advertise` where title=:title ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("title", title);
        List<Advertise> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Advertise.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public List<Advertise> queryAdvertisePage(int page, int rows, String sqlx){
        int start = (page - 1) * rows;// 每页的起始下标
        String sql = "select * from `advertise` where 1=1 "+sqlx+" order by `state` desc, sort_level asc, create_time desc limit :start,:rows ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("start", start);
        sps.addValue("rows", rows);
        List<Advertise> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Advertise.class));
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    public int queryAdvertiseTotal(String sqlx) {
        String sql = "select count(*) from `advertise` where 1=1 "+sqlx;
        MapSqlParameterSource sps = new MapSqlParameterSource();
        return namedParameterJdbcTemplate.queryForInt(sql, sps);
    }

    // 操作广告开启状态
    public int updateState(Integer id, Integer state,String create_time){
        String sql = "update `advertise` set state=:state,create_time=:create_time where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        sps.addValue("state", state);
        sps.addValue("create_time",create_time);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }
}
