package com.platform.modules.ks.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.Query;
import com.platform.modules.ks.dao.PlmVideoDownloadLogDao;
import com.platform.modules.ks.entity.PlmVideoDownloadLogEntity;
import com.platform.modules.ks.service.PlmVideoDownloadLogService;

@Service("PlmVideoDownloadLogService")
public class PlmVideoDownloadLogServiceImpl extends ServiceImpl<PlmVideoDownloadLogDao, PlmVideoDownloadLogEntity> implements PlmVideoDownloadLogService {
	
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Long createUserId = (Long)params.get("createUserId");

		Page<PlmVideoDownloadLogEntity> page = this.selectPage(
			new Query<PlmVideoDownloadLogEntity>(params).getPage(),
			new EntityWrapper<PlmVideoDownloadLogEntity>()
				.eq(createUserId != null,"create_user", createUserId).orderBy("id", false)
		);
		PageUtils pageUtils = new PageUtils(page);
		
		if(pageUtils!=null && pageUtils.getList()!=null && pageUtils.getList().size()>0){
			List<PlmVideoDownloadLogEntity> list = (List<PlmVideoDownloadLogEntity>) pageUtils.getList();
			List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(PlmVideoDownloadLogEntity bean : list){
            	if(bean!=null && StringUtils.isNotBlank(bean.getRequestJson())){
            		JSONObject json = JSONObject.fromObject(bean.getRequestJson()); 
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("photoId", json.get("photoId"));
					map.put("title", json.get("caption")==null ? null : json.get("caption").toString());
					map.put("viewCount", json.get("viewCount"));
					map.put("time", json.get("timestamp")==null ? null : sdf.format(new Date(new Long(json.get("timestamp").toString()))));
//					map.put("time", json.get("time"));
//					JSONObject locationJson = json.getJSONObject("location");
//					map.put("address", json.get("address"));
					String addres = "";
                	JSONObject locationJson = json.getJSONObject("location");
					if(locationJson!=null && locationJson.get("province")!=null){
						addres = locationJson.get("province")+".";
					}
					if(locationJson!=null && locationJson.get("city")!=null){
						addres += locationJson.get("city")+".";
					}
					if(locationJson!=null && locationJson.get("district")!=null){
						addres += locationJson.get("district")+".";
					}
					map.put("address", addres);
					map.put("photoId", json.get("photoId"));
					map.put("mainMvUrls", json.get("mainMvUrls"));
					map.put("headUrls", json.get("coverUrls"));
					map.put("headUrl", bean.getHeadUrl());
					map.put("mediaUrl", bean.getMediaUrl());
					map.put("downloadDate", bean.getCreateDate());
					mapList.add(map);
                }
            }
            pageUtils.setList(mapList);
		}
		return pageUtils;
	}
}
