package org.linlinjava.jz.hm.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.domain.JzDmTicketResult;
import org.linlinjava.jz.db.domain.JzTicket;
import org.linlinjava.jz.db.service.JzDmTicketResultService;
import org.linlinjava.jz.db.service.JzSkillGroupService;
import org.linlinjava.jz.db.service.JzTicketService;
import org.linlinjava.jz.db.service.hm.HmNoticeService;
import org.linlinjava.jz.db.util.TicketStatusConstant;
import org.linlinjava.jz.db.util.hm.HmCommentEnum;
import org.linlinjava.jz.db.util.hm.HmNoticeEnum;
import org.linlinjava.jz.db.util.hm.HmTicketActionContans;
import org.linlinjava.jz.dto.AddTicketActionDto;
import org.linlinjava.jz.dto.HmNoticeDto;
import org.linlinjava.jz.dto.SkillGroupUserVo;
import org.linlinjava.jz.dto.TicketAction;
import org.linlinjava.jz.hm.util.HmResponseUtil;
import org.linlinjava.jz.service.NoticeService;
import org.linlinjava.jz.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "小程通知信息和工单处理", description = "小程通知信息和工单处理")
@RestController
@RequestMapping("notice")
public class NoticeController {

	@Autowired
	HmNoticeService hmNoticeService;

	@Autowired
	NoticeService noticeService;

	@Autowired
	private TicketService ticketService;

	@Autowired
	private JzSkillGroupService skillGroupService;

	@Autowired
	private JzDmTicketResultService jzDmTicketResultService;

	// 通知获取，保姆获取雇主发的通知，保姆获取管家发的通知

	@ApiOperation(value = "获取用户未读通知", notes = "获取用户未读通知type 1保姆获取雇主发的通知;2保姆获取管家发的通知；3雇主获取保姆发送的通知，4雇主获取管家发送的通知")
	@GetMapping(value = "getUserNotice")
	public Object getUserNotice(Long userId, Integer type) {
		if (null == userId || type == null) {
			return HmResponseUtil.fail("userId和type不能为空");
		}
		return HmResponseUtil.ok(hmNoticeService.getUnReadNotice(userId, type));
	}

	@ApiOperation(value = "读取通知，修改通知未读状态", notes = "读取通知，修改通知未读状态")
	@GetMapping(value = "updateNoticeRead")
	public Object updateNoticeRead(Long noticeId) {
		if (null == noticeId) {
			return HmResponseUtil.fail("noticeId不能为空");
		}
		hmNoticeService.updateNoticeRead(noticeId);
		return HmResponseUtil.ok();
	}

	@ApiOperation(value = "根据关联rId和通知类型rType拿通知id", notes = "根据关联rId和通知类型rType拿通知id(5.保姆有无法完成的任务；6保姆缺勤打卡)")
	@GetMapping(value = "getNoticeByRid")
	public Object getNoticeByRid(String rId, String rType) {
		if (StringUtils.isBlank(rId) || StringUtils.isBlank(rType)) {
			return HmResponseUtil.fail("rId和rType不能为空");
		}
		return HmResponseUtil.ok(hmNoticeService.getNoticeByRidRtype(rId, rType));
	}

	@ApiOperation(value = "跟进类，用户读取某条通知 详细信息  type 1拜访2跟进类", notes = "用户读取某条通知 详细信息,管家，保姆，雇主都用  type 1拜访2跟进类 ")
	@GetMapping(value = "getNoticeInfo")
	public Object getNoticeInfo(@NotNull @RequestParam Long noticeId, @NotNull @RequestParam Integer type) {
		//通知列表数据是合并2张表 hmparenttask（1），hmnotice（2）
		// type 1拜访类还是2跟进类 HmCommentEnum.comment_num_2.getType();

		return HmResponseUtil.ok(noticeService.getNoticeInfo(noticeId, type));
	}

	@ApiOperation(value = "跟进类，添加跟进记录", notes = "跟进类，添加跟进记录")
	@PostMapping(value = "addTicketAction")
	public Object addTicketAction(@RequestBody @Valid AddTicketActionDto dto) {

		ticketService.addActionHm(dto);
		return HmResponseUtil.ok();
	}

