package com.fs.crm.controller;


import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.crm.domain.CrmEvent;
import com.fs.crm.param.CrmAddEventParam;
import com.fs.crm.param.CrmDelEventParam;
import com.fs.crm.param.CrmDoEventParam;
import com.fs.crm.param.CrmEventListQueryParam;
import com.fs.crm.service.ICrmEventService;
import com.fs.crm.vo.CrmEventListQueryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.fs.common.constant.Constants.PAGE_SIZE;


@Api("代办事项接口")
@RestController
@RequestMapping(value="/crm/event")
public class CrmEventController extends BaseController {
	@Autowired
	private TokenService tokenService;
	@Autowired
	ICrmEventService eventService;

//	@GetMapping("/getMyCrmDoEventList")
//	@ApiOperation("获取我代办事件列表")
//	public R getMyCrmDoEventList()
//	{
//		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//		CrmEventListQueryParam param=new CrmEventListQueryParam();
//		param.setCompanyId(loginUser.getCompany().getCompanyId());
//		param.setCompanyUserId(loginUser.getUser().getUserId());
//		param.setStatus(0);
//		List<CrmEventListQueryVO> list = eventService.selectCrmEventListQuery(param);
//		PageInfo<CrmEventListQueryVO> listPageInfo=new PageInfo<>(list);
//		return R.ok().put("data",listPageInfo);
//	}
	@PreAuthorize("@ss.hasPermi('crm:event:myList')")
	@GetMapping("/getMyCrmEventList")
	@ApiOperation("获取我的事件列表")
	public R getMyCrmEventList(CrmEventListQueryParam param)
	{
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		PageHelper.startPage(param.getPageNum(), param.getPageSize());
		param.setCompanyId(loginUser.getCompany().getCompanyId());
		param.setCompanyUserId(loginUser.getUser().getUserId());
		List<CrmEventListQueryVO> list = eventService.selectCrmEventListQuery(param);
		PageInfo<CrmEventListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

	@PostMapping("/addCrmEvent")
	@ApiOperation("添加事件")
	public R addCrmEvent(@RequestBody CrmAddEventParam param)
	{
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		CrmEvent event=new CrmEvent();
		BeanUtils.copyProperties(param,event);
		event.setCompanyId(loginUser.getCompany().getCompanyId());
		event.setCompanyUserId(loginUser.getUser().getUserId());
		if(eventService.insertCrmEvent(event)>0){
			return R.ok("操作成功");
		}
		else{
			return R.error("操作失败");
		}
	}

	@PostMapping("/doEvent")
	@ApiOperation("完成事件")
	public R doEvent(@RequestBody CrmDoEventParam param)
	{
		CrmEvent event=eventService.selectCrmEventById(param.getEventId());
		if(event.getStatus()!=0){
			return R.error("此事件已完成");
		}
		event.setStatus(1);
		if(eventService.updateCrmEvent(event)>0){
			return R.ok("操作成功");
		}
		else{
			return R.error("操作失败");
		}
	}

	@PostMapping("/delCrmEvent")
	@ApiOperation("删除事件")
	public R addCrmEvent(@RequestBody CrmDelEventParam param)
	{
		if(eventService.deleteCrmEventById(param.getEventId())>0){
			return R.ok("操作成功");
		}
		else{
			return R.error("操作失败");
		}
	}


}
