package cn.stylefeng.guns.zjxl.service;

import cn.stylefeng.guns.zjxl.dao.ZjxlPartnerDao;
import cn.stylefeng.guns.zjxl.model.ZjxlPartner;
import cn.stylefeng.guns.zjxlUtil.BaseService;
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.util.List;

/***
 * @ClassName: ZjxlPartnerService
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/11/3 14:58
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ZjxlPartnerService extends BaseService {

    @Autowired
    private ZjxlPartnerDao partnerDao;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<ZjxlPartner> findPartenrById(String id, String name, Integer page, Integer pagesize){
        String sql = "select * from zjxl_partner where 1=1";
        if (StringUtils.isNotEmpty(id)){
            sql += " and partner_id = '"+id+"' ";
        }
        if (StringUtils.isNotEmpty(name)){
            sql += " and partner_name like '%"+name+"%'";
        }
        sql += " order by concat(partner_sort,partner_create_time) desc limit "+page+" , "+pagesize+"";
        List<ZjxlPartner> partnerList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlPartner.class));
        return partnerList;
    }

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

    public String addPartenr(ZjxlPartner partner){
        partner.setPartnerId(getCode());
        ZjxlPartner p = partnerDao.save(partner);
        return p.getPartnerId();
    }

    public int updatePartner(String id, Integer isLine){
        partnerDao.updatePartner(id,isLine);
        return 1;
    }

    public int deletePartner(String id){
        partnerDao.deleteById(id);
        return 1;
    }
}
