package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomLoginCompletionExportDto;
import com.crm.manager.entity.excel.CustomLoginCompletionExcelEntity;
import com.crm.manager.entity.excel.WebActivityOcExcelEntity;
import com.crm.manager.service.CustomLoginCompletionService;
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.custom.login.completion.CustomLoginCompletionListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.login.completion.CustomLoginCompletionAddEntity;
import com.crm.rely.backend.core.entity.custom.login.completion.CustomLoginCompletionDeleteEntitys;
import com.crm.rely.backend.core.entity.custom.login.completion.CustomLoginCompletionSearchEntity;
import com.crm.rely.backend.core.entity.custom.login.completion.CustomLoginCompletionUpdateEntity;
import com.crm.rely.backend.core.entity.web.activity.oc.WebActivityOcAddEntity;
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.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
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.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/custom/login/completion")
public class CustomLoginCompletionController {

    @Autowired
    private CustomLoginCompletionService customLoginCompletionService;

    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated CustomLoginCompletionAddEntity entity) throws Exception {
        customLoginCompletionService.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated CustomLoginCompletionDeleteEntitys entitys) throws Exception {

        customLoginCompletionService.delete(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated CustomLoginCompletionUpdateEntity entity) throws Exception {
        customLoginCompletionService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<CustomLoginCompletionListDto> pageList(@RequestBody CustomLoginCompletionSearchEntity entity) throws Exception {

        return customLoginCompletionService.searchPageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        CustomLoginCompletionListDto result = customLoginCompletionService.searchSingle(entity);
        return BaseResultDto.success(result);
    }

    @GetMapping("/template/export")
    public void templateExport(HttpServletResponse response) throws Exception {
        List<CustomLoginCompletionExportDto> dtos = Lists.newArrayList();

        ExportUtil.transferToResponse(StringUtil.getXlsxFileName("CUSTOM_LOGIN_COMPLETION", false), dtos,
                CustomLoginCompletionExportDto.class, response);
    }

    @PostMapping("/import/excel")
    public BaseResultDto importExcel(@RequestParam("file") MultipartFile file) throws Exception {

        List<CustomLoginCompletionExcelEntity> entities = EasyExcelUti.readExcel(file,
                new CustomLoginCompletionExcelEntity(), 1, CustomLoginCompletionExcelEntity.class);

        if (entities == null || entities.size() <= 0) {
            return BaseResultDto.success();
        }

        List<CustomLoginCompletionAddEntity> addEntities = new ArrayList<>();
        for (CustomLoginCompletionExcelEntity customLoginCompletionExcelEntity : entities) {
            CustomLoginCompletionAddEntity entity = new CustomLoginCompletionAddEntity();
            entity.setPlatform(customLoginCompletionExcelEntity.getPlatform());
            entity.setLogin(Long.valueOf(customLoginCompletionExcelEntity.getLogin()));
            addEntities.add(entity);
        }

        customLoginCompletionService.adds(addEntities);

        return BaseResultDto.success();
    }

}
