package cn.stylefeng.guns.zjxl.service;

import cn.stylefeng.guns.zjxl.dao.ZjxlBannerDao;
import cn.stylefeng.guns.zjxl.model.ZjxlBanner;
import cn.stylefeng.guns.zjxl.model.ret.BannerCompanyCaseRet;
import cn.stylefeng.guns.zjxlUtil.BaseService;
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 banner_sort DESC LIMIT "+page+" , "+pagesize+" ";
        List<ZjxlBanner> bannerList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ZjxlBanner.class));
       return bannerList;
    }

    public int addBanner(ZjxlBanner banner){
        banner.setBannerId(getCode());
        bannerDao.save(banner);
        return 1;
    }

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

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

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

}