	@ApiOperation(value = "跟进类，创建新的任务", notes = "跟进类，创建新的任务")
	@PostMapping(value = "createNewTicket")
	public Object createNewTicket(@RequestBody @Valid HmNoticeDto dto) {

		// dto.setEmployRelationId(dto.getEmployRelationId());//三者关系id
		// dto.setCreateUserId(dto.getCreateUserId());// 登录的用户id

		// dto.setRType(HmNoticeEnum.NOTICE_NUM_10.getType());//通知枚举类型
		// dto.setRId("");//关联业务关系id
		// dto.setTaskSourceType(taskSourceType);//任务来源
		// HmNoticeEnum.getTaskSourceTypeList()
		dto.setNoticeUserType(HmNoticeEnum.getNoticeUserTypeByType(dto.getRtype()));// 获取需要通知几方
		dto.setAppUserId(hmNoticeService.getNoticeUserId(dto.getEmployRelationId(), dto.getNoticeUserType()));// 具体被通知的人

		noticeService.create(dto);
		return HmResponseUtil.ok();
	}

	@ApiOperation(value = "跟进类，跟进记录，创建任务的字典数据", notes = "跟进类，跟进记录，创建任务的字典数据")
	@GetMapping(value = "getTicketTaskDict")
	public Object getTicketTaskDict() {
		Map<String, Object> data = new HashMap<String, Object>();

		// 添加任务类
		data.put("taskType", HmNoticeEnum.getTaskTypeList());// 任务类型 jz_campaign
		data.put("taskSourceType", HmNoticeEnum.getTaskSourceTypeList());// 任务来源 jz_dm_ticket_consult_type

		// 跟进记录类型
		data.put("followType", HmTicketActionContans.getFollowTypeList());

		return HmResponseUtil.ok(data);
	}

	@ApiOperation(value = "工单完成", notes = "工单完成")
	@PostMapping("/ticketCompleted")
	public Object ticketCompleted(@RequestBody TicketAction action) {
		// action.setAdminId(adminId);//从登陆的用户id获取 前端传
		// action.setId(id);//工单id 前端传task的id
		// action.setMemo(remark);//完成内容 前端传
		// action.setResult(result);//ticketResult内容
		// action.setSkillGroupId(skillGroupId);//技能组 sql查询，默认取一个
		ticketService.completed(action);
		// 完成了通知盒马小程序用户
		noticeService.addNoticeUsers(action.getId(), action.getMemo(),
				TicketStatusConstant.STATUS_COMPLETED.toString());
		// 通知评论
		return HmResponseUtil.ok();
	}

	@ApiOperation(value = "工单完成-查询处理参数", notes = "工单完成-查询处理参数")
	@GetMapping("/ticketCompletedResult")
	public Object ticketCompleted(Integer ticketId, Integer userId) {
		if (null == ticketId || null == userId) {
			return HmResponseUtil.fail("ticketId和userId不能为空");
		}

		// 工单信息
		JzTicket ticket = ticketService.getTicketById(ticketId);
		if (ticket == null) {
			return HmResponseUtil.fail_null();
		}

		// 获取当前人的技能组
		Integer skillGroupId = skillGroupService.getSkillGroupIdByUserId(userId);

		// 获取工单的处理结果
		List<JzDmTicketResult> ticketResult = jzDmTicketResultService.queryByType(ticket.getTicketType().shortValue());

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("ticket", ticket);
		data.put("skillGroupId", skillGroupId);
		data.put("ticketResult", ticketResult);

		return HmResponseUtil.ok(data);
	}

	@PostMapping("/ticketUpgrade")
	@ApiOperation(value = "工单流转，升级", notes = "工单流转，升级")
	public Object ticketUpgrade(@RequestBody TicketAction action) {
//		action.setAdminId(adminId);//从登陆的用户id获取 前端传
		// action.setId(id);//工单id 前端传task的id
		// action.setMemo(remark);//完成内容 前端传
		// action.setResult(result);//ticketResult内容
		// action.setSkillGroupId(skillGroupId);//技能组 sql查询，默认取一个
		ticketService.upgrade(action);
		// 流转升级是crm内部通知，小程序用户不用知道
		return HmResponseUtil.ok();
	}

	@GetMapping("/getSkillGroupUser")
	@ApiOperation(value = "获取技能组下的用户", notes = "获取技能组下的用户")
	public Object getSkillGroupUser() {
		List<org.linlinjava.jz.db.dto.SkillGroupUserVo> allUsers = skillGroupService.getSkillSysUser();
		return HmResponseUtil.ok(allUsers);
	}

}
