package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivitySurplusGiveExportDto;
import com.crm.manager.service.activity.CustomActivitySurplusGiveFeignService;
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.surplus.CustomActivitySurplusGiveDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.surplus.CustomActivitySurplusGiveSearcherEntity;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
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.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/custom/activity/surplus")
public class CustomActivitySurplusGiveController {

    @Autowired
    private CustomActivitySurplusGiveFeignService feignService;

    @PostMapping("/search/list")
    public ResultWithPagerDto<CustomActivitySurplusGiveDto> customSearcherList(@RequestBody @Validated CustomActivitySurplusGiveSearcherEntity entity) throws Exception {
        return feignService.pageList(entity);
    }

    @PostMapping("/recover/creadit")
    public BaseResultDto managerRecoverCreadit(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return feignService.managerRecoverCreadit(entity);
    }

    @PostMapping("/cancel")
    public BaseResultDto managerCancel(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return feignService.managerCancel(entity);
    }

    @PostMapping("/complete")
    public BaseResultDto managerComplete(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return feignService.managerComplete(entity);
    }

    @GetMapping("/search/list/export")
    public void searcherListExport(@RequestParam(name = "cId") Long cId,
                                   @RequestParam(name = "login") Long login,
                                   @RequestParam(name = "status") Integer status,
                                   HttpServletResponse response) throws Exception {

        CustomActivitySurplusGiveSearcherEntity entity = new CustomActivitySurplusGiveSearcherEntity();
        entity.setCId(cId);
        entity.setLogin(login);
        entity.setStatus(status);
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if (StringUtils.isEmpty(startDate)) {
            entity.setStartDate(DateUtil.getMonthStart(new Date()));
        } else {
            entity.setStartDate(sdf.parse(startDate));
        }
        if (StringUtils.isEmpty(endDate)) {
            entity.setEndDate(DateUtil.addDate(new Date(), 1));
        } else {
            entity.setEndDate(sdf.parse(endDate));
        }*/

        List<CustomActivitySurplusGiveDto> resultDto = feignService.searcherListExport(entity);
        List<CustomActivitySurplusGiveExportDto> exportDtos = Lists.newArrayList();
        resultDto.forEach((item) -> {
            CustomActivitySurplusGiveExportDto exportDto = new CustomActivitySurplusGiveExportDto();
            BeanUtils.copyProperties(item, exportDto);
            exportDto.setCID(item.getCId());
            exportDto.setAddtime(DateUtil.formatTime(item.getAddTime()));
            if(1 == item.getStatus()){
                exportDto.setStatus("任务中");
            }else if(2 == item.getStatus()){
                exportDto.setStatus("完成任务");
            }else if(3 == item.getStatus()){
                exportDto.setStatus("已取消");
            }else{
                exportDto.setStatus("已结束");
            }
            exportDtos.add(exportDto);
        });
        ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("CUSTOM_ACTIVITY_SURPLUS"), exportDtos, CustomActivitySurplusGiveExportDto.class, response);

    }

}
