package com.crm.manager.controller;

import com.crm.manager.service.BusinessEmailReceiveRecordService;
import com.crm.manager.service.BusinessEmailSentRecordService;
import com.crm.manager.service.CommonService;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.business.email.receive.BusinessEmailReceiveRecordDto;
import com.crm.rely.backend.core.dto.business.email.sent.BusinessEmailSentRecordDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.tenant.email.receive.BusinessEmailReceiveRecordSearchEntity;
import com.crm.rely.backend.core.entity.tenant.email.sent.BusinessEmailSentRecordAddEntity;
import com.crm.rely.backend.core.entity.tenant.email.sent.BusinessEmailSentRecordSearchEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author houn
 */
@RestController
@RequestMapping("/business/email")
public class BusinessEmailController {

    @Autowired
    private BusinessEmailReceiveRecordService receiveRecordService;

    @Autowired
    private BusinessEmailSentRecordService sentRecordService;

    @Autowired
    private CommonService commonService;

    @PostMapping("/sent")
    public BaseResultDto addRate(@RequestBody @Validated BusinessEmailSentRecordAddEntity entity,
                                 InfoEntity infoEntity) throws Exception {
        entity.setAgentId(infoEntity.getIbInfo().getId());
        sentRecordService.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/sent/list")
    public BaseResultDto searcherListRate(@RequestBody @Validated BusinessEmailSentRecordSearchEntity entity,
                                          InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }
        BaseResultDto resultDto = sentRecordService.searchList(entity);
        return resultDto;
    }

    @PostMapping("/search/receive/list")
    public BaseResultDto searcherListRate(@RequestBody @Validated BusinessEmailReceiveRecordSearchEntity entity,
                                          InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }
        BaseResultDto resultDto = receiveRecordService.searchList(entity);
        return resultDto;
    }

    @PostMapping("/search/sent/single")
    public BaseResultDto searcherSentSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        BusinessEmailSentRecordDto dto = sentRecordService.searchSingle(entity.getId());
        return BaseResultDto.success(dto);
    }

    @PostMapping("/search/receive/single")
    public BaseResultDto searcherReceiveSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        BusinessEmailReceiveRecordDto dto = receiveRecordService.searchSingle(entity.getId());
        return BaseResultDto.success(dto);
    }

    @PostMapping("/sent/upload")
    public BaseResultDto upload(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {


        String middlePath = "/business/email/" + DateUtil.formatDate();
        String path = commonService.uploadFile(middlePath, file);

        return BaseResultDto.success(path);
    }

}
