package com.crm.manager.controller;

import com.crm.manager.dto.export.WebUploadCidExportDto;
import com.crm.manager.entity.excel.WebUploadCidExcelEntity;
import com.crm.manager.service.activity.WebUploadCidFeignService;
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.web.upload.WebUploadCidDto;
import com.crm.rely.backend.core.dto.web.upload.detail.WebUploadCidDetailDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.web.upload.WebUploadCidAddEntity;
import com.crm.rely.backend.core.entity.web.upload.WebUploadCidDeleteEntity;
import com.crm.rely.backend.core.entity.web.upload.WebUploadCidSearchPageEntity;
import com.crm.rely.backend.core.entity.web.upload.detail.WebUploadCidDetailAddEntity;
import com.crm.rely.backend.core.entity.web.upload.detail.WebUploadCidDetailDeleteEntity;
import com.crm.rely.backend.core.entity.web.upload.detail.WebUploadCidDetailSearchPageEntity;
import com.crm.rely.backend.core.entity.web.upload.detail.WebUploadCidDetailUpdateEntity;
import com.crm.rely.backend.util.EasyExcelUti;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
import com.google.common.collect.Lists;
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;
import java.util.List;

/**
 * 上传列表
 *
 * @author houn
 */
@RestController
@RequestMapping("/web/upload/cid")
public class WebUploadCidController {

    @Autowired
    private WebUploadCidFeignService webUploadCidService;

    /**
     * 填加上传列表
     *
     * @param name
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) throws Exception {
        WebUploadCidAddEntity entity = new WebUploadCidAddEntity();
        entity.setName(name);
        List<WebUploadCidDetailAddEntity> marketUploadEmailDetailAddEntities = EasyExcelUti.readExcel(file,
                new WebUploadCidExcelEntity(), 1, WebUploadCidDetailAddEntity.class);
        entity.setNumber(marketUploadEmailDetailAddEntities.size());
        entity.setDetailAddEntities(marketUploadEmailDetailAddEntities);

        return webUploadCidService.add(entity);
    }

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

        return webUploadCidService.pageList(entity);
    }

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

        return webUploadCidService.single(entity);
    }

    /**
     * 查看详情列表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list/detail")
    public ResultWithPagerDto<WebUploadCidDetailDto> pageListDetail(@RequestBody @Validated WebUploadCidDetailSearchPageEntity entity) throws Exception {
        return webUploadCidService.pageListDetail(entity);
    }

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

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

        return webUploadCidService.updateDetail(entity);
    }

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

        return webUploadCidService.delete(entity);
    }

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

        return webUploadCidService.deleteDetail(entity);
    }

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

        List<WebUploadCidExportDto> dtos = Lists.newArrayList();

        ExportUtil.transferToResponse(StringUtil.getXlsxFileName("CID-TEMPLATE", false), dtos,
                WebUploadCidExportDto.class, response);

    }

}
