package com.fs.app.controller;


import com.fs.app.annotation.Login;
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.app.utils.JwtUtils;
import com.fs.common.core.domain.R;
import com.fs.crm.domain.CrmEvent;
import com.fs.crm.service.*;
import com.fs.crm.vo.CrmEventListQueryVO;
import com.fs.store.param.FsCustomerStoreOrderListQueryParam;
import com.fs.store.vo.FsCustomerStoreOrderListQueryVO;
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.web.bind.annotation.*;

import java.util.List;

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


@Api("代办事项接口")
@RestController
@RequestMapping(value="/app/crmEvent")
public class CrmEventController extends  AppBaseController {
	@Autowired
    JwtUtils jwtUtils;
	@Autowired
	ICrmEventService eventService;

	@Login
	@GetMapping("/getMyCrmDoEventList")
	@ApiOperation("获取我代办事件列表")
	public R getMyCrmDoEventList()
	{
		CrmEventListQueryParam param=new CrmEventListQueryParam();
		param.setCompanyId(getCompanyId());
		param.setCompanyUserId(Long.parseLong(getUserId()));
		param.setStatus(0);
		List<CrmEventListQueryVO> list = eventService.selectCrmEventListQuery(param);
		PageInfo<CrmEventListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}
	@Login
	@GetMapping("/getMyCrmEventList")
	@ApiOperation("获取我的事件列表")
	public R getMyCrmEventList(CrmEventListQueryParam param)
	{
		PageHelper.startPage(param.getPage(), PAGE_SIZE);
		param.setCompanyId(getCompanyId());
		param.setCompanyUserId(Long.parseLong(getUserId()));
		List<CrmEventListQueryVO> list = eventService.selectCrmEventListQuery(param);
		PageInfo<CrmEventListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

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

	@Login
	@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("操作失败");
		}
	}

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


}
