package com.happy.dao.impl;

import com.happy.Model.BookingLog;
import com.happy.Model.weixin.Users;
import com.happy.Until.Func;
import com.happy.Until.UUIDUtil;
import com.happy.dao.BookingLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("BookingLogDao")
public class BookingLogDaoImpl implements BookingLogDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(
            NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    public int insert(BookingLog bookingLog){
        String sql = "insert into `booking_log`(booking_id,operate_id,operate_name,operate_time,operate,remark,booking_status,create_time) " +
                "values(:booking_id,:operate_id,:operate_name,:operate_time,:operate,:remark,:booking_status,:create_time) ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("booking_id", bookingLog.getBookingId());
        sps.addValue("operate_id", bookingLog.getOperateId());
        sps.addValue("operate_name", bookingLog.getOperateName());
        sps.addValue("operate_time", bookingLog.getOperateTime());
        sps.addValue("operate", bookingLog.getOperate());
        sps.addValue("remark", bookingLog.getRemark());
        sps.addValue("booking_status", bookingLog.getBookingStatus());
        sps.addValue("create_time", UUIDUtil.getNewDate());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    // 修改信息
    public int update(BookingLog bookingLog){
        String sql = "update `booking_log` set booking_id=:booking_id,operate_id=:operate_id,operate_name=:operate_name,operate_time=:operate_time, operate=:operate," +
                "operate=:operate ,remark=:remark ,booking_status=:booking_status ,create_time=:create_time   where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("booking_id", bookingLog.getBookingId());
        sps.addValue("operate_id", bookingLog.getOperateId());
        sps.addValue("operate_name", bookingLog.getOperateName());
        sps.addValue("operate_time", bookingLog.getOperateTime());
        sps.addValue("operate", bookingLog.getOperate());
        sps.addValue("remark", bookingLog.getRemark());
        sps.addValue("booking_status", bookingLog.getBookingStatus());
        sps.addValue("create_time", UUIDUtil.getNewDate());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int updateInfoByParam(BookingLog BookingLog) {
        return 0;
    }

    // 修改信息
    public int updateByParam(BookingLog bookingLog){
        StringBuffer stringBuffer =  new StringBuffer(" update `booking_log` set  ");
        MapSqlParameterSource msp = new MapSqlParameterSource();
        // 将要修改的数据填充到查询语句中
        appendValue(bookingLog,stringBuffer,msp);

        stringBuffer.append("  where id=:id  ");
        msp.addValue("id", bookingLog.getId());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(stringBuffer.toString(), msp);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    public void appendValue(BookingLog info, StringBuffer stringBuffer, MapSqlParameterSource msp)
    {
        if (!Func.checkNull(info.getBookingId()))
        {
            stringBuffer.append(" booking_id=:booking_id, ");
            msp.addValue("booking_id", info.getBookingId());
        }
        if (!Func.checkNull(info.getOperateId()))
        {
            stringBuffer.append(" operate_id=:operate_id, ");
            msp.addValue("operate_id", info.getOperateId());
        }
        if (!Func.checkNull(info.getOperateName()))
        {
            stringBuffer.append(" operate_name=:operate_name ,");
            msp.addValue("operate_name", info.getOperateName());
        }
        if (info.getOperateTime() != null)
        {
            stringBuffer.append(" operate_time=:operate_time ,");
            msp.addValue("operate_time", info.getOperateTime());
        }
        if (!Func.checkNull(info.getOperate()))
        {
            stringBuffer.append(" operate=:operate ,");
            msp.addValue("operate", info.getOperate());
        }
        if (!Func.checkNull(info.getRemark()))
        {
            stringBuffer.append(" remark=:remark ,");
            msp.addValue("remark", info.getRemark());
        }
        if (!Func.checkNull(info.getBookingStatus()))
        {
            stringBuffer.append(" booking_status=:booking_status ,");
            msp.addValue("booking_status", info.getBookingStatus());
        }
        if (info.getCreateTime() != null)
        {
            stringBuffer.append(" create_time=:create_time ,");
            msp.addValue("create_time", info.getCreateTime());
        }


        stringBuffer.append(" remark=''");
    }


    public int del(int id){
        String sql = "delete from `booking_log` where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }


    @Override
    public List<BookingLog> queryPage(String sqlx, int page, int rows) {
        int start = (page - 1) * rows;// 每页的起始下标
        String sql = "select * from `booking_log` where 1=1 "+sqlx+" order by id desc limit :start,:rows ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("start", start);
        sps.addValue("rows", rows);
        List<BookingLog> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(BookingLog.class));
        if (list != null && list.size() > 0) {
            return list;
        }
        return new ArrayList<>();
    }

    public List<BookingLog> queryByBookingId(String booking_id){
        String sql = "select * from `booking_log` where booking_id=:booking_id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("booking_id", booking_id);
        List<BookingLog> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(BookingLog.class));
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }


    public BookingLog queryById(String id){
        String sql = "select * from booking_log where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        List<BookingLog> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(BookingLog.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }




}
