package com.storlead.sales.controller.mail;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.mail.UserClickRecordDTO;
import com.storlead.sales.pojo.entity.mail.UserClickRecordEntity;
import com.storlead.sales.service.customer.CustomerFollowUpService;
import com.storlead.mail.pojo.vo.UserClickTitleVO;
import com.storlead.mail.service.EmailsService;
import com.storlead.sales.service.UserClickRecordService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 用户点击记录 前端控制器
 * </p>
 *
 * @author chenkq
 * @since 2025-02-21
 */
@RestController
@RequestMapping("/click/record")
public class UserClickRecordController {

    @Resource
    private UserClickRecordService userClickRecordService;

    @Resource
    private EmailsService emailsService;

    @Resource
    private CustomerFollowUpService customerFollowUpService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "获取10条" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = UserClickTitleVO.class)
    })
    public Result<?> list(UserClickRecordDTO dto) {

        Long currentUserId = LoginUserUtil.getCurrentUserId();
        List<UserClickTitleVO> data;
        if (Integer.valueOf(1).equals(dto.getClickType())) {
            data = emailsService.selectUserClickTitle(currentUserId,dto.getTitle());
        } else {
            data = customerFollowUpService.selectUserClickTitle(currentUserId,dto.getTitle());
        }

        return Result.result(data);
    }

    @PostMapping(value = "/click_record")
    @ApiOperation(value = "点击" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = UserClickRecordEntity.class)
    })
    public Result<?> save(UserClickRecordEntity userClickRecord) {

        Long currentUserId = LoginUserUtil.getCurrentUserId();
        UserClickRecordEntity old = userClickRecordService.getOne(new LambdaQueryWrapper<UserClickRecordEntity>().eq(UserClickRecordEntity::getOwnerBy,currentUserId).eq(UserClickRecordEntity::getDataId, userClickRecord.getDataId()).eq(UserClickRecordEntity::getClickType,userClickRecord.getClickType()));
        if (Objects.nonNull(old)) {
            userClickRecord.setId(old.getId());
            userClickRecord.setClickCount(old.getClickCount() + 1);
        } else {
            userClickRecord.setClickCount(1);
            userClickRecord.setOwnerBy(currentUserId);
        }
        userClickRecordService.saveOrUpdate(userClickRecord);
        return Result.ok();
    }
}
