package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivityApplyExportDto;
import com.crm.manager.service.activity.CustomActivityApplyFeignService;
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.activity.CustomActivityApplySearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.CustomActivityApplyApproveEntity;
import com.crm.rely.backend.core.entity.custom.activity.CustomActivityApplyDeleteEntity;
import com.crm.rely.backend.core.entity.custom.activity.CustomActivityApplySearchEntity;
import com.crm.rely.backend.core.entity.custom.activity.CustomActivityApplyUpdateEntity;
import com.crm.rely.backend.core.pojo.view.CustomActivityApplyExportView;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @description: 客户活动控制器
 * @author: houn
 * @create: 2020-06-13 18:01
 **/
@RestController
@RequestMapping("/custom/activity")
public class CustomActivityApplyController {

    @Autowired
    private CustomActivityApplyFeignService customActivityApplyFeignService;

    @PostMapping("/approve")
    public BaseResultDto approve(@RequestBody @Validated CustomActivityApplyApproveEntity entity) throws Exception {
        return customActivityApplyFeignService.approve(entity);
    }

    @PostMapping("/de/complete")
    public BaseResultDto deComplete(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return customActivityApplyFeignService.deComplete(entity);
    }

    @PostMapping("/complete")
    public BaseResultDto complete(@RequestBody @Validated CustomActivityApplyApproveEntity entity) throws Exception {

        return customActivityApplyFeignService.complete(entity);
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated CustomActivityApplyDeleteEntity entity) throws Exception {

        return customActivityApplyFeignService.delete(entity);
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated CustomActivityApplyUpdateEntity entity) throws Exception {

        return customActivityApplyFeignService.update(entity);
    }

    @PostMapping("/searcher/list")
    public BaseResultDto searcherList(@RequestBody @Validated CustomActivityApplySearchEntity entity) throws Exception {

        ResultWithPagerDto<CustomActivityApplySearchDto> resultWithPagerDto =
                customActivityApplyFeignService.searcherList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/searcher/passed/list")
    public BaseResultDto searcherPassedList(@RequestBody @Validated CustomActivityApplySearchEntity entity) throws Exception {

        ResultWithPagerDto<CustomActivityApplySearchDto> resultWithPagerDto =
                customActivityApplyFeignService.searcherPassedList(entity);
        return resultWithPagerDto;
    }

    @GetMapping(value = {"/export", "/passed/export"})
    public void exportList(@RequestParam(name = "login") Long login,
                           @RequestParam(name = "cId") Long cId,
                           @RequestParam(name = "startDate") String startDate,
                           @RequestParam(name = "endDate") String endDate,
                           @RequestParam(name = "status") Integer status,
                           @RequestParam(name = "completeStatus") Integer completeStatus,
                           HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomActivityApplySearchEntity entity = new CustomActivityApplySearchEntity();
        entity.setLogin(login);
        entity.setCId(cId);
        entity.setStatus(status);
        entity.setCompleteStatus(completeStatus);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        BaseResultDto<List<CustomActivityApplyExportView>> resultDto = null;
        switch (request.getRequestURI()) {
            case "/custom/activity/export":
                resultDto = customActivityApplyFeignService.exportList(entity);
                break;
            case "/custom/activity/passed/export":
                resultDto = customActivityApplyFeignService.exportPassedList(entity);
                break;
        }
        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomActivityApplyExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomActivityApplyExportDto dto = new CustomActivityApplyExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("CUSTOM_ACTIVITY", true), dtos,
                    CustomActivityApplyExportDto.class, response);
        }
    }

    @PostMapping("/searcher/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        CustomActivityApplySearchDto customActivityApplySearchDto =
                customActivityApplyFeignService.searcherSingle(entity);
        return BaseResultDto.success(customActivityApplySearchDto);
    }

}
