package com.storlead.sales.controller.mail;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.sales.pojo.dto.customer.CustomerMailDTO;
import com.storlead.mail.pojo.entity.SmtpPopSettingsEntity;
import com.storlead.sales.pojo.entity.mail.MailClickRecordEntity;
import com.storlead.sales.service.MailClickRecordService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.util.CollectionUtils;
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.Objects;

/**
 * <p>
 * mail_click_record邮件点击记录 前端控制器
 * </p>
 *
 * @author chenkq
 * @since 2024-09-25
 */
@RestController
@RequestMapping("/mail/click")
public class MailClickRecordController {

    @Resource
    private MailClickRecordService clickRecordService;
    @PostMapping(value = "/click_record")
    @ApiOperation(value = "点击" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> getDetail(Long customerMailId) {
        MailClickRecordEntity clickRecord = new MailClickRecordEntity();
        clickRecord.setCustomerMailId(customerMailId);
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        clickRecord.setDeptName(loginUser.getDeptName());
        clickRecordService.save(clickRecord);

        LambdaQueryWrapper<MailClickRecordEntity> queryWrapper = new LambdaQueryWrapper();
        if (Objects.nonNull(customerMailId)) {
            queryWrapper.eq(MailClickRecordEntity::getCustomerMailId,customerMailId);
        }

        int longCount = clickRecordService.count(queryWrapper);
        return Result.result(longCount);
    }


    @PostMapping(value = "/page_list")
    @ApiOperation(value = "查看记录" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> pageList(CustomerMailDTO dto) {
        if (Objects.isNull(dto.getCustomerMailId())) {
            return Result.error("参数错误");
        }
        Page<MailClickRecordEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<MailClickRecordEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(MailClickRecordEntity::getCustomerMailId,dto.getCustomerMailId());
        IPage<MailClickRecordEntity> pageList = clickRecordService.page(page,queryWrapper);
        if (!CollectionUtils.isEmpty(pageList.getRecords())) {
            SysBaseField.orgFileds(pageList.getRecords());
        }
        return Result.result(pageList);
    }
}
