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,String lineStatus,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 like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(lineStatus)){
            sql += " and company_is_line ='"+lineStatus+"' ";
        }
        sql += " order by company_is_line desc,company_sort asc,company_create_time DESC limit "+page+" , "+pagesize+"";
        List<ZjxlCompanyCase> companyCaseList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZjxlCompanyCase.class));
        return companyCaseList;
    }

    public int allCount(String id, String name,String lineStatus){
        String sql = "select count(*) 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 like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(lineStatus)){
            sql += " and company_is_line ='"+lineStatus+"' ";
        }
        Integer allCount = jdbcTemplate.queryForObject(sql, Integer.class);;
        return allCount;
    }

    public List<ZjxlCompanyCase> findAll(){
        String sql = "select * from zjxl_company_case where company_is_line = 1";
        List<ZjxlCompanyCase> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(ZjxlCompanyCase.class));
        return list;
    }

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

    public int updateCompanyCase(ZjxlCompanyCase cc){
        String sql = "update zjxl_company_case SET company_id = '"+cc.getCompanyId()+"'";
        if (StringUtils.isNotEmpty(cc.getCompanyName())){
            sql += " ,  company_name = '"+cc.getCompanyName()+"' ";
        }
        if (StringUtils.isNotEmpty(cc.getCompanyDescribe())){
            sql += " ,  company_describe = '"+cc.getCompanyDescribe()+"' ";
        }
        if (StringUtils.isNotEmpty(cc.getCompanyDefaultImg())){
            sql += " ,  company_default_img = '"+cc.getCompanyDefaultImg()+"' ";
        }
        if (StringUtils.isNotEmpty(cc.getCompanyExchangeImg())){
            sql += " ,  company_exchange_img = '"+cc.getCompanyExchangeImg()+"' ";
        }
        if (StringUtils.isNotEmpty(cc.getCompanyAssociatedCase())){
            sql += " ,  company_associated_case = '"+cc.getCompanyAssociatedCase()+"' ";
        }
        if (StringUtils.isNotEmpty(cc.getCompanyJumpUrl())){
            sql += " ,  company_jump_url = '"+cc.getCompanyJumpUrl()+"' ";
        }
        if (cc.getCompanySort() != null){
            sql += " ,  company_sort = "+cc.getCompanySort()+" ";
        }
        if (cc.getCompanyIsLine() != null){
            sql += " ,  company_is_line = "+cc.getCompanyIsLine()+" ";
        }
        sql += " where company_id = '"+cc.getCompanyId()+"' ";
        int update = jdbcTemplate.update(sql);
        return update;
    }

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

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