package com.caimei.www.service.page.impl;

import com.caimei.www.mapper.SinglePageDao;
import com.caimei.www.pojo.JsonModel;
import com.caimei.www.pojo.page.ImageLink;
import com.caimei.www.pojo.page.PageContent;
import com.caimei.www.pojo.page.PageFloor;
import com.caimei.www.service.page.SinglePageService;
import com.caimei.www.utils.ImageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Description
 *
 * @author : Charles
 * @date : 2020/7/20
 */
@Slf4j
@Service
public class SinglePageServiceImpl implements SinglePageService {

    @Resource
    private SinglePageDao singlePageDao;
    @Value("${caimei.wwwDomain}")
    private String domain;

    /**
     * 自由页面
     *
     * @param id
     */
    @Override
    public PageContent getFreePageById(Integer id) {
        if (id == null) { return null;}
        return singlePageDao.getFreePageById(id);
    }

    /**
     * 二级页面
     *
     * @param id
     */
    @Override
    public PageContent getTopicPageById(Integer id) {
        if (id == null) { return null;}
        return singlePageDao.getTopicPageById(id);
    }

    /**
     * 采美直播
     */
    @Override
    public PageContent getLivePageData() {
        return singlePageDao.getLivePageData();
    }

    /**
     * 二级专题数据
     *
     * @param id
     */
    @Override
    public JsonModel<List<PageFloor>> getTopicDataById(Integer id) {
        if (id == null) { return JsonModel.error("参数异常", null);}
        List<PageFloor> pageFloors = singlePageDao.getFloorByPageId(id);
        if (!CollectionUtils.isEmpty(pageFloors)) {
            pageFloors.forEach(floor -> {
                List<ImageLink> floorData = singlePageDao.getDataByFloorId(floor.getId());
                if (!CollectionUtils.isEmpty(floorData)) {
                    floorData.forEach(data -> {
                        data.setImage(ImageUtil.getImageURL("actType", data.getImage(), 0, domain));
                    });
                }
                floor.setFloorData(floorData);
            });
        }
        return JsonModel.success(pageFloors);
    }

    /**
     * 专题活动列表数据
     *
     * @param pageNum
     * @param pageSize
     */
    @Override
    public JsonModel<PageInfo<ImageLink>> gePromotionsList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ImageLink>  promotionsList = singlePageDao.getPromotionsList();
        if (!CollectionUtils.isEmpty(promotionsList)) {
            long currentTime = System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            promotionsList.forEach(item -> {
                //已结束
                item.setStatus(3);
                String format = "";
                if(null != item.getBeginTime() && null != item.getEndTime() && (item.getEndTime().getTime() > item.getBeginTime().getTime() ) && (item.getBeginTime().getTime() > currentTime)){
                    //即将开始: 活动开始时间小于当前时间
                    item.setStatus(1);
                    format = sdf.format(item.getBeginTime())+"～"+sdf.format(item.getEndTime());
                }
                if(null != item.getBeginTime() && null != item.getEndTime() && (item.getEndTime().getTime() > currentTime ) && (item.getBeginTime().getTime() < currentTime)) {
                    //进行中: 当前时间小于活动开始，小于活动结束时间
                    item.setStatus(2);
                    format = sdf.format(item.getBeginTime())+"～"+sdf.format(item.getEndTime());
                }
                item.setDetail(format);
            });
        }
        PageInfo<ImageLink> pageInfo = new PageInfo(promotionsList);
        return JsonModel.success(pageInfo);
    }

    /**
     * 品牌招商表格下载
     *
     * @param response
     */
    @Override
    public Mono<Void> downloadByWriteWith(ServerHttpResponse response) throws UnsupportedEncodingException {
        ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
        response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode("采美365网国际品牌招商资料填写表.xls", "UTF-8"));
        response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);
        String templateFile = "/mnt/newdatadrive/data/custom/www-export-template/investment_template.xlsx";
        File file = new File(templateFile);
        return zeroCopyResponse.writeWith(file, 0, file.length());
    }

    /**
     * 获取讲师列表
     */
    @Override
    public JsonModel<List<ImageLink>> getLiveTeachers() {
        List<ImageLink> teacherList = singlePageDao.getLiveTeachers();
        return JsonModel.success(teacherList);
    }

}
