package cn.stylefeng.guns.zjxl.service;

import cn.stylefeng.guns.zjxl.dao.ZjxlCompanyCaseDao;
import cn.stylefeng.guns.zjxl.model.ZjxlCompanyCase;
import cn.stylefeng.guns.zjxlUtil.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/***
 * @ClassName: ZjxlCompanyCaseService
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/11/2 9:10
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ZjxlCompanyCaseService extends BaseService {
    @Autowired
    private ZjxlCompanyCaseDao companyCaseDao;

    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<ZjxlCompanyCase> findById(String id, String name, Integer page, Integer pagesize){
        String sql = "select a.* from zjxl_company_case a where 1=1";
        if (StringUtils.isNotEmpty(id)){
            sql += " and company_id = '"+id+"'";
        }
        if (StringUtils.isNotEmpty(name)){
            sql += " and company_name = '"+name+"'";
        }
        sql += " order by concat(company_sort,company_create_time) desc limit "+page+" , "+pagesize+"";
        List<ZjxlCompanyCase> companyCaseList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlCompanyCase.class));
        return companyCaseList;
    }

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

    public String addCompanyCase(ZjxlCompanyCase companyCase){
        companyCase.setCompanyId(getCode());
        ZjxlCompanyCase companyCase1 = companyCaseDao.save(companyCase);
        return companyCase1.getCompanyId();
    }

    public int deleteCompanyCase(String id){
        companyCaseDao.deleteById(id);
        return 1;
    }

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