package com.happy.service.impl;

import com.happy.Model.BookingComplaint;
import com.happy.Model.FileInfo;
import com.happy.Model.HotelCoupon;
import com.happy.Model.House;
import com.happy.Until.Enum.PayEnum;
import com.happy.Until.Enum.ProgressEnum;
import com.happy.dao.BookingComplainDao;
import com.happy.dao.BookingLogDao;
import com.happy.dao.FileInfoDao;
import com.happy.dto.IPage;
import com.happy.service.BookingComplaintProgressService;
import com.happy.service.BookingComplaintService;
import com.happy.service.FileService;
import com.happy.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("BookingComplaintService")
public class BookingComplaintImplService implements BookingComplaintService {

    @Resource
    public BookingComplainDao bookingComplainDao;
    @Resource
    public FileInfoDao fileDao;

@Resource
    public FileService fileService;
    @Resource
    public BookingComplaintProgressService bookingComplaintProgressService;

    @Override
    public IPage<BookingComplaintVo> queryPage(String sqlx, String sql, int page, int rows) {
        IPage<BookingComplaintVo> iPage = new IPage();
        List<BookingComplaintVo> houseList =  bookingComplainDao.queryPage(sqlx,page,rows);
        int total = bookingComplainDao.queryTotal(sql);
        iPage.setPageList(houseList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public int queryTotal(String sqlx) {
        return bookingComplainDao.queryTotal(sqlx);
    }

    @Override
    public BookingComplaint getById(String id) {
        BookingComplaint bookingComplaint = bookingComplainDao.getById(id);
        List<FileInfo> fileInfos = fileDao.queryList("and link_id ='" + id + "'");
        bookingComplaint.setFileInfos(fileInfos);
        return bookingComplaint;
    }

    @Override
    public BookingComplaint getDetailById(String id) {
        BookingComplaint bookingComplaint = bookingComplainDao.getDetailById(id);
        List<FileInfo> fileInfos = fileDao.queryList("and link_id ='" + id + "'");
        bookingComplaint.setFileInfos(fileInfos);
        return bookingComplaint;
    }

    @Override
    public List<BookingComplaint> toComplainExcel(String sqlx) {
        return bookingComplainDao.toComplainExcel(sqlx);
    }
	/**
     * 添加投诉
     * @param bookingId
     * @param hotelId
     * @param houseId
     * @param title
     * @param content
     * @param createId
     * @param createDate
     * @param modifyDate
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(String bookingId, String hotelId, String houseId, String title, String content, String createId, String createDate, String modifyDate,List<String> pictureList) {

//        返回主键id
        String complaintId=bookingComplainDao.insert(bookingId,hotelId,houseId,title,content,createId,createDate,modifyDate);
        int i = 1;
//        ͼƬ
        if (pictureList != null && pictureList.size() > 0) {
            List<FileInfo> fileInfoList = new ArrayList<>();
            for (String s : pictureList) {
                String[] split = s.split("/");
                String s1 = split[split.length - 1];
                FileInfo fileInfo = new FileInfo();
                fileInfo.setLinkId(complaintId);
                fileInfo.setName(s1);
                fileInfo.setUrl(s);

                fileInfoList.add(fileInfo);
            }

            i = fileService.batchInsertFile(fileInfoList);
        }

        int cpInsert=bookingComplaintProgressService.insert(complaintId,createId,createDate,modifyDate, "1");

        if (complaintId != null && i > 0 && cpInsert > 0) {
            return i;
        } else {
            return 0;
        }

    }


    /**
     * 投诉进度
     *
     * @param usersId
     * @param page
     * @param rows
     * @return
     */
    @Override
    public IPage<ComplaintPageVo> complaintPage(String usersId, int page, int rows) {
        IPage<ComplaintPageVo> iPage = new IPage();
        List<ComplaintPageVo> vos = bookingComplainDao.complaintPage(usersId, page, rows);
        if (vos != null && vos.size() > 0) {
            for (ComplaintPageVo vo : vos) {
                String valueBykey = vo.getOrderStatus() == null ? "" : PayEnum.getValueBykey(Integer.valueOf(vo.getOrderStatus()));
                vo.setOrderStatus(valueBykey);
                String valueBykey1 = vo.getProgressType() == null ? "" : ProgressEnum.getValueBykey(Integer.valueOf(vo.getProgressType()));
                vo.setProgressType(valueBykey1);

                if (vo.getUrl()!=null) {
                    String url = vo.getUrl();
                    String[] split = url.split(",");
                    vo.setUrl(split[0]);
                }

            }
        }

        int total = bookingComplainDao.complaintPageTotal(usersId);

        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        iPage.setPageList(vos);
        return iPage;


    }


    /**
     * 投诉详情
     *
     * @param complaintId
     * @return
     */
    @Override
    public ProgressDetailsVo progressDetails(String complaintId) {
        ProgressDetailsVo vo = bookingComplainDao.progressDetails(complaintId);

        List<DetailsVo> detailsVoList = bookingComplainDao.detailsVoList(complaintId);
        if (detailsVoList!=null&&detailsVoList.size()>0) {
            for (DetailsVo vo1 : detailsVoList) {
                if (vo1.getProgressType() != null) {
                    String progressType = vo1.getProgressType();
                    String valueBykey = ProgressEnum.getValueBykey(Integer.valueOf(progressType));
                    vo1.setProgressType(valueBykey);
                }
            }
        }

//        获取提交成功的
        DetailsVo detailsVo = bookingComplainDao.detailsVo(complaintId);

        if(detailsVo != null){
            if (detailsVo.getUrlList() != null) {
                List<String> dateTimeList = detailsVo.getUrlList();
                String s = dateTimeList.get(0);
                String[] split = s.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    strings.add(split[i]);
                }
                detailsVo.setUrlList(strings);
            }
            detailsVo.setProgressType("提交成功");

            detailsVoList.add(detailsVo);
        }

        if(vo != null){
            vo.setDetailsVoList(detailsVoList);
        }

        return vo;
    }


    /**
     * 投诉详情
     *
     * @param complaintId 投诉订单ID
     * @return
     */
    @Override
    public ProgressDetailsVo progressNewDetail(String complaintId) {
        ProgressDetailsVo vo = bookingComplainDao.progressNewDetails(complaintId);

        List<DetailsVo> detailsVoList = bookingComplainDao.detailsNewVoList(complaintId);
        if (detailsVoList!=null&&detailsVoList.size()>0) {
            for (DetailsVo vo1 : detailsVoList) {
                if (vo1.getProgressType() != null) {
                    String progressType = vo1.getProgressType();
                    String valueBykey = ProgressEnum.getValueBykey(Integer.valueOf(progressType));
                    vo1.setProgressType(valueBykey);
                }
            }
        }

        //获取提交成功的
        DetailsVo detailsVo = bookingComplainDao.detailsNewVo(complaintId);

        if(detailsVo != null){
            if (detailsVo.getUrlList() != null) {
                List<String> dateTimeList = detailsVo.getUrlList();
                String s = dateTimeList.get(0);
                String[] split = s.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    strings.add(split[i]);
                }
                detailsVo.setUrlList(strings);
            }
            detailsVo.setProgressType("提交成功");

            detailsVoList.add(detailsVo);
        }

        if(vo != null){
            vo.setDetailsVoList(detailsVoList);
        }

        return vo;
    }

    /**
     * 更新投诉状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateComplain(String id,String status){
        int result = bookingComplainDao.updateComplain(id, status);
        return result;
    }
}
