package com.happy.dao.impl;

import com.happy.Model.*;
import com.happy.Until.SqlUtil;
import com.happy.Until.UUIDUtil;
import com.happy.dao.BookingComplainDao;
import com.happy.dto.IPage;
import com.happy.vo.BookingComplaintVo;
import com.happy.vo.ComplaintPageVo;
import com.happy.vo.DetailsVo;
import com.happy.vo.ProgressDetailsVo;
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.List;

@Repository("BookingComplainDao")
public class BookingComplainImplDao implements BookingComplainDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

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

    private String selectSql = "select " +
            "u.user_phone as complainPhone,m.corpn_phone as hotelPhone,a.id,a.remark,a.booking_id,a.hotel_id,a.house_id,a.title,a.content,a.create_id,a.create_date,a.modify_date,a.status,h.h_name as house_name,h2.hname as hotel_name" +
            " from booking_complaint a " +
            " left join house h on a.house_id = h.id " +
            " left join hotel h2 on a.hotel_id = h2.id "+
            " left join admin_manager m on h2.manager_id = m.id " +
            " left join users u on a.create_id = u.id " +
            " where a.status != 0 ";

    private String selectLeftSql = "select " +
            " bk.order_num,u.user_phone as complainPhone,m.corpn_phone as hotelPhone,a.id,a.remark,a.booking_id,a.hotel_id,a.house_id,a.title,a.content,a.create_id,a.create_date,a.modify_date,/*a.status,*/h.h_name as house_name,h2.hname as hotel_name,(SELECT bcp.progress_type FROM booking_complaint_progress bcp WHERE bcp.complaint_id = a.id ORDER BY bcp.create_date DESC LIMIT 1 ) AS status" +
            " from booking_complaint a " +
            " left join house h on a.house_id = h.id " +
            " left join hotel h2 on a.hotel_id = h2.id "+
            " left join booking bk on a.booking_id = bk.id"+
            " left join admin_manager m on h2.manager_id = m.id " +
            " left join users u on a.create_id = u.id " +
            " where a.status != 0 ";


    private String selectLeftSql1 = "select * from (select " +
            " bk.order_num,u.user_phone as complainPhone,m.corpn_phone as hotelPhone,a.id,a.remark,a.booking_id,a.hotel_id,a.house_id,a.title,a.content,a.create_id,a.create_date,a.modify_date,/*a.status,*/h.h_name as house_name,h2.hname as hotel_name,(SELECT bcp.progress_type FROM booking_complaint_progress bcp WHERE bcp.complaint_id = a.id ORDER BY bcp.create_date DESC LIMIT 1 ) AS status" +
            " from booking_complaint a " +
            " left join house h on a.house_id = h.id " +
            " left join hotel h2 on a.hotel_id = h2.id "+
            " left join booking bk on a.booking_id = bk.id"+
            " left join admin_manager m on h2.manager_id = m.id " +
            " left join users u on a.create_id = u.id ) t" +
            " where 1=1 ";

    private String detailSql = "select " +
            "u.user_phone as complainPhone,m.corpn_phone as hotelPhone,a.id,a.remark,a.booking_id,a.hotel_id,a.house_id,a.title,a.content,a.create_id,a.create_date,a.modify_date,h.h_name as house_name,h2.hname as hotel_name,(SELECT bcp.progress_type FROM booking_complaint_progress bcp WHERE bcp.complaint_id = a.id ORDER BY bcp.create_date DESC LIMIT 1 ) AS status" +
            " from booking_complaint a " +
            " left join house h on a.house_id = h.id " +
            " left join hotel h2 on a.hotel_id = h2.id "+
            " left join admin_manager m on h2.manager_id = m.id " +
            " left join users u on a.create_id = u.id " +
            " where a.status != 0 ";

    @Override
    public List<BookingComplaintVo> queryPage(String sqlx, int page, int rows) {
        SqlUtil.filterKeyword(sqlx);
        int start = (page - 1) * rows;// 每页的起始下标
        String sql = selectLeftSql1 + sqlx +" ORDER BY t.create_date DESC limit :start,:rows ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("start", start);
        sps.addValue("rows", rows);
        List<BookingComplaintVo> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(BookingComplaintVo.class));
        if (list != null && list.size() > 0) return list;
        return null;
    }

    @Override
    public List<BookingComplaint> toComplainExcel(String sqlx) {
        SqlUtil.filterKeyword(sqlx);
        String sql = selectSql + sqlx + " and a.status != 0"  +" ORDER BY id DESC ";
        List<BookingComplaint> list = namedParameterJdbcTemplate.query(sql,
                new BeanPropertyRowMapper<>(BookingComplaint.class));
        if (list != null && list.size() > 0) return list;
        return null;
    }

    @Override
    public int queryTotal(String sqlx) {
        SqlUtil.filterKeyword(sqlx);
        String sql = "SELECT count(*) FROM ("+ selectLeftSql1 +") a where 1=1 "+sqlx;
        MapSqlParameterSource sps = new MapSqlParameterSource();
        return namedParameterJdbcTemplate.queryForInt(sql, sps);
    }

    @Override
    public BookingComplaint getById(String id) {
        String sql = selectSql + " and a.id = :id";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        List<BookingComplaint> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(BookingComplaint.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public BookingComplaint getDetailById(String id) {
        String sql = detailSql + " and a.id = :id";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        List<BookingComplaint> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(BookingComplaint.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public String insert(String bookingId, String hotelId, String houseId, String title, String content, String createId, String createDate, String modifyDate) {
        String sql = "insert into `booking_complaint` (id,booking_id,hotel_id,house_id,title,content,create_id,create_date,modify_date,status)" +
                " values (:id,:booking_id,:hotel_id,:house_id,:title,:content,:create_id,:create_date,:modify_date,:status)";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("booking_id",bookingId);
        sps.addValue("hotel_id",hotelId);
        sps.addValue("house_id",houseId);
        sps.addValue("title",title);
        sps.addValue("content",content);
        sps.addValue("create_id",createId);
        sps.addValue("create_date",createDate);
        sps.addValue("modify_date",modifyDate);
        sps.addValue("status",1);
        String id = UUIDUtil.getNewDate();
        id = id.replaceAll(" ","");
        id = id.replaceAll("-","");
        id = id.replaceAll(":","");
        id="TS"+id.substring(2,id.length());
        sps.addValue("id", id);

        int num = 0;
        try {
            num = namedParameterJdbcTemplate.update(sql, sps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (num > 0) {
            return id;
        } else {
            id = num + "";
        }
        return id;

    }

    @Override
    public List<ComplaintPageVo> complaintPage(String usersId, int page, int rows) {
        int start = (page - 1) * rows;// 每页的起始下标
        String sql ="select * from (SELECT\n" +
                "\tbc.id as id ,\n" +
                "\tbc.title as title,\n" +
                "\tbc.create_date as dateTime,\n" +
                "\tb.hotel_name as hotelName,\n" +
                "\tb.order_status as orderStatus,\n" +
                "\tb.order_start_time as orderStartTime,\n" +
                "\tb.order_end_time as orderEndTime,\n" +
                "\tb.pay_account as payAccount,\n" +
                "\tb.house_order_number as houseOrderNumber,\n" +
                "\tb.house_name as houseName, fi2.url as url, \n" +
                "\t( SELECT bcp.progress_type FROM booking_complaint_progress bcp WHERE bcp.complaint_id = bc.id ORDER BY bcp.create_date DESC LIMIT 1 ) AS progressType \n" +
                "FROM\n" +
                "\t`booking_complaint` bc\n" +
                "\tLEFT JOIN booking b ON b.id = bc.booking_id AND b.status_del=1   LEFT JOIN ( SELECT fi.link_id, GROUP_CONCAT( fi.url ) AS url FROM file_info fi GROUP BY fi.link_id ) fi2 ON fi2.link_id = bc.house_id  where bc.create_id=:usersId  )bc2 ORDER BY bc2.progressType asc ,bc2.dateTime DESC limit :start,:rows";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("start", start);
        sps.addValue("rows", rows);
        sps.addValue("usersId", usersId);
        List<ComplaintPageVo> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(ComplaintPageVo.class));
        if (list != null && list.size() > 0) return list;
        return null;
    }

    @Override
    public int complaintPageTotal(String usersId) {
        String sql ="SELECT\n" +
                "\t count(1) \n" +
                "FROM\n" +
                "\t`booking_complaint` bc\n" +
                "\tLEFT JOIN booking b ON b.id = bc.booking_id AND b.status_del=1  LEFT JOIN ( SELECT fi.link_id, GROUP_CONCAT( fi.url ) AS url FROM file_info fi GROUP BY fi.link_id ) fi2 ON fi2.link_id = bc.house_id  where bc.create_id=:usersId ";

        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("usersId", usersId);
        return namedParameterJdbcTemplate.queryForInt(sql, sps);
    }

    @Override
    public ProgressDetailsVo progressDetails(String complaintId) {
        String sql="SELECT\n" +
                "\tb.order_num as bookingId,\n" +
                "\tbc.id as complaintId,\n" +
                "\tbc.title,\n" +
                "\tbc.content,\n" +
                "\tbc.create_date as dateTime\n" +
                "FROM\n" +
                "\t`booking_complaint` bc LEFT JOIN booking b on b.id=bc.booking_id and b.status_del=1  \n" +
                " where bc.id=:complaintId and bc.`status`=1";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("complaintId", complaintId);
        List<ProgressDetailsVo> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(ProgressDetailsVo.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }

    /**
     * 投诉订单详情
     * @param complaintId 投诉订单ID
     * @return
     */
    @Override
    public ProgressDetailsVo progressNewDetails(String complaintId) {
        String sql="SELECT\n" +
                "\tb.order_num as bookingId,\n" +
                "\tbc.id as complaintId,\n" +
                "\tbc.title,\n" +
                "\tbc.content,\n" +
                "\tbc.create_date as dateTime\n" +
                "FROM\n" +
                "\t`booking_complaint` bc " +
                "LEFT JOIN booking b on b.id=bc.booking_id and b.status_del=1 \n" +
                " where bc.id=:complaintId";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("complaintId", complaintId);
        List<ProgressDetailsVo> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(ProgressDetailsVo.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }


    @Override
    public List<DetailsVo> detailsVoList(String complaintId) {
        String sql="SELECT\n" +
                "\thcp.progress_type as progressType,\n" +
                "\thcp.create_date as dateTime \n" +
                "FROM\n" +
                "\t`booking_complaint_progress` hcp \n" +
                "WHERE\n" +
                "\thcp.complaint_id= :complaintId and hcp.status=1 ORDER BY hcp.create_date DESC ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("complaintId", complaintId);
        List<DetailsVo> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(DetailsVo.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list;
        return null;
    }


    @Override
    public List<DetailsVo> detailsNewVoList(String complaintId) {
        String sql="SELECT\n" +
                "\thcp.progress_type as progressType,\n" +
                "\thcp.create_date as dateTime \n" +
                "FROM\n" +
                "\t`booking_complaint_progress` hcp \n" +
                "WHERE\n" +
                "\thcp.complaint_id= :complaintId and hcp.status=1 ORDER BY hcp.create_date DESC ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("complaintId", complaintId);
        List<DetailsVo> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(DetailsVo.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list;
        return null;
    }

    @Override
    public DetailsVo detailsVo(String complaintId) {
        String sql="SELECT\n" +
                "\tbc.title AS title,\n" +
                "\tbc.content AS content,\n" +
                "\tfi2.url as urlList,\n" +
                "\thcp.create_date as dateTime \n" +
                "FROM\n" +
                "\t`booking_complaint_progress` hcp \n" +
                "LEFT JOIN booking_complaint bc on bc.id=hcp.complaint_id and bc.`status`=1\n" +
                "LEFT JOIN ( SELECT fi.link_id, GROUP_CONCAT( fi.url ) AS url FROM file_info fi GROUP BY fi.link_id ) fi2 ON fi2.link_id = bc.id\n" +
                "WHERE hcp.complaint_id= :complaintId and hcp.status=1  and hcp.progress_type=1 ORDER BY hcp.create_date DESC";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("complaintId", complaintId);
        List<DetailsVo> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(DetailsVo.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public DetailsVo detailsNewVo(String complaintId) {
        String sql="SELECT\n" +
                "\tbc.title AS title,\n" +
                "\tbc.content AS content,\n" +
                "\tfi2.url as urlList,\n" +
                "\thcp.create_date as dateTime \n" +
                "FROM\n" +
                "\t`booking_complaint_progress` hcp \n" +
                "LEFT JOIN booking_complaint bc on bc.id=hcp.complaint_id \n" +
                "LEFT JOIN ( SELECT fi.link_id, GROUP_CONCAT( fi.url ) AS url FROM file_info fi GROUP BY fi.link_id ) fi2 ON fi2.link_id = bc.id\n" +
                "WHERE hcp.complaint_id= :complaintId and hcp.status=1 ORDER BY hcp.create_date asc";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("complaintId", complaintId);
        List<DetailsVo> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(DetailsVo.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public int updateComplain(String id,String status) {

        String sql= " update `booking_complaint` set "+status+" 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;
    }
}
