package cn.stylefeng.guns.zjxl.service;

import cn.stylefeng.guns.zjxl.dao.ZjxlBannerDao;
import cn.stylefeng.guns.zjxl.model.ZjxlArticleContent;
import cn.stylefeng.guns.zjxl.model.ZjxlBanner;
import cn.stylefeng.guns.zjxl.model.ret.BannerCompanyCaseRet;
import cn.stylefeng.guns.zjxlUtil.BaseService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/***
 * @ClassName: ZjxlBannerService
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/10/30 17:26
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ZjxlBannerService extends BaseService {

    @Autowired
    private ZjxlBannerDao bannerDao;

    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<ZjxlBanner> findById(String id,String name,Integer page,Integer pagesize){
        String sql = "SELECT * FROM `guns-separation`.zjxl_banner WHERE 1=1";
        if (StringUtils.isNotEmpty(id)){
            sql += " AND banner_id = '"+id+"'";
        }
        if (StringUtils.isNotEmpty(name)){
            sql += " AND banner_name LIKE '%"+name+"%'";
        }
        sql += " ORDER BY concat(banner_sort,banner_create_time) DESC LIMIT "+page+" , "+pagesize+" ";
        List<ZjxlBanner> bannerList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ZjxlBanner.class));
       return bannerList;
    }

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

    public String addBanner(ZjxlBanner banner){
        banner.setBannerId(getCode());
        ZjxlBanner bb = bannerDao.save(banner);
        return bb.getBannerId();
    }

    public int deleteBannerById(String id){
        return bannerDao.deleteById(id);
    }

    public int updateIsLine(String id, Integer isLine){
        return bannerDao.updateIsLine(id,isLine);
    }

    public int updateBanner(ZjxlBanner banner){
        String sql = "update zjxl_banner SET banner_id = '"+banner.getBannerId()+"'";
        if (StringUtils.isNotEmpty(banner.getBannerImage())){
            sql += ",banner_image = '"+banner.getBannerImage()+"'";
        }
        if (banner.getBannerType() != null){
            sql += ",banner_type = "+banner.getBannerType()+"";
        }
        if (banner.getBannerSort() != null){
            sql += ",banner_sort = "+banner.getBannerSort()+"";
        }
        if (StringUtils.isNotEmpty(banner.getBannerName())){
            sql += ",banner_name = '"+banner.getBannerName()+"'";
        }
        if (banner.getBannerIsLine() != null){
            sql += ",banner_is_line = "+banner.getBannerIsLine()+"";
        }
        sql += " where banner_id = '"+banner.getBannerId()+"'";
        int update = jdbcTemplate.update(sql);
        return update;
    }

    public List<BannerCompanyCaseRet> finfBannerCompanyCaseById(String id){
        String sql = "SELECT * FROM zjxl_banner b, zjxl_company_case cc WHERE b.banner_id = '"+id+"' AND b.banner_id = cc.test_banner_id";
        List<BannerCompanyCaseRet> caseRet = jdbcTemplate.query(sql, new BeanPropertyRowMapper(BannerCompanyCaseRet.class));
        return caseRet;
    }

    public List<ZjxlBanner> findBannerByIsOnLine(Integer page,Integer pagesize){
        String sql = "SELECT * FROM `guns-separation`.zjxl_banner WHERE banner_is_line=1";
        sql += " ORDER BY concat(banner_sort,banner_create_time) DESC  ";
        List<ZjxlBanner> bannerList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ZjxlBanner.class));
        return bannerList;
    }

    public int getIsOnLineCount(){
        String sql = "select count(*) from zjxl_banner WHERE banner_is_line=1";
        Integer allCount = jdbcTemplate.queryForObject(sql, Integer.class);;
        return allCount;
    }

    public List<ZjxlBanner> findBannerAll(){
        String sql = "SELECT * FROM `guns-separation`.zjxl_banner WHERE banner_is_line=1";
        List<ZjxlBanner> bannerList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ZjxlBanner.class));
        return bannerList;
    }
}
