package com.hotent.portal.controller;


import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.FieldRelation;
import com.hotent.base.query.PageBean;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
import com.hotent.portal.model.PortalAgendaDay;
import com.hotent.portal.persistence.manager.PortalAgendaDayManager;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;

import dm.jdbc.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 日程管理 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author zhangxw
 * @email zhangxw@jee-soft.cn
 * @since 2021-07-14
 */
@RestController
@Api(tags="日程管理")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
@RequestMapping("/portalAgendaDay/v1/")
public class PortalAgendaDayController extends BaseController<PortalAgendaDayManager, PortalAgendaDay> {
	
	@Resource
	PortalAgendaDayManager portalAgendaDayManager;
	
	@RequestMapping(value="save",method= {RequestMethod.POST,RequestMethod.PUT}, produces = {"application/json; charset=utf-8" })
	@ApiOperation("保存日程")
	public CommonResult<String> save(@ApiParam(name="agendaDay", value="日程信息") @RequestBody PortalAgendaDay agendaDay) {
		try {
			if(StringUtil.isNotEmpty(agendaDay.getId())) {
				if (StringUtil.isEmpty(agendaDay.getTenantId())) {
					agendaDay.setTenantId(ContextUtil.getCurrentUser().getTenantId());
				}
				return super.updateById(agendaDay);
			}else {
				IUser user = ContextUtil.getCurrentUser();
				agendaDay.setCreateBy(user.getUserId());
				agendaDay.setCreator(user.getFullname());
				return super.create(agendaDay);
			}
		} catch (Exception e) {
			return new CommonResult<String>(false, "保存日程失败："+e.getMessage());
		}
	}
	
	@RequestMapping(value="getByUserId",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户所有日程", httpMethod = "GET", notes = "获取用户所有日程")
	public List<PortalAgendaDay> getByUserId(@ApiParam(name = "userId",value = "用户id（不传取当前用户）")@RequestParam(required=false) Optional<String> userId) throws Exception{
		String id = userId.orElse("");
		if(StringUtil.isEmpty(id)) {
			id = ContextUtil.getCurrentUser().getUserId();
		}
		return portalAgendaDayManager.getByUserId(id);
	}
	
	@RequestMapping(value="getAgendaDayByTime",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据条件获取日程", httpMethod = "GET", notes = "根据条件获取日程")
	public List<PortalAgendaDay> getAgendaDay(@ApiParam(name = "userId",value = "用户id（不传取当前用户）")@RequestParam(required=false) Optional<String> userId,
			@ApiParam(name = "startDate",value = "开始日期（如：2021-07-14 00:00:00）")@RequestParam(required=false) Optional<String> startDate,
			@ApiParam(name = "endDate",value = "结束日期（如：2021-07-15 00:00:00）") @RequestParam(required=false) Optional<String> endDate) throws Exception{
		String id = userId.orElse("");
		if(StringUtil.isEmpty(id)) {
			id = ContextUtil.getCurrentUser().getUserId();
		}
		QueryFilter filter = QueryFilter.build().withPage(new PageBean(1, PageBean.WITHOUT_PAGE));
		filter.addFilter("createBy", id, QueryOP.EQUAL, FieldRelation.AND ,"myQuery");
		if(BeanUtils.isNotEmpty(startDate.get()) && BeanUtils.isNotEmpty(endDate.get())) {
			filter.addFilter("endTime", startDate.get(), QueryOP.LESS_EQUAL, FieldRelation.AND ,"myQuery");
			filter.addFilter("startTime", endDate.get(), QueryOP.GREAT_EQUAL, FieldRelation.AND ,"myQuery");
		}else if(BeanUtils.isNotEmpty(startDate.get())) {
			filter.addFilter("endTime", startDate.get(), QueryOP.GREAT_EQUAL, FieldRelation.AND ,"myQuery");
		}else if(BeanUtils.isNotEmpty(endDate.get())) {
			filter.addFilter("endTime", endDate.get(), QueryOP.LESS_EQUAL, FieldRelation.AND ,"myQuery");
		}
		PageList<PortalAgendaDay> page = super.query(filter);
		return page.getRows();
	}
	
}
