package com.crm.manager.controller;

import com.crm.manager.service.MarketUploadEmailFeignService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.market.upload.email.MarketUploadEmailListDto;
import com.crm.rely.backend.core.dto.market.upload.email.detail.MarketUploadEmailDetailListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.market.email.upload.MarketUploadEmailDeleteEntity;
import com.crm.rely.backend.core.entity.market.email.upload.MarketUploadEmailSearchPageEntity;
import com.crm.rely.backend.core.entity.market.email.upload.detail.MarketUploadEmailDetailDeleteEntity;
import com.crm.rely.backend.core.entity.market.email.upload.detail.MarketUploadEmailDetailSearchPageEntity;
import com.crm.rely.backend.core.entity.market.email.upload.detail.MarketUploadEmailDetailUpdateEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
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;

import javax.servlet.http.HttpServletResponse;

/**
 * 上传邮件列表
 *
 * @author houn
 */
@RestController
@RequestMapping("/market/upload/email")
public class MarketUploadEmailController {

    @Autowired
    private MarketUploadEmailFeignService marketUploadEmailFeignService;

    /**
     * 填加上传列表
     *
     * @param name
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestParam("file") MultipartFile file, @RequestParam("name") String name) throws Exception {


        return marketUploadEmailFeignService.add(file, name);
    }


    /**
     * 查看上传列表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public ResultWithPagerDto<MarketUploadEmailListDto> pageList(@RequestBody @Validated MarketUploadEmailSearchPageEntity entity) throws Exception {


        return marketUploadEmailFeignService.pageList(entity);
    }


    /**
     * 查看单个上传列表记录
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto<MarketUploadEmailListDto> single(@RequestBody @Validated SingleLongEntity entity) throws Exception {


        return marketUploadEmailFeignService.single(entity);
    }

    /**
     * 查看详情列表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list/detail")
    public ResultWithPagerDto<MarketUploadEmailDetailListDto> pageListDetail(@RequestBody @Validated MarketUploadEmailDetailSearchPageEntity entity, InfoEntity infoEntity) throws Exception {

        return marketUploadEmailFeignService.pageListDetail(entity);
    }

    /**
     * 查看详情的单个记录条数
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single/detail")
    public BaseResultDto<MarketUploadEmailDetailListDto> singleDetail(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return marketUploadEmailFeignService.singleDetail(entity);
    }

    /**
     * 修改详情的内容
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update/detail")
    public BaseResultDto updateDetail(@RequestBody @Validated MarketUploadEmailDetailUpdateEntity entity) throws Exception {


        return marketUploadEmailFeignService.updateDetail(entity);
    }

    /**
     * 删除删除列表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated MarketUploadEmailDeleteEntity entity) throws Exception {


        return marketUploadEmailFeignService.delete(entity);
    }

    /**
     * 删除详情记录
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete/detail")
    public BaseResultDto deleteDetail(@RequestBody @Validated MarketUploadEmailDetailDeleteEntity entity) throws Exception {


        return marketUploadEmailFeignService.deleteDetail(entity);
    }

    @GetMapping("/template/export")
    public void templateExport(HttpServletResponse response) throws Exception {


//        HttpServletResponse resultResponse= marketUploadEmailFeignService.templateExport();

        byte[] data = marketUploadEmailFeignService.templateExport();
        ExportUtil.transferToResponse(StringUtil.getXlsxFileName("EMIL-TEMPLATE", false), data, response);

    }
}
