package org.linlinjava.jz.hm.web;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.jz.db.domain.HmComment;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.dto.hm.HmCommentDto;
import org.linlinjava.jz.db.service.hm.HmCommentService;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.util.hm.HmCommentEnum;
import org.linlinjava.jz.db.util.hm.HmNoticeEnum;
import org.linlinjava.jz.dto.HmNoticeDto;
import org.linlinjava.jz.hm.util.HmResponseUtil;
import org.linlinjava.jz.service.NoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "评论", description = "评论")
@RestController
@RequestMapping("comment")
public class HmCommentController {

	@Autowired
	HmCommentService hmCommentService;

	@Autowired
	private HmEmployRelationService employRelationService;

	@Autowired
	private NoticeService noticeService;

	@ApiOperation(value = "保姆服务记录评价，管家拜访类/跟进类任务完成评论", notes = "保姆服务记录评价，管家拜访类/跟进类任务完成评论")
	@PostMapping(value = "statistics")
	public Object statistics(@Valid @RequestBody HmCommentDto hmCommentDto) {
		HmEmployRelation relation = employRelationService.queryById(hmCommentDto.getEmployRelationId());
		HmComment comment = new HmComment();
		BeanUtils.copyProperties(hmCommentDto, comment);
		comment.setOrderNo(relation.getOrderNo());
		hmCommentService.addComment(comment);
		// 插入评论，判断是否小于等于3星，分别插入通知和工单给管家
		if (hmCommentDto.getStar() <= 3) {

			HmNoticeDto hmNoticeDto = new HmNoticeDto();
			List<Long> appUserId = new ArrayList<Long>();
			appUserId.add(relation.getUserEmployerId());//是雇主
			hmNoticeDto.setAppUserId(appUserId);// 被通知的人
			hmNoticeDto.setEmployRelationId(relation.getId());
			hmNoticeDto.setCreateUserId(relation.getUserEmployerId());// 雇主的用户id
			hmNoticeDto.setrId(comment.getId().toString());//****插入crm任务builtlist的id

			// 雇主评价 拜访类 差评 
			if (HmCommentEnum.comment_num_1.getType() == hmCommentDto.getRType()) {
//				hmNoticeDto.setContent(HmNoticeEnum.getContentUser(HmNoticeEnum.NOTICE_NUM_11.getType()));
				hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_11.getType());
				hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_11.getNoticeUserType());
			}
			//雇主评价 跟进类 差评
			if (HmCommentEnum.comment_num_2.getType() == hmCommentDto.getRType()) {
//				hmNoticeDto.setContent(HmNoticeEnum.getContentUser(HmNoticeEnum.NOTICE_NUM_14.getType()));
				hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_14.getType());
				hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_14.getNoticeUserType());
			}

			// 雇主评价 保姆每日任务完成 差评
			if (HmCommentEnum.comment_num_3.getType() == hmCommentDto.getRType()) {
//				hmNoticeDto.setContent(HmNoticeEnum.getContentUser(HmNoticeEnum.NOTICE_NUM_8.getType()));
				hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_8.getType());
				hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_8.getNoticeUserType());
			}

			noticeService.create(hmNoticeDto);

		}
		return HmResponseUtil.ok();
	}

//	public Object statistics(@RequestBody HmCommentDto hmCommentDto) {
//		hmCommentService.addCommentDto(hmCommentDto);
//		return HmResponseUtil.ok();
//	}
}
