package com.storlead.sales.controller.customer;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.CommentQueryDTO;
import com.storlead.sales.pojo.dto.customer.RecordCommentDTO;
import com.storlead.sales.pojo.entity.customer.RecordCommentEntity;
import com.storlead.sales.pojo.vo.customer.RecordCommentVO;
import com.storlead.sales.enums.ResourceTypeEnum;
import com.storlead.sales.service.customer.MessageAssembleService;
import com.storlead.sales.service.customer.RecordCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 通用评论表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/record-comment")
@Api(tags = "销售: 通用评论相关接口")
public class RecordCommentController {
    @Resource
    private RecordCommentService recordCommentService;
    @Resource
    private MessageAssembleService assembleService;


    @PostMapping(value = "/list")
    @ApiOperation(value = "通用评论相关接口-通用评论-评论列表", notes = "根据通用ID集合查询通用列表(recordIds如：\"1,2,3,4\")")
    public Result<IPage<RecordCommentVO>> list(@RequestBody CommentQueryDTO dto) {
        // Step.1 组装查询条件
        return recordCommentService.fillTaskList(dto);
    }


    /**
     * 添加
     *
     * @param recordCommentDTO
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "通用评论相关接口-通用评论-添加", notes = "通用评论相关接口-通用评论-添加")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> add(@RequestBody RecordCommentDTO recordCommentDTO) {
        RecordCommentEntity recordCommentEntity = new RecordCommentEntity();
        BeanUtils.copyProperties(recordCommentDTO,recordCommentEntity);
        if(recordCommentEntity.getContent().isEmpty() && CollectionUtils.isEmpty(recordCommentDTO.getTagIdls())){
            return Result.error("回复不能为空");
        }
        Long userId = LoginUserUtil.getCurrentUserId();
        recordCommentEntity.setStaffId(userId);
        boolean isOk = recordCommentService.save(recordCommentEntity);
        if(ResourceTypeEnum.FOLLOW_UP.code.equals(recordCommentDTO.getType())) {
            assembleService.sendRecordCommentMessages(recordCommentDTO.getRecordId(), "COMMENT_FOLLOW_UP");
        }
        if (recordCommentDTO.getType().equals("120")){
            LambdaQueryWrapper<RecordCommentEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RecordCommentEntity::getId,recordCommentDTO.getParentId());
            queryWrapper.eq(RecordCommentEntity::getIsDelete,0);
            List<RecordCommentEntity> recordList = recordCommentService.list(queryWrapper);
            if (recordList.size() > 0){
                RecordCommentEntity recordComment = recordList.get(0);
                assembleService.sendBulletinCommentMessages(recordComment.getOwnerBy(), recordCommentDTO.getRecordId(), "COMMENT_BULLETIN");
            }else {
                assembleService.sendBulletinCommentMessages(null, recordCommentDTO.getRecordId(), "COMMENT_BULLETIN");
            }
        }

        if(isOk) {
            //发送消息
//            if (recordCommentDTO.getType().equals(ResourceTypeEnum.CLUS.code)) {
//                assembleService.sendCustomerEventMessages(customerTransferDTO.getIdList().get(0), "COMMENT_FOLLOW_UP");
//            }
//            if (recordCommentDTO.getType().equals(ResourceTypeEnum.CUSTOMER.code)) {
//                assembleService.sendCustomerEventMessages(customerTransferDTO.getIdList().get(0), "TRANSFER_CUSTOMER");
//            }
//            if (recordCommentDTO.getType().equals(ResourceTypeEnum.BUSINESS.code)){
//                assembleService.sendBusinessEventMessages(customerTransferDTO.getIdList().get(0),"TRANSFER_OPPORTUNITY");
//            }
//            if (recordCommentDTO.getType().equals(ResourceTypeEnum.ORDER_FORM.code)){
//                assembleService.sendOrderEventMessages(customerTransferDTO.getIdList().get(0),"TRANSFER_ORDER");
//            }

            return Result.result("评论成功！");
        }else {
            return Result.error("评论失败！");
        }
    }

    /**
     * 通过id删除
     *
     * @param commentId
     * @return
     */
    @PostMapping(value = "/delete")
    @ApiOperation(value = "通用评论相关接口-通用评论-删除", notes = "通用评论相关接口-通用评论-评论删除(根据评论ID删除任务)")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> delete(@RequestParam(name="commentId",required=true) Long commentId) {
        RecordCommentEntity recordCommentEntity = new RecordCommentEntity();
        recordCommentEntity.setId(commentId);
        recordCommentEntity.setIsDelete(1);
        boolean isOk = recordCommentService.updateById(recordCommentEntity);

        RecordCommentEntity recordComment = recordCommentService.getById(commentId);
        if(!isOk) {
            return Result.error("删除失败！");
        }
        //递归删除所有子评论
        recordCommentService.removeByParentid(commentId);
        return Result.result("删除成功!");
    }
}
